public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rs2740 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/65352] New: array<T,0>::begin()/end() etc. forms a null reference and breaks on clang+ubsan
Date: Sun, 08 Mar 2015 17:43:00 -0000	[thread overview]
Message-ID: <bug-65352-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 65352
           Summary: array<T,0>::begin()/end() etc. forms a null reference
                    and breaks on clang+ubsan
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rs2740 at gmail dot com

Repro:

#include <array>
int main(){
    std::array<int, 0> foo;
    foo.begin(); // or end(), etc.
}

Output (http://coliru.stacked-crooked.com/a/e1cbe7e73bcee449):

> clang++ --version
clang version 3.5.0 (tags/RELEASE_350/final 217394)
Target: x86_64-unknown-linux-gnu
Thread model: posix
> clang++ -std=c++11 -O0 -Wall -pedantic -pthread main.cpp -fsanitize=undefined
> ./a.out
==15356==WARNING: readlink("/proc/self/exe") failed with errno 2, some stack
frames may not be symbolized
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/array:63:15:
runtime error: reference binding to null pointer of type 'int'
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/array:222:33:
runtime error: reference binding to null pointer of type 'int'

begin()/end() are supposed to be well-defined even if N = 0. The implementation
in <array> defers to data(), which in turn returns
std::__addressof(_AT_Type::_S_ref(_M_elems, 0)). The problem is that for the N
= 0 case, __array_traits::_S_ref forms and returns a null reference:

     static constexpr _Tp&
     _S_ref(const _Type&, std::size_t) noexcept
     { return *static_cast<_Tp*>(nullptr); }

An obvious possible fix is to provide a pointer-returning helper in addition to
or instead of _S_ref.


             reply	other threads:[~2015-03-08 17:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-08 17:43 rs2740 at gmail dot com [this message]
2015-03-08 18:09 ` [Bug libstdc++/65352] " redi at gcc dot gnu.org
2015-03-08 18:13 ` redi at gcc dot gnu.org
2015-03-13  3:15 ` rs2740 at gmail dot com
2015-05-28  9:43 ` redi at gcc dot gnu.org
2015-05-28 12:34 ` redi at gcc dot gnu.org
2015-05-28 16:00 ` redi at gcc dot gnu.org
2015-05-28 16:28 ` redi at gcc dot gnu.org
2015-05-28 16:32 ` redi 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-65352-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).