From ce1587186204123712c67532137f330acb7c755c Mon Sep 17 00:00:00 2001 From: Satya Das Date: Tue, 16 Oct 2018 04:25:22 +0200 Subject: [PATCH 1/2] yyinput() didn't correctly store yy_hold_char --- src/flex.skl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flex.skl b/src/flex.skl index d9cdcc01e..731443f05 100644 --- a/src/flex.skl +++ b/src/flex.skl @@ -1893,8 +1893,8 @@ m4_ifdef( [[M4_YY_USE_LINENO]], } c = *(unsigned char *) YY_G(yy_c_buf_p); /* cast for 8-bit char's */ - *YY_G(yy_c_buf_p) = '\0'; /* preserve yytext */ YY_G(yy_hold_char) = *++YY_G(yy_c_buf_p); + *YY_G(yy_c_buf_p) = '\0'; /* preserve yytext */ %% [19.0] update BOL and yylineno From d309ad710b3417cf98c0736b0673f8ea15c37895 Mon Sep 17 00:00:00 2001 From: Satya Das Date: Wed, 17 Oct 2018 20:46:16 +0200 Subject: [PATCH 2/2] Adding tests for changes in yyinput --- tests/.gitignore | 2 + tests/Makefile.am | 5 ++- tests/yyinput.l | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/yyinput.l diff --git a/tests/.gitignore b/tests/.gitignore index e5c42b37c..a5dcde6e7 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -99,6 +99,8 @@ top top.[ch] yyextra yyextra.c +yyinput +yyinput.c tableopts_*.c *.opt *.ser diff --git a/tests/Makefile.am b/tests/Makefile.am index 65f9d65a8..77dadecf6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -93,7 +93,8 @@ simple_tests = \ string_nr \ string_r \ top \ - yyextra + yyextra \ + yyinput reject_tests = \ reject_nr.reject \ @@ -182,6 +183,7 @@ string_r_SOURCES = string_r.l top_SOURCES = top.l top_main.c nodist_top_SOURCES = top.h yyextra_SOURCES = yyextra.l +yyinput_SOURCES = yyinput.l # Normally, automake would distribute files built by flex. Since the # point of the test suite is to test the files that flex builds, and @@ -261,6 +263,7 @@ CLEANFILES = \ top.c \ top.h \ yyextra.c \ + yyinput.c \ $(tableopts_c) \ $(tableopts_tables) diff --git a/tests/yyinput.l b/tests/yyinput.l new file mode 100644 index 000000000..8d9d15cc5 --- /dev/null +++ b/tests/yyinput.l @@ -0,0 +1,100 @@ +/* + * This file is part of flex. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE. + */ + +%{ +/* The goal of this test is to verify that yyinput() sets correct byte to null + * and also when called again it resets the correct byte to it's original value. + */ +#include +#include + +#define TESTBUF_SIZE 6 + +static char testBuffers[][TESTBUF_SIZE] = { + "flex\0\0", + "FLEX\0\0" +}; + +/* C lexer defines input() instead of yyinput() */ +#ifndef __cplusplus +# define yyinput input +#endif + +%} + +%option 8bit prefix="test" +%option nounput nomain noyywrap +%option warn posix-compat + + +%% + +"fle" { + yyinput(); +} + +"F" { + yyinput(); + yyinput(); + yyinput(); +} + +%% + +int testYYInput(char* testBuf) +{ + YY_BUFFER_STATE state; + + const char* origTestStr = strdup(testBuf); + state = test_scan_buffer(testBuf, TESTBUF_SIZE); + testlex(); + yy_delete_buffer(state); + if (strcmp(origTestStr, testBuf) != 0) + { + printf("FAILED: Expected %s but left with %s\n", origTestStr, testBuf); + return 1; + } + + free((void*) origTestStr); + printf("OK\n"); + return 0; +} + +int main (void) +{ + int result = 0; + /* Run the tests */ + printf("Testing yyinput()\n"); + result += testYYInput(testBuffers[0]); + result += testYYInput(testBuffers[1]); + + if (result > 0) + { + printf("TEST FAILED.\n"); + exit(1); + } + + printf("TEST RETURNING OK.\n"); + return 0; +} +