From: Joel E. Denny Date: Fri, 10 Nov 2006 05:26:26 +0000 (+0000) Subject: Fix memory leaks in scanners generated by at least Flex 2.5.9 and X-Git-Tag: v2.3b~243 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/580b892607e7709b316003f31c825231b7dc6d62 Fix memory leaks in scanners generated by at least Flex 2.5.9 and later. Reported by Paul Eggert in . * src/flex-scanner.h (yylex_destroy): Define for Flex before 2.5.9. * src/scan-code.l (translate_action): Don't bother invoking yy_delete_buffer (YY_CURRENT_BUFFER) before creating the first buffer. (code_scanner_free): Instead of invoking yy_delete_buffer (YY_CURRENT_BUFFER) directly, invoke yylex_destroy, which frees more. * src/scan-gram.l (gram_scanner_free): Likewise. * src/scan-skel.l (scan_skel): Likewise. --- diff --git a/ChangeLog b/ChangeLog index d558baca..2d954577 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2006-11-10 Joel E. Denny + + Fix memory leaks in scanners generated by at least Flex 2.5.9 and + later. Reported by Paul Eggert in + . + * src/flex-scanner.h (yylex_destroy): Define for Flex before 2.5.9. + * src/scan-code.l (translate_action): Don't bother invoking + yy_delete_buffer (YY_CURRENT_BUFFER) before creating the first buffer. + (code_scanner_free): Instead of invoking + yy_delete_buffer (YY_CURRENT_BUFFER) directly, invoke yylex_destroy, + which frees more. + * src/scan-gram.l (gram_scanner_free): Likewise. + * src/scan-skel.l (scan_skel): Likewise. + 2006-11-09 Joel E. Denny * src/files.c (tr): Change return type to void. diff --git a/src/flex-scanner.h b/src/flex-scanner.h index 3e16234c..5d08dec6 100644 --- a/src/flex-scanner.h +++ b/src/flex-scanner.h @@ -50,6 +50,19 @@ int FLEX_PREFIX (lex_destroy) (void); # define yytext FLEX_PREFIX (text) #endif +/* Non-reentrant scanners generated by Flex 2.5.9 and later (and some earlier + versions according to the Flex manual) leak memory if yylex_destroy is not + invoked. However, yylex_destroy is not defined before Flex 2.5.9, so give + an implementation here that at least appears to work with Flex 2.5.4. */ +#if !defined(YY_FLEX_MAJOR_VERSION) || YY_FLEX_MAJOR_VERSION < 2 \ + || (YY_FLEX_MAJOR_VERSION == 2 \ + && (!defined(YY_FLEX_MINOR_VERSION) || YY_FLEX_MINOR_VERSION < 5 \ + || (YY_FLEX_MINOR_VERSION == 5 \ + && (!defined(YY_FLEX_SUBMINOR_VERSION) \ + || YY_FLEX_SUBMINOR_VERSION < 9)))) +# define yylex_destroy() yy_delete_buffer (YY_CURRENT_BUFFER) +#endif + /* OBSTACK_FOR_STRING -- Used to store all the characters that we need to keep (to construct ID, STRINGS etc.). Use the following macros to use it. diff --git a/src/scan-code.l b/src/scan-code.l index 6b33e015..0a338b4e 100644 --- a/src/scan-code.l +++ b/src/scan-code.l @@ -380,8 +380,6 @@ translate_action (int sc_context, symbol_list *rule, char const *a, location l) if (!initialized) { obstack_init (&obstack_for_string); - /* The initial buffer, never used. */ - yy_delete_buffer (YY_CURRENT_BUFFER); yy_flex_debug = 0; initialized = true; } @@ -422,5 +420,5 @@ code_scanner_free (void) { obstack_free (&obstack_for_string, 0); /* Reclaim Flex's buffers. */ - yy_delete_buffer (YY_CURRENT_BUFFER); + yylex_destroy (); } diff --git a/src/scan-gram.l b/src/scan-gram.l index 6fa9f220..88369ea6 100644 --- a/src/scan-gram.l +++ b/src/scan-gram.l @@ -800,5 +800,5 @@ gram_scanner_free (void) { obstack_free (&obstack_for_string, 0); /* Reclaim Flex's buffers. */ - yy_delete_buffer (YY_CURRENT_BUFFER); + yylex_destroy (); } diff --git a/src/scan-skel.l b/src/scan-skel.l index e787d288..6c516097 100644 --- a/src/scan-skel.l +++ b/src/scan-skel.l @@ -121,5 +121,5 @@ scan_skel (FILE *in) skel__flex_debug = trace_flag & trace_skeleton; skel_lex (); /* Reclaim Flex's buffers. */ - yy_delete_buffer (YY_CURRENT_BUFFER); + yylex_destroy (); }