public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/44300]  New: Spurious array subscript warning
@ 2010-05-27 20:00 jmattson at vmware dot com
  2010-05-27 20:01 ` [Bug c/44300] " jmattson at vmware dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: jmattson at vmware dot com @ 2010-05-27 20:00 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 4067 bytes --]

In the following code, foo() properly guards the expression 'p[-1]' with a test
that p is a pointer to an element of a[] other than the first element.  Yet,
when gcc analyzes array subscripts, it raises a warning because foo() is used
in a context where p points to the first element of b[].

Even if the compiler isn't sophisticated enough to optimize away the entire
body of foo(), it should have enough information to determine that 'p[-1]' is
valid in the context where it is used.  Instead, it complains:

x.c: In function ‘bar’:
x.c:7: warning: array subscript is below array bounds

----------------------------------------
int a[10], b[10];

static inline void
foo(int *p)
{
   if (p > a && p < a + 10) {
      p[-1] = 0;
   }
}

void
bar(void)
{
   foo(b);
}
----------------------------------------

% gcc -v -save-temps -c -O2 -Wall x.c
Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,java,ada
--enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.4
--enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/
--with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap
--with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--enable-version-specific-runtime-libs --program-suffix=-4.4
--enable-linux-futex --without-system-libunwind --with-arch-32=i586
--with-tune=generic --build=x86_64-suse-linux
Thread model: posix
gcc version 4.4.1 [gcc-4_4-branch revision 150839] (SUSE Linux) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-O2' '-Wall' '-mtune=generic'
 /usr/lib64/gcc/x86_64-suse-linux/4.4/cc1 -E -quiet -v x.c -mtune=generic -Wall
-O2 -fpch-preprocess -o x.i
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib64/gcc/x86_64-suse-linux/4.4/include
 /usr/lib64/gcc/x86_64-suse-linux/4.4/include-fixed
 /usr/lib64/gcc/x86_64-suse-linux/4.4/../../../../x86_64-suse-linux/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-O2' '-Wall' '-mtune=generic'
 /usr/lib64/gcc/x86_64-suse-linux/4.4/cc1 -fpreprocessed x.i -quiet -dumpbase
x.c -mtune=generic -auxbase x -O2 -Wall -version -o x.s
GNU C (SUSE Linux) version 4.4.1 [gcc-4_4-branch revision 150839]
(x86_64-suse-linux)
        compiled by GNU C version 4.4.1 [gcc-4_4-branch revision 150839], GMP
version 4.3.1, MPFR version 2.4.1-p5.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 7c58d9f1f1af203b4391f1c94895405a
x.c: In function ‘bar’:
x.c:7: warning: array subscript is below array bounds
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-O2' '-Wall' '-mtune=generic'
 /usr/lib64/gcc/x86_64-suse-linux/4.4/../../../../x86_64-suse-linux/bin/as -V
-Qy -o x.o x.s
GNU assembler version 2.19.51 (x86_64-suse-linux) using BFD version (GNU
Binutils; openSUSE 11.2) 2.19.51.20090527-10.26.4
COMPILER_PATH=/usr/lib64/gcc/x86_64-suse-linux/4.4/:/usr/lib64/gcc/x86_64-suse-linux/4.4/:/usr/lib64/gcc/x86_64-suse-linux/:/usr/lib64/gcc/x86_64-suse-linux/4.4/:/usr/lib64/gcc/x86_64-suse-linux/:/usr/lib64/gcc/x86_64-suse-linux/4.4/../../../../x86_64-suse-linux/bin/
LIBRARY_PATH=/usr/lib64/gcc/x86_64-suse-linux/4.4/:/usr/lib64/gcc/x86_64-suse-linux/4.4/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib64/gcc/x86_64-suse-linux/4.4/../../../../x86_64-suse-linux/lib/:/usr/lib64/gcc/x86_64-suse-linux/4.4/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-O2' '-Wall' '-mtune=generic'


-- 
           Summary: Spurious array subscript warning
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jmattson at vmware dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44300


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-05-29  0:34 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-27 20:00 [Bug c/44300] New: Spurious array subscript warning jmattson at vmware dot com
2010-05-27 20:01 ` [Bug c/44300] " jmattson at vmware dot com
2010-05-27 20:04 ` pinskia at gcc dot gnu dot org
2010-05-27 20:31 ` jmattson at vmware dot com
2010-05-28 10:30 ` rguenth at gcc dot gnu dot org
2010-05-28 14:39 ` jmattson at vmware dot com
2010-05-28 14:44 ` rguenth at gcc dot gnu dot org
2010-05-28 15:55 ` jmattson at vmware dot com
2010-05-28 16:40 ` rguenth at gcc dot gnu dot org
2010-05-28 16:53 ` jmattson at vmware dot com
2010-05-28 17:52 ` [Bug middle-end/44300] Spurious array subscript warning, &b[0] == &a[1] is not folded rguenth at gcc dot gnu dot org
2010-05-29  0:34 ` segher at kernel dot crashing dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).