diff --git a/.gitattributes b/.gitattributes index 29578b736d..13767e4b8a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8132,6 +8132,7 @@ tests/webtbs/tw8434.pp svneol=native#text/plain tests/webtbs/tw8462.pp svneol=native#text/plain tests/webtbs/tw8513.pp svneol=native#text/plain tests/webtbs/tw8525.pp svneol=native#text/plain +tests/webtbs/tw8573.pp svneol=native#text/plain tests/webtbs/ub1873.pp svneol=native#text/plain tests/webtbs/ub1883.pp svneol=native#text/plain tests/webtbs/uw0555.pp svneol=native#text/plain diff --git a/compiler/nadd.pas b/compiler/nadd.pas index 34a5ce4ce3..c21014f06c 100644 --- a/compiler/nadd.pas +++ b/compiler/nadd.pas @@ -384,6 +384,52 @@ implementation exit; end; + { Add,Sub,Mul with constant 0 or 1? } + if is_constintnode(right) then + begin + if tordconstnode(right).value = 0 then + begin + case nodetype of + addn,subn: + result := left.getcopy; + muln: + result:=cordconstnode.create(0,left.resultdef,true); + end; + end + else if tordconstnode(right).value = 1 then + begin + case nodetype of + muln: + result := left.getcopy; + end; + end; + if assigned(result) then + exit; + end; + if is_constintnode(left) then + begin + if tordconstnode(left).value = 0 then + begin + case nodetype of + addn: + result := right.getcopy; + subn: + result := cunaryminusnode.create(right.getcopy); + muln: + result:=cordconstnode.create(0,right.resultdef,true); + end; + end + else if tordconstnode(left).value = 1 then + begin + case nodetype of + muln: + result := right.getcopy; + end; + end; + if assigned(result) then + exit; + end; + { both real constants ? } if (lt=realconstn) and (rt=realconstn) then begin diff --git a/tests/webtbs/tw8573.pp b/tests/webtbs/tw8573.pp new file mode 100644 index 0000000000..3fa76fc504 --- /dev/null +++ b/tests/webtbs/tw8573.pp @@ -0,0 +1,23 @@ +program overflowbug; + +{$mode objfpc}{$Q+} + +const + zero=0; + one=1; + +var + x,y,z: cardinal; + +begin + x := 0; + y := one + x; + + // the next line sets the carry flag, so a overflow error will be generated + if x>y then; + // here the overflow error will be generated. + // the addition of zero is optimized away, but the check for the carry flag + // is not removed, so it is using the result of the compile in line 17 + z := zero + y; +end. +