From 8b4213cd5acf3653536830be7e8bd641edf6c32a Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Fri, 14 Sep 2018 00:22:10 -0500 Subject: [PATCH] Fix bug where the condition check of the ?: operator may be mis-evaluated. This could happen in certain cases where the condition codes might not be set at expected. The following program gives an example: #pragma optimize 1 #include int one(void) {return 1;} int negative_one(void) {return -1;} int main(void) { puts((one() + negative_one()) ? "A" : "B"); } This could also occur if the condition used the % operator, particularly after the recent changes to it. Also, add unsigned multiplication, division, and modulo operations to the list of those that may not set the condition codes based on the result value, both in this and other contexts. Detected based on several programs from FizzBuzz-C. --- Gen.pas | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Gen.pas b/Gen.pas index 5ddbed6..c5ee45f 100644 --- a/Gen.pas +++ b/Gen.pas @@ -404,7 +404,8 @@ begin {NeedsCondition} NeedsCondition := opcode in [pc_and,pc_ior,pc_cui,pc_cup,pc_lor,pc_lnd,pc_ldl,pc_lil,pc_lld, pc_lli,pc_gil,pc_gli,pc_gdl,pc_gld,pc_iil,pc_ili,pc_idl,pc_ild, - pc_cop,pc_cpo,pc_cpi,pc_dvi,pc_mpi,pc_adi,pc_sbi,pc_mod,pc_bno]; + pc_cop,pc_cpo,pc_cpi,pc_dvi,pc_mpi,pc_adi,pc_sbi,pc_mod,pc_bno, + pc_udi,pc_uim,pc_umi]; end; {NeedsCondition} @@ -5448,9 +5449,7 @@ procedure GenTree {op: icptr}; lab2 := GenLabel; lab3 := GenLabel; GenTree(op^.left); - if op^.left^.opcode in - [pc_and,pc_ior,pc_cui,pc_cup,pc_lor,pc_lnd,pc_ldl,pc_lil,pc_lld, - pc_lli,pc_gil,pc_gli,pc_gdl,pc_gld] then + if NeedsCondition(op^.left^.opcode) then GenImplied(m_tax); GenNative(m_beq, relative, lab1, nil, 0); GenNative(m_brl, longrelative, lab2, nil, 0);