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...
Status: RESOLVED WORKSFORME
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
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-16 13:37 UTC by Sergey Vojtovich
Modified: 2017-01-31 21:34 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

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:
http://bazaar.launchpad.net/~maria-captains/maria/10.0/view/head:/strings/decimal.c#L251

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;
  }

assembly:

remove_leading_zeroes:
        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
.L2:
        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.