From patchwork Wed Oct 30 19:49:03 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 287332 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 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2CB7A2C03CF for ; Thu, 31 Oct 2013 06:49:15 +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 :mime-version:date:message-id:subject:from:to:content-type; q= dns; s=default; b=k+TiOamn1QzUDSA3Gie8Q9YMUWay1B83ubMxROB939veYb Z31hroLpHlYKXm3LxlbmB2FMLpkn3tpJ/5N5U9nT8m9U048gzBsKWS2bYi+8mkh1 +YI3IUswMgt6AfhV2kfJUkFzLdpVOUlqoM26bjAbibLKz98lHSLEoMaBHL9o0= 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 :mime-version:date:message-id:subject:from:to:content-type; s= default; bh=1McsEJkLj/gV4+YPqoZvl8EyS1E=; b=uIXoWApnIINevfrUo3go 2Z/3OaJbg1yY5MLYM+E89c0ziFbppTzQ7AAOKmSTdl/yhY9Wnvmn4x50kzDvhnsD H9MybuluqQzG6P5LRxRpPVFoLlBa6S3VSQrkN51TkYG2283843a6Qf3I3hkCcPA9 nlEgnxl87BRE5uzSh2Ueeyc= Received: (qmail 11444 invoked by alias); 30 Oct 2013 19:49:09 -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 11423 invoked by uid 89); 30 Oct 2013 19:49:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-lb0-f176.google.com Received: from mail-lb0-f176.google.com (HELO mail-lb0-f176.google.com) (209.85.217.176) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 30 Oct 2013 19:49:07 +0000 Received: by mail-lb0-f176.google.com with SMTP id z5so1628479lbh.35 for ; Wed, 30 Oct 2013 12:49:03 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.112.235.3 with SMTP id ui3mr103034lbc.44.1383162543248; Wed, 30 Oct 2013 12:49:03 -0700 (PDT) Received: by 10.112.101.7 with HTTP; Wed, 30 Oct 2013 12:49:03 -0700 (PDT) Date: Wed, 30 Oct 2013 19:49:03 +0000 Message-ID: Subject: [v3 patch] Extend smart ptr assertions to reject void* From: Jonathan Wakely To: "libstdc++" , gcc-patches Because of the GNU extension that allows sizeof(void) we fail to reject ill-formed programs. This patch fixes that. 2013-10-30 Jonathan Wakely * include/bits/shared_ptr (__shared_ptr): Assert non-void pointer. * include/bits/shared_ptr (default_delete): Likewise. * include/backward/auto_ptr.h (__shared_ptr(auto_ptr&&)): Likewise. * testsuite/20_util/shared_ptr/cons/58839.cc: Do not use default_delete. * testsuite/20_util/shared_ptr/cons/void_neg.cc: New. * testsuite/20_util/default_delete/void_neg.cc: New. * testsuite/20_util/shared_ptr/cons/43820_neg.cc: Adjust line numbers. * testsuite/20_util/unique_ptr/assign/48635_neg.cc: Likewise. Tested x86_64-linux, committed to trunk. commit 5b0ffcc57b7126016eda484e56eda3eba0a0ec90 Author: Jonathan Wakely Date: Wed Oct 30 17:50:39 2013 +0000 * include/bits/shared_ptr (__shared_ptr): Assert non-void pointer. * include/bits/shared_ptr (default_delete): Likewise. * testsuite/20_util/shared_ptr/cons/58839.cc: Do not use default_delete. * testsuite/20_util/shared_ptr/cons/void_neg.cc: New. * testsuite/20_util/default_delete/void_neg.cc: New. * testsuite/20_util/shared_ptr/cons/43820_neg.cc: Adjust line numbers. * testsuite/20_util/unique_ptr/assign/48635_neg.cc: Likewise. diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index 91b6367..cf90d7a 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -775,6 +775,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _M_ptr(__p), _M_refcount(__p) { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) + static_assert( !is_void<_Tp>::value, "incomplete type" ); static_assert( sizeof(_Tp1) > 0, "incomplete type" ); __enable_shared_from_this_helper(_M_refcount, __p, __p); } diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index c6c9a5a..bfe40ec 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -69,6 +69,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void operator()(_Tp* __ptr) const { + static_assert(!is_void<_Tp>::value, + "can't delete pointer to incomplete type"); static_assert(sizeof(_Tp)>0, "can't delete pointer to incomplete type"); delete __ptr; diff --git a/libstdc++-v3/testsuite/20_util/default_delete/void_neg.cc b/libstdc++-v3/testsuite/20_util/default_delete/void_neg.cc new file mode 100644 index 0000000..79786cb --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/default_delete/void_neg.cc @@ -0,0 +1,30 @@ +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +// Copyright (C) 2013 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 +// . + +// 20.8.1.1 Default deleters [util.ptr.dltr] + +#include + +void test01() +{ + std::default_delete d; + d(nullptr); // { dg-error "here" } + // { dg-error "incomplete" "" { target *-*-* } 72 } +} diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820_neg.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820_neg.cc index fd2a677..db3fcac 100644 --- a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820_neg.cc +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820_neg.cc @@ -32,7 +32,7 @@ void test01() { X* px = 0; std::shared_ptr p1(px); // { dg-error "here" } - // { dg-error "incomplete" "" { target *-*-* } 778 } + // { dg-error "incomplete" "" { target *-*-* } 779 } std::shared_ptr p9(ap()); // { dg-error "here" } // { dg-error "incomplete" "" { target *-*-* } 307 } diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/58839.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/58839.cc index 6ad2564..f78a07f 100644 --- a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/58839.cc +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/58839.cc @@ -22,8 +22,12 @@ // libstdc++/58839 +struct D { + void operator()(void*) const noexcept { } +}; + void test01() { - std::unique_ptr y; + std::unique_ptr y; std::shared_ptr x = std::move(y); } diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/void_neg.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/void_neg.cc new file mode 100644 index 0000000..3fd38cf --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/void_neg.cc @@ -0,0 +1,29 @@ +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +// Copyright (C) 2013 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 +// . + +// 20.8.2.2 Template class shared_ptr [util.smartptr.shared] + +#include + +void test01() +{ + std::shared_ptr p((void*)nullptr); // { dg-error "here" } + // { dg-error "incomplete" "" { target *-*-* } 778 } +} diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/assign/48635_neg.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/48635_neg.cc index c785642..fdffafc 100644 --- a/libstdc++-v3/testsuite/20_util/unique_ptr/assign/48635_neg.cc +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/48635_neg.cc @@ -41,10 +41,10 @@ void f() std::unique_ptr ub(nullptr, b); std::unique_ptr ud(nullptr, d); ub = std::move(ud); -// { dg-error "use of deleted function" "" { target *-*-* } 270 } +// { dg-error "use of deleted function" "" { target *-*-* } 272 } std::unique_ptr uba(nullptr, b); std::unique_ptr uda(nullptr, d); uba = std::move(uda); -// { dg-error "use of deleted function" "" { target *-*-* } 515 } +// { dg-error "use of deleted function" "" { target *-*-* } 517 } }