From 7b0dda5a5e66f39daca6e8e1f2c67e674e8cf4b3 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 10 Jul 2022 22:03:30 -0500 Subject: [PATCH] Fix a flawed optimization. The optimization could turn an unsigned comparison "x <= 0xFFFF" into "x < 0". Here is an example affected by this: int main(void) { unsigned i = 1; return (i <= 0xffff); } --- DAG.pas | 5 +++-- cc.notes | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/DAG.pas b/DAG.pas index 52a71d7..95cb44e 100644 --- a/DAG.pas +++ b/DAG.pas @@ -1850,8 +1850,9 @@ case op^.opcode of {check for optimizations of this node} end; {if} end; {case} end {if} - else if (op^.opcode = pc_leq) and (op^.optype in [cgWord,cgUWord]) then - if op^.right^.q < maxint then begin + else if (op^.opcode = pc_leq) then + if ((op^.optype = cgWord) and (op^.right^.q <> maxint)) + or ((op^.optype = cgUWord) and (op^.right^.q <> -1)) then begin op^.right^.q := op^.right^.q + 1; op^.opcode := pc_les; end; {if} diff --git a/cc.notes b/cc.notes index 6711887..79ae8da 100644 --- a/cc.notes +++ b/cc.notes @@ -1865,6 +1865,8 @@ int foo(int[42]); 201. When certain expressions were used for the condition in an if statement, loop, or conditional expression, the conditional branch might be evaluated incorrectly, leading to incorrect control flow. The affected condition expressions included shifts, loads or stores of bit-fields, and ? : expressions. +202. Some comparisons against the constant 0xFFFF would give the wrong result when intermediate code peephole optimization was used. + -- Bugs from C 2.1.0 that have been fixed ----------------------------------- 1. In some situations, fread() reread the first 1K or so of the file.