This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/66422] New: -Warray-bounds false positive with -O3


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

            Bug ID: 66422
           Summary: -Warray-bounds false positive with -O3
           Product: gcc
           Version: 5.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gajjanagadde at gmail dot com
  Target Milestone: ---

Created attachment 35698
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35698&action=edit
test

Compiling the following code on GCC 5.1.0 produces a bogus out of bounds
warning:

-------

#include <inttypes.h>
// Simple test file to trigger this bug
// Two things noticed:
// 1. commenting out const char* bar results in no warning
// 2. Changing the "loop" in run_foo to a single, direct control
// results in no warning
// (These were done independently of each other, starting with this file)

typedef struct foo {
    uint8_t foo_size;
    int buf[4];
    const char* bar;
} foo;

const foo *get_foo(int index);

static int foo_loop(const foo *myfoo) {
    int i;
    if (myfoo->foo_size < 3)
        return 0;
    for (i = 0; i < myfoo->foo_size; i++) {
        if (myfoo->buf[i] != 1)
            return 0;
    }

    return 1;
}

static int run_foo(void) {
    int i;
    for (i = 0; i < 1; i++) {
        const foo *myfoo = get_foo(i);
        if (foo_loop(myfoo))
            return 0;
    }
    return -1;
}

// To suppress "unused run_foo" warning
typedef struct hack {
    int (*func)(void);
} hack;

hack myhack = {
    .func = run_foo,
};

----------------------
%gcc -Warray-bounds -O3 -c test.c

test.c: In function ârun_fooâ:
test.c:22:23: warning: array subscript is above array bounds [-Warray-bounds]
         if (myfoo->buf[i] != 1)

-----------------------

config:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/5.1.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc-5-20150519/configure --prefix=/usr
--libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared
--enable-threads=posix --enable-libmpx --with-system-zlib --with-isl
--enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu
--disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object
--enable-linker-build-id --enable-lto --enable-plugin
--enable-install-libiberty --with-linker-hash-style=gnu
--enable-gnu-indirect-function --disable-multilib --disable-werror
--enable-checking=release --with-default-libstdcxx-abi=c++98
Thread model: posix
gcc version 5.1.0 (GCC)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]