From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by sourceware.org (Postfix) with ESMTPS id 255CD385801D; Wed, 22 Sep 2021 17:49:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 255CD385801D Received: by mail-qt1-x82b.google.com with SMTP id j13so3495330qtq.6; Wed, 22 Sep 2021 10:49:13 -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:content-transfer-encoding; bh=iV7J6zzgwjGotiUdpUKIcltWIqP+3WTiiV+MpcRTXJg=; b=Z5xkYOD3vK9pp6GjMv6kK+YE+jqfWhMN3V97WPlLDHO601byGMcVkfg65aItLJq7Lp Hzj0zxcK7YOql2oP9/x6FtngDOCRpZcRk3NPyFFrid8ogLEfw+5TM+zL/AOE+8MvoP+J M2nVm9en8Ril9rkQfA/eJpzROAsCAwWZePe1Iy5KasLh3O2x3VJWtzvvwP4d/wf3D0GD ueJOaMKldI73vwcZ6XGukbdMAhZHLAk/EdrQnbXnWR0uRxPiY54GNIzFq7yA7WvelCXK 9m72TiIfaOwK+dsL9hpBQGIn8WU1Xe1DqdxLfYFw7FTnylgTRQo3mBxoFyzFDw+vTbBj tWKw== X-Gm-Message-State: AOAM5306geNVXPrJItimQFV5KUfQfTXjwVFNBzMlbARlXm9Wbf20ZIlk votVlQJaCa1+rw08+aiNW+tpLJdj6eFVd+Xs0cEdOD66oUI= X-Google-Smtp-Source: ABdhPJx2+D9vGbtx+zG3zrFBz+SI8X4X4YBQQbH/05gCBFjdtnn17Ay8CNX1tCTIWs2PluxGhh+hcuOMF2K2jw7oArY= X-Received: by 2002:ac8:6e8f:: with SMTP id c15mr470703qtv.179.1632332952734; Wed, 22 Sep 2021 10:49:12 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Antony Polukhin Date: Wed, 22 Sep 2021 20:49:00 +0300 Message-ID: Subject: Re: [PATCH] assert that deleting by pointer to base in unique_ptr does not cause UB To: Ville Voutilainen Cc: "libstdc++" , gcc-patches List Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00, BODY_8BITS, 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:49:14 -0000 =D1=81=D1=80, 22 =D1=81=D0=B5=D0=BD=D1=82. 2021 =D0=B3. =D0=B2 20:23, Ville= Voutilainen : > > On Wed, 22 Sept 2021 at 20:09, Antony Polukhin via Libstdc++ > 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. > > I don't understand the sizeof(_Tp) =3D=3D sizeof(_Up) part in the > static_assert. I fail to see how > a same-size check suggests that the types are similar enough that a > delete-expression works. I used the following logic: [unique.ptr.single.*] sections have the constraint that "unique_=C2=ADptr::pointer is implicitly convertible to pointer". There's already a static assert that T in unique_ptr is not void, so U either has to be the same type T, or a type derived from T. If a derived type adds members, then size changes and types are not similar as the decompositions won't have the qualification-decompositions with the same n. --=20 Best regards, Antony Polukhin