flex
flex copied to clipboard
scanner: fix buffer stack popping in yylex_destroy
Fixes #392.
Memory leakage issue confirmed and that the proposed commit remedies it. Appears to be in the world for decades now. Currently only the top buffer is freed up, all others left alone: https://github.com/westes/flex/blob/98018e3f58d79e082216d406866942841d4bdf8a/src/flex.skl#L2899-L2903 A simple example:
/* buffer-stack-memory-leakage.l */
%option noyywrap noinput nounput
%%
.
%%
int main ( int argc, char** argv )
{
int i, n, rc = 0;
char s[20];
FILE* fp;
if ( argc == 1 || ( n = atoi ( argv[1] ) ) <=0 ) {
fprintf ( stderr, "First command line argument expected to be an integer > 0"
" (number of buffers to be created).\n" );
return 1;
}
for ( i=1; i<=n; i++ ) {
sprintf ( s, "%d.txt", i );
fp = fopen ( s, "w" );
if ( !fp ) {
perror ( s );
rc = 1;
goto cleanup;
}
fclose ( fp );
fp = fopen ( s, "r" );
if ( !fp ) {
perror ( s );
rc = 1;
goto cleanup;
}
yypush_buffer_state ( yy_create_buffer ( fp, 256 ) );
}
cleanup:
yylex_destroy();
fclose ( NULL );
return rc;
}
Please rebase and resolve conflicts.
Rebased.