From 586229e6eb62180987bf89f5f63aee7e5a163ec8 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sun, 31 Dec 2023 11:34:25 -0500 Subject: [PATCH] #define should always use the global pool.... if a #define is within a function, it could use the local memory pool for string allocation (via Malloc in NextToken, line 5785) which can lead to a dangling memory reference when the macro is expanded. void function(void) { #define TEXT "abc" static struct { char text[sizeof(TEXT)]; } template = { TEXT }; } --- Scanner.pas | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Scanner.pas b/Scanner.pas index 78c7873..90fe9a5 100644 --- a/Scanner.pas +++ b/Scanner.pas @@ -2786,8 +2786,12 @@ var ple: stringListPtr; {pointer to the last element in parameterList} pnum: integer; {for counting parameters} tPtr,tk1,tk2: tokenListRecordPtr; {pointer to a token} + luseGlobalPool: boolean; {local copy of useGlobalPool} begin {DoDefine} + + lUseGlobalPool := useGlobalPool; + useGlobalPool := true; {use global memory for defines} expandMacros := false; {block expansions} saveNumber := true; {save characters in numeric tokens} parameterList := nil; {no parameters yet} @@ -2999,6 +3003,7 @@ var dispose(np); end; {while} saveNumber := false; {stop saving numeric strings} + useGlobalPool := lUseGlobalPool; end; {DoDefine}