Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yyinput() didn't correctly store yy_hold_char #396

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/flex.skl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ top
top.[ch]
yyextra
yyextra.c
yyinput
yyinput.c
tableopts_*.c
*.opt
*.ser
Expand Down
5 changes: 4 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ simple_tests = \
string_nr \
string_r \
top \
yyextra
yyextra \
yyinput

reject_tests = \
reject_nr.reject \
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -261,6 +263,7 @@ CLEANFILES = \
top.c \
top.h \
yyextra.c \
yyinput.c \
$(tableopts_c) \
$(tableopts_tables)

Expand Down
100 changes: 100 additions & 0 deletions tests/yyinput.l
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>

#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;
}