flex icon indicating copy to clipboard operation
flex copied to clipboard

scanner: fix buffer stack popping in yylex_destroy

Open happyCoder92 opened this issue 7 years ago • 1 comments

Fixes #392.

happyCoder92 avatar Sep 28 '18 13:09 happyCoder92

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

jannick0 avatar Oct 15 '18 21:10 jannick0

Please rebase and resolve conflicts.

westes avatar Apr 25 '24 19:04 westes

Rebased.

happyCoder92 avatar Apr 29 '24 13:04 happyCoder92