From 2958619726056ce8db714576220536887631447d Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 8 Jan 2023 15:15:32 -0600 Subject: [PATCH] Fix varargs stack repair. Varargs-only stack repair (i.e. using #pragma optimize bit 3 but not bit 6) was broken by commit 32975b720fb4d79. It removed some code that was needed to allocate the direct page location used to hold the stack pointer value in that case. This would lead to invalid code being produced, which could cause a crash when run. The fix is to revert the erroneous parts of commit 32975b720fb4d79 (which do not affect its core purpose of enabling intermediate code peephole optimization to be used when stack repair code is active). --- CGI.pas | 1 + Expression.pas | 2 ++ Gen.pas | 2 +- Parser.pas | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CGI.pas b/CGI.pas index ffd53fd..759d137 100644 --- a/CGI.pas +++ b/CGI.pas @@ -364,6 +364,7 @@ var symLength: integer; {length of debug symbol table} toolParms: boolean; {generate tool format parameters?} volatile: boolean; {has a volatile qualifier been used?} + hasVarargsCall: boolean; {does current function call any varargs fns?} {desk accessory variables} {------------------------} diff --git a/Expression.pas b/Expression.pas index 3bec4e3..9618172 100644 --- a/Expression.pas +++ b/Expression.pas @@ -3508,6 +3508,8 @@ var Gen1tName(pc_cup, ord(hasVarargs and strictVararg), UsualUnaryConversions, fname); end; {else} + if hasVarargs then + hasVarargsCall := true; end {if} else GenTool(pc_tl1, ftype^.toolNum, long(ftype^.ftype^.size).lsw, diff --git a/Gen.pas b/Gen.pas index 9a554db..2a9c094 100644 --- a/Gen.pas +++ b/Gen.pas @@ -7679,7 +7679,7 @@ while bk <> nil do begin end; {while} bk := bk^.next; end; {while} -if saveStack or checkStack then begin +if saveStack or checkStack or (strictVararg and hasVarargsCall) then begin stackLoc := minSize; minSize := minSize + 2; localSize := localSize + 2; diff --git a/Parser.pas b/Parser.pas index 2c14fa0..25bf3d1 100644 --- a/Parser.pas +++ b/Parser.pas @@ -4019,6 +4019,7 @@ if isFunction then begin end; {if} Gen2Name(dc_str, segType, 0, fName); doingMain := variable^.name^ = 'main'; + hasVarargsCall := false; firstCompoundStatement := true; Gen0 (dc_pin); if not isAsm then