From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 6CDBC3858CDB for ; Thu, 20 Jul 2023 15:47:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6CDBC3858CDB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689868035; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RiJqVq3Wa9wnbpJf4I50EQSG83boxremAbs7RgFhcbA=; b=CRO+nuvQ5tJjZZXtt+U6vg67siOkrt8+BxRIYFFGVGy+1Ptx7FM/DcAwfA2z049M4T+vQZ TA+Wf2+taK5CCsQjgx/2FRPp42FOKkBxaZScCiiwzxw2DkwSj6IPkVB947f5kP9Me80i7N fzUzp95ZF1oa7TaesO+midMaKwwsXQA= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-584-aL2Iuw1sM8GIVjSTtixnsA-1; Thu, 20 Jul 2023 11:46:59 -0400 X-MC-Unique: aL2Iuw1sM8GIVjSTtixnsA-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-635ed44d2afso10315446d6.2 for ; Thu, 20 Jul 2023 08:46:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689868010; x=1690472810; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RiJqVq3Wa9wnbpJf4I50EQSG83boxremAbs7RgFhcbA=; b=K1oY+Sr9FCtWGB02HXQuKSYMrhIIqFnZf2tewArciHeqffhPr40mFHj9HeYSV1Zq82 tGVi0lJwbHci2I1ihSeaPTdvpFEOkkNTL57AcKds+NAHlqJ5PPCeWeXbPeNbil7iP5kT JsmUFR51qJSd5DpwIj8OndR3rxsiDFErVswWNiMWDyzoigjaop2U27v4bf0QT6527R3N ydc7WcU6vG3dw9DUdJjEhkZr5LHhowqlnHk6rKnUW71Z0yljRlBYmn8VobxY/u6KQW9J A2xZTCPkGcmB0pRUrnVamRyop/YHHLyF6SE+J8ZK7RhAOdRAtYPVx7FRL/1i6ws1mkcV tskw== X-Gm-Message-State: ABy/qLYd+vA8wz9MO7W7EuFC+GkGLoRyvRTe1M2DX2Ro2uQqpzTNk7kx uR7X65+k8XzuBedTx5o4uHls50Ox6rbA3mAEOMLrGkeGzRIfw8ZR6nzP9q1lCyfZ2clk1i3Z9hG dLMxlzhheqRI92wFCdA== X-Received: by 2002:a0c:f513:0:b0:636:2d83:cea6 with SMTP id j19-20020a0cf513000000b006362d83cea6mr17870493qvm.31.1689868010628; Thu, 20 Jul 2023 08:46:50 -0700 (PDT) X-Google-Smtp-Source: APBJJlF+9v9ykp/rRASlj9dQL7MapgQKZ40qjTsQoVk+0XM9VBm5VCv302hhpFEHio+CEMSyWpzv2A== X-Received: by 2002:a0c:f513:0:b0:636:2d83:cea6 with SMTP id j19-20020a0cf513000000b006362d83cea6mr17870476qvm.31.1689868010308; Thu, 20 Jul 2023 08:46:50 -0700 (PDT) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id o4-20020a0ce404000000b0063c5fdf65b4sm471474qvl.130.2023.07.20.08.46.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 20 Jul 2023 08:46:49 -0700 (PDT) Message-ID: <033dbf6e-6585-f5fc-75de-5ac7a47c8250@redhat.com> Date: Thu, 20 Jul 2023 11:46:47 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH v4 2/3] c++: Improve constexpr error for dangling local variables [PR110619] To: Nathaniel Shead , gcc-patches@gcc.gnu.org Cc: Patrick Palka References: From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 7/20/23 05:36, Nathaniel Shead wrote: > Currently, when typeck discovers that a return statement will refer to a > local variable it rewrites to return a null pointer. This causes the > error messages for using the return value in a constant expression to be > unhelpful, especially for reference return values. > > This patch removes this "optimisation". This isn't an optimization, it's for safety, removing a way for an attacker to get a handle on other data on the stack (CWE-562). But I agree that we need to preserve some element of UB for constexpr evaluation to see. Perhaps we want to move this transformation to cp_maybe_instrument_return, so it happens after maybe_save_constexpr_fundef? > Relying on this raises a warning > by default and causes UB anyway, so there should be no issue in doing > so. We also suppress additional warnings from later passes that detect > this as a dangling pointer, since we've already indicated this anyway. > > PR c++/110619 > > gcc/cp/ChangeLog: > > * semantics.cc (finish_return_stmt): Suppress dangling pointer > reporting on return statement if already reported. > * typeck.cc (check_return_expr): Don't set return expression to > zero for dangling addresses. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp1y/constexpr-lifetime5.C: Test reported message is > correct. > * g++.dg/cpp1y/constexpr-lifetime6.C: Likewise. > * g++.dg/cpp1y/constexpr-110619.C: New test. > * g++.dg/warn/Wreturn-local-addr-6.C: Remove check for return > value optimisation. > > Signed-off-by: Nathaniel Shead > --- > gcc/cp/semantics.cc | 5 ++++- > gcc/cp/typeck.cc | 5 +++-- > gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C | 10 ++++++++++ > gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C | 4 ++-- > gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C | 8 ++++---- > gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C | 3 --- > 6 files changed, 23 insertions(+), 12 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C > > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc > index 8fb47fd179e..107407de513 100644 > --- a/gcc/cp/semantics.cc > +++ b/gcc/cp/semantics.cc > @@ -1260,7 +1260,10 @@ finish_return_stmt (tree expr) > > r = build_stmt (input_location, RETURN_EXPR, expr); > if (no_warning) > - suppress_warning (r, OPT_Wreturn_type); > + { > + suppress_warning (r, OPT_Wreturn_type); > + suppress_warning (r, OPT_Wdangling_pointer_); > + } > r = maybe_cleanup_point_expr_void (r); > r = add_stmt (r); > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index 859b133a18d..47233b3b717 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -11273,8 +11273,9 @@ check_return_expr (tree retval, bool *no_warning) > else if (!processing_template_decl > && maybe_warn_about_returning_address_of_local (retval, loc) > && INDIRECT_TYPE_P (valtype)) > - retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, > - build_zero_cst (TREE_TYPE (retval))); > + /* Suppress the Wdangling-pointer warning in the return statement > + that would otherwise occur. */ > + *no_warning = true; > } > > /* A naive attempt to reduce the number of -Wdangling-reference false > diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C > new file mode 100644 > index 00000000000..cca13302238 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C > @@ -0,0 +1,10 @@ > +// { dg-do compile { target c++14 } } > +// { dg-options "-Wno-return-local-addr" } > +// PR c++/110619 > + > +constexpr auto f() { > + int i = 0; > + return &i; > +}; > + > +static_assert( f() != nullptr ); > diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C > index a4bc71d890a..ad3ef579f63 100644 > --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C > +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C > @@ -1,11 +1,11 @@ > // { dg-do compile { target c++14 } } > // { dg-options "-Wno-return-local-addr" } > > -constexpr const int& id(int x) { return x; } > +constexpr const int& id(int x) { return x; } // { dg-message "note: declared here" } > > constexpr bool test() { > const int& y = id(3); > return y == 3; > } > > -constexpr bool x = test(); // { dg-error "" } > +constexpr bool x = test(); // { dg-error "accessing object outside its lifetime" } > diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C > index f358aff4490..b81e89af79c 100644 > --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C > +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C > @@ -4,12 +4,12 @@ > struct Empty {}; > > constexpr const Empty& empty() { > - return Empty{}; > + return Empty{}; // { dg-message "note: declared here" } > } > > -constexpr const Empty& empty_parm(Empty e) { > +constexpr const Empty& empty_parm(Empty e) { // { dg-message "note: declared here" } > return e; > } > > -constexpr Empty a = empty(); // { dg-error "" } > -constexpr Empty b = empty_parm({}); // { dg-error "" } > +constexpr Empty a = empty(); // { dg-error "accessing object outside its lifetime" } > +constexpr Empty b = empty_parm({}); // { dg-error "accessing object outside its lifetime" } > diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C > index fae8b7e766f..ec8e241d83e 100644 > --- a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C > +++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C > @@ -24,6 +24,3 @@ return_addr_local_as_intref (void) > > return (const intptr_t&)a; // { dg-warning "\\\[-Wreturn-local-addr]" } */ > } > - > -/* Verify that the return value has been replaced with zero: > - { dg-final { scan-tree-dump-times "return 0;" 2 "optimized" } } */