[Bug c/86341] New: Aggressive loop unrolling in gcc 7.x produces out-of-bounds index

tomek.bury at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Jun 28 09:20:00 GMT 2018


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

            Bug ID: 86341
           Summary: Aggressive loop unrolling in gcc 7.x produces
                    out-of-bounds index
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tomek.bury at gmail dot com
  Target Milestone: ---

Created attachment 44331
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44331&action=edit
test case, compile with 7.x gcc -Wall -O3

Compiling the below code produces a warning in gcc 7.x series:

test.c:
---
struct test
{
    unsigned num_items;
    int items[3];
};

struct test t = {2, {1, 2}};

int oopsie(void)
{
    int foo = 0;
    for(unsigned i = 0; i < t.num_items; i++)
    {
        const int *item_i = &t.items[i];
        foo += *item_i;
        for(unsigned j = i + 1; j < t.num_items; j++)
        {
            const int *item_j = &t.items[j];
            foo += *item_j;
        }
    }
    return foo;
}
---

$ gcc -Wall -o test.o -c test.c -O3
test.c: In function ‘oopsie’:
test.c:18:41: warning: array subscript is above array bounds [-Warray-bounds]
             const int *item_j = &t.items[j];
                                  ~~~~~~~^~~

Copying the t.num_items to a local variable and using a local copy in any of
the loops makes the problem disappear. Also disabling aggressive loop unrolling
make the warning disappear. The gcc 6.x and 8.x seem to be all right.


More information about the Gcc-bugs mailing list