public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "spamandnoise at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-100137-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 100137
           Summary: -Werror=array-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=c++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=array-bounds]
   38 |     s.back() = '2';
      |     ~~~~~~^~
test.cpp:36:10: note: while referencing 'hello'
   36 |     char hello[] = "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 <stddef.h>

struct span
{
    span( char* _data, size_t _size )
    : first_( _data ),
      last_( _data != nullptr ? _data + _size : nullptr )
    {
        if ( _size != 0 && _data == 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 = (size_t) - 1 )
{
    size_t len = 0;
    while ( len < max && ptr[len] )
    {
        ++len;
    }
    return len;
}

int
main()
{
    char hello[] = "hello";
    span s{ hello, string_length( hello ) };
    s.back() = '2';
}
-----

`g++ -v` output:

-----
Using built-in specs.
COLLECT_GCC=/opt/compiler-explorer/gcc-snapshot/bin/g++
Target: x86_64-linux-gnu
Configured with: ../gcc-trunk-20210418/configure
--prefix=/opt/compiler-explorer/gcc-build/staging --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-bootstrap
--enable-multiarch --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --enable-clocale=gnu --enable-languages=c,c++,fortran,ada,d
--enable-ld=yes --enable-gold=yes --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto
--enable-plugins --enable-threads=posix
--with-pkgversion=Compiler-Explorer-Build
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.1 20210417 (experimental) (Compiler-Explorer-Build) 
COLLECT_GCC_OPTIONS='-fdiagnostics-color=always' '-g' '-o' '/app/output.s'
'-masm=intel' '-S' '-v' '-O2' '-Werror' '-Wall' '-std=c++17' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' '/app/'

/opt/compiler-explorer/gcc-trunk-20210418/bin/../libexec/gcc/x86_64-linux-gnu/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/11.0.1/
-D_GNU_SOURCE <source> -quiet -dumpdir /app/ -dumpbase output.cpp -dumpbase-ext
.cpp -masm=intel -mtune=generic -march=x86-64 -g -O2 -Werror -Wall -std=c++17
-version -fdiagnostics-color=always -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=30 --param ggc-min-heapsize=4096
[...]
Compiler executable checksum: c26ce8a3d2d070f1dc9f9a165aea3eaf
-----

             reply	other threads:[~2021-04-18 20:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-18 20:40 spamandnoise at gmail dot com [this message]
2021-04-18 20:42 ` [Bug c++/100137] " spamandnoise at gmail dot com
2021-04-19 15:39 ` [Bug tree-optimization/100137] [10/11 Regression] -Warray-bounds false positive on varying offset plus negative msebor at gcc dot gnu.org
2021-04-19 15:47 ` msebor at gcc dot gnu.org
2021-04-19 16:56 ` spamandnoise at gmail dot com
2021-06-21 23:35 ` [Bug tree-optimization/100137] [10/11/12 " msebor at gcc dot gnu.org
2021-07-06  6:45 ` rguenth at gcc dot gnu.org
2021-07-06  6:45 ` rguenth at gcc dot gnu.org
2021-07-07 20:14 ` cvs-commit at gcc dot gnu.org
2021-07-07 20:27 ` [Bug tree-optimization/100137] [10/11 " msebor at gcc dot gnu.org
2021-07-13 16:10 ` msebor at gcc dot gnu.org
2022-06-28 10:44 ` jakub at gcc dot gnu.org
2023-07-07 10:39 ` [Bug tree-optimization/100137] [11 " rguenth at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-100137-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).