From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 19C013858435; Wed, 22 Sep 2021 17:44:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 19C013858435 Received: by mail-wr1-x42a.google.com with SMTP id g16so9321532wrb.3; Wed, 22 Sep 2021 10:44:38 -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:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pKKMciuue8sua8w659Y0gm2TCuStwWh60w2keF2OVGw=; b=YcXJpmZKoKtZDsC2TTxSA5zR8T1sZSNj9zwO8zjQ/DOIXC7aOKjZSZRE29YJn7b3JE YTtnqBXZx3KNewjWn9cZJLXyreR52K3EBFeQnTcvc62f59nEm0YHJ2TndGA093NHZNY4 0JbgEojtKZDpp6jwASTDc4Q8q5ZJP16q+9QhLmW4q/zUDalC28SgoufE6XKF34myXAX+ 1/MsLDjpNd42C4ZXzFum+GX9h0LKjVsB135WhG+rkRAhfjbW30NYfIKWCFqWuE54hVDh 1L7/WpDc9i+ThNlQHVW/RYFlfHCqbiC1/6z7ntJdrFZrRyBlnV6ifAluzMOEGDJdm0Rd besw== X-Gm-Message-State: AOAM533krXRlSiCay2JFgt7JLaGNqyas3Xg6rRntVgj5Qi+wveKnLAVB k6k1Fn18hPZYgKZH2H6v1lkkjIKjpMs+JRImmLk= X-Google-Smtp-Source: ABdhPJyD8jX4/dqSyx7NCEK7WqvguKuyJyOhPYZK1pEo8Hzvslw+R3Ju71EaRrHLEGfT/eh+g1aKwdMsnOVQPHa6/so= X-Received: by 2002:a5d:4481:: with SMTP id j1mr285707wrq.6.1632332676953; Wed, 22 Sep 2021 10:44:36 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Wed, 22 Sep 2021 18:44:24 +0100 Message-ID: Subject: Re: [PATCH] assert that deleting by pointer to base in unique_ptr does not cause UB To: Antony Polukhin Cc: "libstdc++" , gcc-patches List Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Sep 2021 17:44:39 -0000 On Wed, 22 Sept 2021 at 18:09, Antony Polukhin wrote: > > 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. The undefined behaviour only happens if the destructor is actually reached at runtime, but won't these static assertions make it ill-formed to instantiate these members, even if the UB never happens? For example, if you ensure that release() is called before destruction, the undefined delete never happens.