Bug 61520 - False warning: array subscript is below array bounds (-Warray-bounds -O -ftree-vrp -funroll-loops)
Summary: False warning: array subscript is below array bounds (-Warray-bounds -O -ftre...
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.8.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2014-06-16 13:37 UTC by Sergey Vojtovich
Modified: 2017-01-31 21:34 UTC (History)
3 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Sergey Vojtovich 2014-06-16 13:37:41 UTC
gcc emits false warning for the following code:

gcc -Warray-bounds -O -ftree-vrp -funroll-loops -c dec.c -o dec.o
or gcc -Warray-bounds -O3 -c dec.c -o dec.o

static const int powers10[2 + 1]= { 1, 10, 100 };

int remove_leading_zeroes(int decimals, int var)
  decimals%= 2;
  while (var < powers10[decimals--]) /* no-op */;
  return decimals;

This is simplified code, original code is available here:

Note that original code ensures (within the same function) that array subscript won't underrun array bounds.

4.8.2 seem to be also affected, look for compile results for decimal.c: https://buildbot.askmonty.org/buildbot/builders/kvm-deb-trusty-amd64/builds/261/steps/compile/logs/stdio
Comment 1 Sergei Golubchik 2014-06-17 15:42:56 UTC
A slightly modified version:

  static const int powers10[]= { 0, 1, 10, 100 };
  int remove_leading_zeroes(unsigned int decimals, unsigned int var)
    decimals%= 2;
    while (var < powers10[decimals--]) ;
    return decimals;


        andl    $1, %edi
        movl    %edi, %edx
        leal    -1(%rdi), %eax
        cmpl    powers10(,%rdx,4), %esi
        jae     .L2
        movl    %eax, %ecx
        leal    -2(%rdi), %edx
        cmpl    powers10(,%rcx,4), %esi
        movl    %edx, %eax
        jae     .L2
        leal    -3(%rdi), %eax
        subl    $4, %edi
        cmpl    powers10(,%rdx,4), %esi
        cmovb   %edi, %eax
        rep ret
Comment 2 Jeffrey A. Law 2017-01-31 21:34:55 UTC
I went back to a gcc-4.8 based compiler, but could not reproduce this failure.  

If you can trigger the problem, don't hestitate to re-open and I'll be happy to iterate with you to help track down why it fails for you, but not me.