From patchwork Mon Jan 27 19:38:51 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 314471 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 07E222C0089 for ; Tue, 28 Jan 2014 06:39:01 +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=bugbRJMaDMOEG3VeFFnoPFgOIxlKPi1lFChQhYtDqgJ5DUPzcLwMD XNYYSaoG1Skd8qKuRDUCGs4QclKFaTfAxoKYR0DcnNykLizoHvTjS+L4RL20rkEe xxOrcfJ8iFr/EwsmupQD24F9IZbRiL+cRFAapT56F2c7u4FeX2fRuY= 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=B9dVzXVKhU2uZovInxbQJwvqMcs=; b=ROeJJ7in3O38Q68BPsdD fFqOs78CBhn3ozLmoGIkZ4/K83plXQ0Ut+VZVkqfS5m0n8M6o0REB5dz/LCIrI7d 02cj94fayqYtWRpCTK/EJChmRXCV8svfdglRY/TpZcGi0rCewBk2CyPaYHNjbYSj TfxR3n7Ikub9qLnlfXw3gEk= Received: (qmail 22989 invoked by alias); 27 Jan 2014 19:38:55 -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 22970 invoked by uid 89); 27 Jan 2014 19:38:54 -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 19:38:53 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s0RJcqDP017089 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 27 Jan 2014 14:38:52 -0500 Received: from localhost (vpn1-5-120.ams2.redhat.com [10.36.5.120]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s0RJcp1P003313; Mon, 27 Jan 2014 14:38:51 -0500 Date: Mon, 27 Jan 2014 19:38:51 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [patch] proposed fix for libstdc++/59829 Message-ID: <20140127193851.GA16809@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 dereferencing an invalid pointer when calling vector::data() on an empty vector. For C++03 we reurn the vector's pointer type, so can just return the internal pointer, but for C++11 we need to convert that to a raw pointer, which we do by dereferencing, so we must check if it's valid first. Any better suggestions before I commit this? (Tests pass, although I need to adjust some dg-error line numbers before committing it.) 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(); +}