From 741861aef8c7fa81bfddf4eab67f3f2cf4ccf53b Mon Sep 17 00:00:00 2001 From: Takahiro Ueda Date: Tue, 7 Mar 2023 12:30:27 +0900 Subject: [PATCH] fix: possible memory error in calling macros with arguments (#434) The pointer to the currently processed preprocessor variable is invalidated when the list of preprocessor variables is extended. Close #434. --- check/fixes.frm | 12 ++++++++++++ sources/pre.c | 3 +++ 2 files changed, 15 insertions(+) diff --git a/check/fixes.frm b/check/fixes.frm index ed00e021..265a7209 100644 --- a/check/fixes.frm +++ b/check/fixes.frm @@ -2285,3 +2285,15 @@ Print +s; assert succeeded? assert result("F3") =~ expr("+D") *--#] Issue405 : +*--#[ Issue434 : +* Memory error with macros with arguments +* The loop (appending a variable and calling the macro) is repeated enough +* to hit the problem (Valgrind error). +#define var(a) "`~a'" +#do n=1,{12*2^5} + #define x + #message `var(`n')' +#enddo +.end +assert succeeded? +*--#] Issue434 : diff --git a/sources/pre.c b/sources/pre.c index b6697201..6aa45bf4 100644 --- a/sources/pre.c +++ b/sources/pre.c @@ -310,6 +310,7 @@ UBYTE GetChar(int level) */ int nargs = 1; PREVAR *p; + size_t p_offset; *s++ = 0; namebuf[i-2] = 0; if ( StrICmp(namebuf,(UBYTE *)"random_") == 0 ) { UBYTE *ranvalue; @@ -367,6 +368,7 @@ UBYTE GetChar(int level) while ( *s ) s++; s++; t = p->argnames; + p_offset = p - PreVar; for ( j = 0; j < p->nargs; j++ ) { if ( ( nargs == p->nargs-1 ) && ( *t == '?' ) ) { PutPreVar(t,0,0,0); @@ -376,6 +378,7 @@ UBYTE GetChar(int level) while ( *s ) s++; s++; } + p = PreVar + p_offset; while ( *t ) t++; t++; }