From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by sourceware.org (Postfix) with ESMTPS id 908413858D29; Wed, 22 Sep 2021 17:53:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 908413858D29 Received: by mail-qk1-x72b.google.com with SMTP id 194so12329497qkj.11; Wed, 22 Sep 2021 10:53:28 -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=sVwTPUedy61jnakz9WdK13FxiAJHBvFb+QX/7gWOS/U=; b=xV55p/iZBlsL/CpdbxXvL9Vf65w2viPCBQiAKqR0pOeCm/B18TBSzY4BjQfA7uOrKr AmE9m97uyv1WrUUVCOCif7fyVLAPaKEsCvc9zuEKH3rOiT5R+Lw7Qe2SB63JIBkZ+2KC 9q3tO+xQv2RfkypMLlxinIve8Xl9doS6u0TvcVIVCL9uPU0hG1Fx4oLWil2YrLu+fA31 FJJzFTWUyTUOZpfukOeFFtZStSyMHXjDxHDjWRRrnDKDpFYkS57bBulNc1IREayUa/Cm ZwpAN8A1p9OpHBfHvNAmYEGJirWE8FMjgj4wR3dKlLjCMeMu//62ujwaS7XjomeskIBm O+hQ== X-Gm-Message-State: AOAM531P7gSA0IbjW3TpdBww5Tc1iZTHi3UlWyzao97AfWzHi7aiJb2u pk621yTPMS9nqCwaL990U07oMFxBZHo66Ehd3nQ= X-Google-Smtp-Source: ABdhPJx5xgLMcL346xNVHq2rswmbMnYN1ENv0r7F2BplwwEfprJdVSIgG89qW/EyWNHbRrR6jHl5T+lWeJob8+qMoEc= X-Received: by 2002:a25:9d89:: with SMTP id v9mr595304ybp.8.1632333208227; Wed, 22 Sep 2021 10:53:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ville Voutilainen Date: Wed, 22 Sep 2021 20:53:17 +0300 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" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.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:53:29 -0000 On Wed, 22 Sept 2021 at 20:49, Antony Polukhin wrote: > > =D1=81=D1=80, 22 =D1=81=D0=B5=D0=BD=D1=82. 2021 =D0=B3. =D0=B2 20:23, Vil= le 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 insid= e > > > 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. Right, but the delete-expression on a non-polymorphic type where the static type and the dynamic type are different is UB regardless of whether the derived type adds member= s.