[Bug middle-end/84433] gcc 7 and before miscompile loop and remove exit due to incorrect range calculation

marxin at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Feb 19 14:27:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84433

--- Comment #10 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to acsawdey from comment #8)
> It looks like both gcc 7 and 8 assume that the statement 
> 
>   ptrA->sA[ptrB->int1].zt = parm1;
> 
> will only be executed 14+1 times because of the declaration sA[15].
> 
> However gcc 7 assumes the whole loop will only execute that number of times:
> 
> Statement ptrA_14(D)->sA[ptrB__int1_lsm.11_22].zt = _34;
>  is executed at most 14 (bounded by 14) + 1 times in loop 1.
> Analyzing # of iterations of loop 1
>   exit condition [15, + , 4294967295] != 0
>   bounds on difference of bases: -15 ... -15
>   result:
>     # of iterations 15, bounded by 15
> Loop 1 iterates 15 times.
> Loop 1 iterates at most 14 times.
> Loop 1 likely iterates at most 14 times.
> Analyzing # of iterations of loop 1
>   exit condition [15, + , 4294967295] != 0
>   bounds on difference of bases: -15 ... -15
>   result:
>     # of iterations 15, bounded by 15
> Removed pointless exit: if (ivtmp_24 != 0)
> 
> were gcc8 does not:
> 
> Statement ptrA_13(D)->sA[ptrB__int1_lsm.5_22].zt = _20;
>  is executed at most 14 (bounded by 14) + 1 times in loop 1.
> Analyzing # of iterations of loop 1
>   exit condition [15, + , 4294967295] != 0
>   bounds on difference of bases: -15 ... -15
>   result:
>     # of iterations 15, bounded by 15
> Loop 1 iterates 15 times.
> Loop 1 iterates at most 15 times.
> Loop 1 likely iterates at most 15 times.

GCC makes an assumption that the loop does iterate 'at most' 15 times.
The issue happens in runtime, compiler doesn't prove that in compile time.
For situations like this one can use UBSAN or ASAN sanitizers.

> 
> Neither gcc 7 nor 8 produce any warnings for the revised test case with
> -Wall.


More information about the Gcc-bugs mailing list