From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28248 invoked by alias); 15 Sep 2009 10:19:16 -0000 Received: (qmail 28240 invoked by uid 22791); 15 Sep 2009 10:19:14 -0000 X-SWARE-Spam-Status: No, hits=-0.7 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_31,J_CHICKENPOX_33,J_CHICKENPOX_61 X-Spam-Check-By: sourceware.org Received: from mail01d.mail.t-online.hu (HELO mail01d.mail.t-online.hu) (84.2.42.6) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 15 Sep 2009 10:19:10 +0000 Received: from guardian.praire-chicken.com (540339F7.catv.pool.telekom.hu [84.3.57.247]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail01d.mail.t-online.hu (Postfix) with ESMTPSA id B20A27590B9 for ; Tue, 15 Sep 2009 12:15:17 +0200 (CEST) Received: from rakitadsl.fixdial.interware.hu ([213.178.97.113] helo=[192.168.1.13]) by guardian.praire-chicken.com with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1MnWpw-0003Pu-8T for gcc@gcc.gnu.org; Tue, 15 Sep 2009 14:08:44 +0200 Message-ID: <4AAF6A13.2090303@praire-chicken.com> Date: Tue, 15 Sep 2009 10:19:00 -0000 From: "Peter A. Felvegi" User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707) MIME-Version: 1.0 To: gcc@gcc.gnu.org Subject: array subscript is below array bounds : false positive? Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-DCC-mail.t-online.hu-Metrics: mail01d.mail.t-online.hu 32711; Body=1 Fuz1=1 Fuz2=1 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2009-09/txt/msg00260.txt.bz2 Hello, I've run into this strange warning when compiling w/ optimization: gcc-4.3 -O2 -Werror -Wall -c -o t.o t.c cc1: warnings being treated as errors t.c: In function ‘foo’: t.c:25: error: array subscript is below array bounds gcc-4.4 gives the same warning/error, however, gcc 4.1 and 4.2 compiles the source. gcc-4.1 -v says: Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --enable-languages=c,c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.1.3 --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --with-tune=generic --enable-checking=release x86_64-linux-gnu Thread model: posix gcc version 4.1.3 20080704 (prerelease) (Debian 4.1.2-27) gcc-4.2 -v says: Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.2.4 (Debian 4.2.4-6) gcc-4.3 -v says: Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.4-2' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.3.4 (Debian 4.3.4-2) gcc-4.4 -v says: Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.1-1' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-objc-gc --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.4.1 (Debian 4.4.1-1) t.c is : ---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8---- #define ASSERT(x) if (x) { } else { __asm__("int $0x03"); } #define SIZE 5 char hnd[SIZE]; char flg[SIZE]; char crd(); int idx(char); void set(int i, char v); #if 1 void set(int i, char v) { ASSERT(i >=0 && i < SIZE); flg[i] = v; } #endif void foo() { char c = crd(); int i = idx(0); ASSERT(i != -1); hnd[i] = c; // array subscript is below array bounds set(i, 1); } ---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8---- Suppose that idx(c) returns the position of c in an array, an the return value of -1 means that c is not in the array. The assertion checks that. The funny thing is, if I change the source a bit, the warning goes away: 1) set '#if 1' to '#if 0' so that only the prototype of set() is visible 2) comment out the ASSERT() int set() 3) comment out ASSERT() just before the marked line 4) comment out set(i, 1) just after the marked line The warning is not present under -O2. Is this warning legal? Cheers, Peter