From patchwork Mon Jan 27 19:20:05 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 314473 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 91B622C0097 for ; Tue, 28 Jan 2014 07:11:40 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=Tv1579uF3IUkm+h7eob0mZR8NOA/+UqdbbWyPy/Mlp2Da3gLw7UJ9 Lx3cqeGZJ4VBzHyI7t39HjC/fA6D1iPuGaz137HBddKFXOtTePYHFQA3D2KBM7og cP892y7c0EC6kKqA4AbKByDPvwqdp14VBIjDcTIUZlG/f5X6tBfsIw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=V5NMaiv91Tbiml38j9MnoldK9N8=; b=DMc5Zcie0km1+BUoUc0b x8gh8GRyUcghI85onLsBRMiqsiQy9AIWMx81zFbJI1N6lvao/S0BBvbekvw8d+5/ CrieczQiG/+YCYH+3a3QPIHsJzY7dVSxsIox91g02pt9/9zU2JcXUd4ngVRdj32Z VkXhcLtvYbjyLH0c1KZdXB0= Received: (qmail 24087 invoked by alias); 27 Jan 2014 20:11:23 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 24064 invoked by uid 89); 27 Jan 2014 20:11:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 27 Jan 2014 20:11:20 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s0RKBH8Y029705 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 27 Jan 2014 15:11:18 -0500 Received: from localhost (vpn1-5-120.ams2.redhat.com [10.36.5.120]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s0RJK5Dw015152; Mon, 27 Jan 2014 14:20:06 -0500 Date: Mon, 27 Jan 2014 19:20:05 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [patch] proposed fix for libstdc++/59829 Message-ID: <20140127192005.GA30558@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) This is the best I've come up with to avoid undefined behaviour when calling vector::data() on an empty vector. In C++03 mode we return vector::pointer, so can just return the internal pointer. In C++11 mode we need to dereference it, but only when it's valid. Any suggestions for improvements before I commit it? (Tests pass, but some dg-error line numbers need adjusting) 2014-01-27 Jonathan Wakely PR libstdc++/59829 * include/bits/stl_vector.h (vector::data()): Do not dereference pointers when empty. commit 60f9bb3b06ea95054d3fb442c9c212e7afe66acc Author: Jonathan Wakely Date: Mon Jan 27 18:35:17 2014 +0000 PR libstdc++/59829 * include/bits/stl_vector.h (vector::data()): Do not dereference pointers when empty. * testsuite/23_containers/vector/59829.cc: New. diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index f482957..8393242 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -880,19 +880,21 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ #if __cplusplus >= 201103L _Tp* -#else - pointer -#endif data() _GLIBCXX_NOEXCEPT - { return std::__addressof(front()); } + { return empty() ? nullptr : std::__addressof(*this->_M_impl._M_start); } -#if __cplusplus >= 201103L const _Tp* + data() const _GLIBCXX_NOEXCEPT + { return empty() ? nullptr : std::__addressof(*this->_M_impl._M_start); } #else + pointer + data() _GLIBCXX_NOEXCEPT + { return this->_M_impl._M_start; } + const_pointer -#endif data() const _GLIBCXX_NOEXCEPT - { return std::__addressof(front()); } + { return this->_M_impl._M_start; } +#endif // [23.2.4.3] modifiers /** diff --git a/libstdc++-v3/testsuite/23_containers/vector/59829.cc b/libstdc++-v3/testsuite/23_containers/vector/59829.cc new file mode 100644 index 0000000..8d9f39d --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/59829.cc @@ -0,0 +1,108 @@ +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++11" } + +// libstdc++/59829 + +#include + +// User-defined pointer type that throws if a null pointer is dereferenced. +template +struct Pointer +{ + typedef T element_type; + + // typedefs for iterator_traits + typedef T value_type; + typedef std::ptrdiff_t difference_type; + typedef std::random_access_iterator_tag iterator_category; + typedef Pointer pointer; + typedef T& reference; + + T* value; + + explicit Pointer(T* p = nullptr) : value(p) { } + + template())> + Pointer(const Pointer& p) : value(p.value) { } + + T& operator*() const + { + if (!value) + throw nullptr; + return *value; + } + + T* operator->() const { return value; } + + Pointer& operator++() { ++value; return *this; } + Pointer operator++(int) { Pointer tmp(*this); ++value; return tmp; } + Pointer& operator--() { --value; return *this; } + Pointer operator--(int) { Pointer tmp(*this); --value; return tmp; } + + Pointer& operator+=(difference_type n) { value += n; return *this; } + Pointer& operator-=(difference_type n) { value -= n; return *this; } + + explicit operator bool() const { return value != nullptr; } + + Pointer operator+(difference_type n) { Pointer p(*this); return p += n; } + Pointer operator-(difference_type n) { Pointer p(*this); return p -= n; } +}; + +template +std::ptrdiff_t operator-(Pointer l, Pointer r) +{ return l.value - r.value; } + +template +bool operator==(Pointer l, Pointer r) +{ return l.value == r.value; } + +template +bool operator!=(Pointer l, Pointer r) +{ return l.value != r.value; } + +template +struct Alloc +{ + typedef T value_type; + typedef Pointer pointer; + + Alloc() = default; + template + Alloc(const Alloc&) { } + + pointer allocate(std::size_t n) + { return pointer(std::allocator().allocate(n)); } + + void deallocate(pointer p, std::size_t n) + { std::allocator().deallocate(p.value, n); } +}; + +template +bool operator==(Alloc l, Alloc r) +{ return true; } + +template +bool operator!=(Alloc l, Alloc r) +{ return false; } + +int main() +{ + std::vector> a; + a.data(); +}