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

scanner: fix buffer stack popping in yylex_destroy #393

Merged
merged 1 commit into from
Apr 29, 2024

Conversation

happyCoder92
Copy link
Contributor

Fixes #392.

@jannick0
Copy link
Contributor

jannick0 commented Oct 15, 2018

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:

flex/src/flex.skl

Lines 2899 to 2903 in 98018e3

while(YY_CURRENT_BUFFER){
yy_delete_buffer( YY_CURRENT_BUFFER M4_YY_CALL_LAST_ARG );
YY_CURRENT_BUFFER_LVALUE = NULL;
yypop_buffer_state(M4_YY_CALL_ONLY_ARG);
}

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

@westes westes added this to the 2.6.6 milestone Oct 22, 2018
@westes
Copy link
Owner

westes commented Apr 25, 2024

Please rebase and resolve conflicts.

@happyCoder92
Copy link
Contributor Author

Rebased.

@westes westes merged commit cab0bbc into westes:master Apr 29, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak - buffer stack cleanup
3 participants