public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/65352] New: array<T,0>::begin()/end() etc. forms a null reference and breaks on clang+ubsan
@ 2015-03-08 17:43 rs2740 at gmail dot com
  2015-03-08 18:09 ` [Bug libstdc++/65352] " redi at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: rs2740 at gmail dot com @ 2015-03-08 17:43 UTC (permalink / raw)
  To: gcc-bugs

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.


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

end of thread, other threads:[~2015-05-28 16:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-08 17:43 [Bug libstdc++/65352] New: array<T,0>::begin()/end() etc. forms a null reference and breaks on clang+ubsan rs2740 at gmail dot com
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

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).