From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29980 invoked by alias); 8 Mar 2015 17:43:40 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 29964 invoked by uid 48); 8 Mar 2015 17:43:36 -0000 From: "rs2740 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/65352] New: array::begin()/end() etc. forms a null reference and breaks on clang+ubsan Date: Sun, 08 Mar 2015 17:43:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 4.9.2 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: rs2740 at gmail dot com X-Bugzilla-Status: UNCONFIRMED 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 Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-03/txt/msg00844.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65352 Bug ID: 65352 Summary: array::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 int main(){ std::array 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 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.