From patchwork Wed Sep 22 17:08:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antony Polukhin X-Patchwork-Id: 45317 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3FCE23858410 for ; Wed, 22 Sep 2021 17:08:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3FCE23858410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1632330525; bh=6o3QTsknRuJdIw9A3Xq88OcSgZfxptoRZLmrj36UcTU=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=XFKtv/pGNAjG6889I2NiTzahN9uaKGqO4ZZRN3ese9aVferUQW7jqupOVtznAFjiP 20VAIs46mBz94+0Dn3gWEqQkm9yCQ2Em5XjQMBnjDK9cxEprL9Wq75aJXMtkpluMlz Y3/e8/Wcibg4M4JRoT4rTehICVKl+aqWFrnlpbm4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by sourceware.org (Postfix) with ESMTPS id 167303858400; Wed, 22 Sep 2021 17:08:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 167303858400 Received: by mail-qt1-x829.google.com with SMTP id r16so3319698qtw.11; Wed, 22 Sep 2021 10:08:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=6o3QTsknRuJdIw9A3Xq88OcSgZfxptoRZLmrj36UcTU=; b=13MCa1y5LU5ei6ojUFPNzDmLWxHzMdS9mzxLFWJ8IW3HmHuL8j7ZO7wAE33WP+0zG5 BZUjTCHrxwCDRdSJS7q26KTF+jD36c/ujRNm8bMSv4JKYvhlox1PZvqvZFHiuWvtBzM1 XWzVwBnDwa0HV3aClij2COao3peuXwzPh1C8SQ5LDkPrdVg2k+4kxM96xpl1Z1D4MqgJ 9l1syJmmVYKG3ICeT5ZC5HZtW8xNh7joUU4a/HkiHADj8NjiQaVDvOdp+V8QSqhrXnr5 bnxSVj6FjqZ6V9LtkZWkLC9SIckh5akjZY32gD0Nz+4zuiPoMR+n9Fy8RC1kfm5yJUT7 6rhg== X-Gm-Message-State: AOAM532qpRaWm9jD6ZD7SM2roYBBrLvb/cFDV1Lz3ZPOYg8+OJiCHc3R MKzryq3dXMlT6/b4JSCQXZDD97OzlBIHMhEPmowEpz0t7TU= X-Google-Smtp-Source: ABdhPJyD4O//RGyuf5M1nvOk6WLGKgBFSdX4wxKePrKfzeciab5QSA11p8//c977X1VOo9z3v2Vm/r6MpCy4/5wODuw= X-Received: by 2002:ac8:6e8f:: with SMTP id c15mr254826qtv.179.1632330494527; Wed, 22 Sep 2021 10:08:14 -0700 (PDT) MIME-Version: 1.0 Date: Wed, 22 Sep 2021 20:08:03 +0300 Message-ID: Subject: [PATCH] assert that deleting by pointer to base in unique_ptr does not cause UB To: "libstdc++" , gcc-patches List X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Antony Polukhin via Gcc-patches From: Antony Polukhin Reply-To: Antony Polukhin Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" std::unique_ptr allows construction from std::unique_ptr of derived type as per [unique.ptr.single.asgn] and [unique.ptr.single.ctor]. If std::default_delete is used with std::unique_ptr, then after such construction a delete is called on a pointer to base. According to [expr.delete] calling a delete on a non similar object without a virtual destructor is an undefined behavior. This patch turns that undefined behavior into static assertions inside std::unique_ptr. Changelog: * include/bits/unique_ptr.h: Add static asserts that deleting by pointer to base in unique_ptr does not cause UB * testsuite/20_util/unique_ptr/assign/slicing_neg.cc: New test. diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index 6e55375..53a68f5 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -339,7 +339,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION is_convertible<_Ep, _Dp>>::type>> unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) - { } + { + static_assert(!is_same<_Dp, default_delete<_Tp>>::value + || has_virtual_destructor::type>::value + || sizeof(_Tp) == sizeof(_Up), + "type of pointer owned by __u must be similar to the type of pointer " + "owned by this object or the latter must have a virtual destructor"); + } #if _GLIBCXX_USE_DEPRECATED #pragma GCC diagnostic push @@ -385,6 +391,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION unique_ptr&>::type operator=(unique_ptr<_Up, _Ep>&& __u) noexcept { + static_assert(!is_same<_Dp, default_delete<_Tp>>::value + || has_virtual_destructor::type>::value + || sizeof(_Tp) == sizeof(_Up), + "type of pointer owned by __u must be similar to the type of pointer " + "owned by this object or the latter must have a virtual destructor"); + reset(__u.release()); get_deleter() = std::forward<_Ep>(__u.get_deleter()); return *this; diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/assign/slicing_neg.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/slicing_neg.cc new file mode 100644 index 0000000..e93483a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/slicing_neg.cc @@ -0,0 +1,86 @@ +// { dg-do compile { target c++11 } } +// { dg-prune-output "virtual destructor" } + +// Copyright (C) 2021 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 +// . + +#include + +struct A { }; +struct B : A { }; +struct C : B { int i; }; + +struct Ac { char c; }; +struct Bc : Ac { }; +struct Cc : Bc { short s; }; + + +void test01() +{ + std::unique_ptr upB; + + std::unique_ptr cA; + cA = std::move(upB); + + std::unique_ptr vA; + vA = std::move(upB); + + std::unique_ptr cvA; + cvA = std::move(upB); +} + +void test02() +{ + std::unique_ptr upC; + + std::unique_ptr cA{std::move(upC)}; // { dg-error "required from here" } + cA = std::move(upC); // { dg-error "required from here" } + + std::unique_ptr vA{std::move(upC)}; // { dg-error "required from here" } + vA = std::move(upC); // { dg-error "required from here" } + + std::unique_ptr cvA{std::move(upC)}; // { dg-error "required from here" } + cvA = std::move(upC); // { dg-error "required from here" } +} + +void test03() +{ + std::unique_ptr upB; + + std::unique_ptr cA; + cA = std::move(upB); + + std::unique_ptr vA; + vA = std::move(upB); + + std::unique_ptr cvA; + cvA = std::move(upB); +} + +void test04() +{ + std::unique_ptr upC; + + std::unique_ptr cA{std::move(upC)}; // { dg-error "required from here" } + cA = std::move(upC); // { dg-error "required from here" } + + std::unique_ptr vA{std::move(upC)}; // { dg-error "required from here" } + vA = std::move(upC); // { dg-error "required from here" } + + std::unique_ptr cvA{std::move(upC)}; // { dg-error "required from here" } + cvA = std::move(upC); // { dg-error "required from here" } +}