onetrueawk / awk

One true awk
Other
1.98k stars 159 forks source link

Plug a regex memory leak. #156

Closed millert closed 2 years ago

millert commented 2 years ago

The string returned by reg_expr was never freed after conversion to a dfa.

mpinjr commented 2 years ago

Hi, millert:

What do you think of this alternative?

diff --git a/lex.c b/lex.c
index d7b2d03..c162a70 100644
--- a/lex.c
+++ b/lex.c
@@ -545,7 +545,7 @@ int regexpr(void)
        *bp = 0;
        if (c == 0)
                SYNTAX("non-terminated regular expression %.10s...", buf);
-       yylval.s = tostring(buf);
+       yylval.s = buf;
        unput('/');
        RET(REGEXPR);
 }

This tostring is the source of the leaked memory and it's unnecessary (makedfa->mkdfa will make a copy).

The last bit of your commit seems to regard an unrelated grammatical matter which my proposal does not address.

Take care, Miguel

millert commented 2 years ago

Yes, that looks better indeed. I didn't intend for that last hunk to be included, too many tree :-)

millert commented 2 years ago

Closing in favor of the fix from @mpinjr