From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 1B36F3945C1D; Sun, 18 Apr 2021 20:41:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B36F3945C1D From: "spamandnoise at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/100137] New: -Werror=array-bounds false positive:"subscript -1 is outside array bounds" Date: Sun, 18 Apr 2021 20:40:59 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: spamandnoise at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 18 Apr 2021 20:41:00 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D100137 Bug ID: 100137 Summary: -Werror=3Darray-bounds false positive:"subscript -1 is outside array bounds" Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: spamandnoise at gmail dot com Target Milestone: --- - exact version of GCC: 11.0.1 20210417 (bug first appeared in GCC 10.3) - system type: x86_64-linux-gnu - options given when GCC was configured/built: see below - complete command line that triggers the bug: `g++ -O2 -Werror -Wall -std=3Dc++17 -c test.cpp` compiler output: ----- test.cpp: In function 'int main()': test.cpp:38:11: error: array subscript -1 is outside array bounds of 'char = [6]' [-Werror=3Darray-bounds] 38 | s.back() =3D '2'; | ~~~~~~^~ test.cpp:36:10: note: while referencing 'hello' 36 | char hello[] =3D "hello"; | ^~~~~ cc1plus: all warnings being treated as errors Compiler returned: 1 ----- - preprocessed file that triggers the bug: ----- typedef long unsigned int size_t; // expanded from struct span { span( char* _data, size_t _size ) : first_( _data ), last_( _data !=3D nullptr ? _data + _size : nullptr ) { if ( _size !=3D 0 && _data =3D=3D nullptr ) throw 42; } char& back() const { //return *( first_ + ( last_ - first_ - 1 ) ); // this works return *( last_ - 1 ); } char* first_; char* last_; }; size_t string_length( char const * ptr, size_t max =3D (size_t) - 1 ) { size_t len =3D 0; while ( len < max && ptr[len] ) { ++len; } return len; } int main() { char hello[] =3D "hello"; span s{ hello, string_length( hello ) }; s.back() =3D '2'; } ----- `g++ -v` output: ----- Using built-in specs. COLLECT_GCC=3D/opt/compiler-explorer/gcc-snapshot/bin/g++ Target: x86_64-linux-gnu Configured with: ../gcc-trunk-20210418/configure --prefix=3D/opt/compiler-explorer/gcc-build/staging --build=3Dx86_64-linux-= gnu --host=3Dx86_64-linux-gnu --target=3Dx86_64-linux-gnu --disable-bootstrap --enable-multiarch --with-abi=3Dm64 --with-multilib-list=3Dm32,m64,mx32 --enable-multilib --enable-clocale=3Dgnu --enable-languages=3Dc,c++,fortran= ,ada,d --enable-ld=3Dyes --enable-gold=3Dyes --enable-libstdcxx-debug --enable-libstdcxx-time=3Dyes --enable-linker-build-id --enable-lto --enable-plugins --enable-threads=3Dposix --with-pkgversion=3DCompiler-Explorer-Build Thread model: posix Supported LTO compression algorithms: zlib gcc version 11.0.1 20210417 (experimental) (Compiler-Explorer-Build)=20 COLLECT_GCC_OPTIONS=3D'-fdiagnostics-color=3Dalways' '-g' '-o' '/app/output= .s' '-masm=3Dintel' '-S' '-v' '-O2' '-Werror' '-Wall' '-std=3Dc++17' '-shared-l= ibgcc' '-mtune=3Dgeneric' '-march=3Dx86-64' '-dumpdir' '/app/' /opt/compiler-explorer/gcc-trunk-20210418/bin/../libexec/gcc/x86_64-linux-g= nu/11.0.1/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -iprefix /opt/compiler-explorer/gcc-trunk-20210418/bin/../lib/gcc/x86_64-linux-gnu/1= 1.0.1/ -D_GNU_SOURCE -quiet -dumpdir /app/ -dumpbase output.cpp -dumpbase= -ext .cpp -masm=3Dintel -mtune=3Dgeneric -march=3Dx86-64 -g -O2 -Werror -Wall -s= td=3Dc++17 -version -fdiagnostics-color=3Dalways -o /app/output.s GNU C++17 (Compiler-Explorer-Build) version 11.0.1 20210417 (experimental) (x86_64-linux-gnu) compiled by GNU C version 7.5.0, GMP version 6.1.0, MPFR version 3.= 1.4, MPC version 1.0.3, isl version isl-0.18-GMP GGC heuristics: --param ggc-min-expand=3D30 --param ggc-min-heapsize=3D4096 [...] Compiler executable checksum: c26ce8a3d2d070f1dc9f9a165aea3eaf -----=