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.129.124]) by sourceware.org (Postfix) with ESMTPS id 6F81F3853D10 for ; Fri, 21 Jul 2023 21:44:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F81F3853D10 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=1689975895; 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=g37wBG+Do70JpYh+n0A/ccdvQ4EgRAuSpNvq0sUrF8I=; b=dj/xa2/7dUGbPjrGu0cN1VgEYOwVdy1mnAFiht343CZIl6shwsr4qTwvxPE1tj7CCtADnV 9g8/f0gpzpAgEK5lo5WEmgVZRPY/TaO8ntOO9IJXsdkYHA+DQDEtpyta+L3L0I6FNu2k5K oEHwG4P1SJ0sfcaD2GyD1Ti8Xvy0j78= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-215-y1YOlZLpM6qj4ljRtIBikQ-1; Fri, 21 Jul 2023 17:44:54 -0400 X-MC-Unique: y1YOlZLpM6qj4ljRtIBikQ-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7659db633acso331821385a.0 for ; Fri, 21 Jul 2023 14:44:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689975893; x=1690580693; 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=g37wBG+Do70JpYh+n0A/ccdvQ4EgRAuSpNvq0sUrF8I=; b=Q0VNGqA4bal9J8lhTojPTX0O84rUekBGx/y4AkvGHjZrKpYK5ZrIoTQVJi0HOFXq6I mH0XEqBMVZEf0TjXzxMCpxHQm9qLnW03+jX6BJA15WyrrtmSVneNwFBq5+EZPXUkF+07 rWSL4TBSU4km1G9FKTJHaAi1We5UZq+yKOE8I9E+9xUvOnhiYJm8HkUWFCKDfjXAsoVx 2oNsGo03iyzMe3ZNShE9pWMQHveCDgQdEF4Vj0be5HqYttRMN8YGDUty8E3Z2kOcXNVV 94Q+iW4lGRNHnGtFgkZxv2Hi6GuLTq2ORPS2ZlFx2KopqNADzpJRhRWw0VbmNq418Srg MH8Q== X-Gm-Message-State: ABy/qLZxijb9DX0PruwyxFO1O5x9ND9TOum02bP9oAwe5bTNWxfsCpB8 kqdxbZPpXuN5oFdMxSPbVxu8LueGaEEk0JtN4ilvYOaH4UVwS0u9Flo3SPVFI3VXFMVqz+ZnC9g KdHgi3ba/wOPb42ZYvw== X-Received: by 2002:a05:620a:f11:b0:765:3d00:b445 with SMTP id v17-20020a05620a0f1100b007653d00b445mr1275775qkl.13.1689975893555; Fri, 21 Jul 2023 14:44:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlHDgHpeeY5hSRMRRJkA9qikOxi7s4EW9Cg/z7g9QYjCrWRKOBj5EgD2+47LBI7UgyGwEvW6jg== X-Received: by 2002:a05:620a:f11:b0:765:3d00:b445 with SMTP id v17-20020a05620a0f1100b007653d00b445mr1275768qkl.13.1689975893341; Fri, 21 Jul 2023 14:44:53 -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 i18-20020a05620a145200b00767d00d10e9sm1386536qkl.58.2023.07.21.14.44.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Jul 2023 14:44:52 -0700 (PDT) Message-ID: <0ae263e3-992f-f88a-aa91-081e6f279823@redhat.com> Date: Fri, 21 Jul 2023 17:44:51 -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 Cc: gcc-patches@gcc.gnu.org, Patrick Palka References: <033dbf6e-6585-f5fc-75de-5ac7a47c8250@redhat.com> 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=-6.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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/21/23 01:39, Nathaniel Shead wrote: > On Thu, Jul 20, 2023 at 11:46:47AM -0400, Jason Merrill wrote: >> 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? > > Hm, OK. I can try giving this a go. I guess I should move the entire > maybe_warn_about_returning_address_of_local function to cp-gimplify.cc > to be able to detect this? Or is there a better way of marking that a > return expression will return a reference to a local for this > transformation? (I guess I can't use whether the warning has been > surpressed or not because the warning might not be enabled at all.) You could use a TREE_LANG_FLAG, looks like none of them are used on RETURN_EXPR. > It looks like this warning is raised also by diag_return_locals in > gimple-ssa-isolate-paths, should the transformation also be made here? Looks like it already is, in warn_return_addr_local: > tree zero = build_zero_cst (TREE_TYPE (val)); > gimple_return_set_retval (return_stmt, zero); > update_stmt (return_stmt); ...but, weirdly, only with -fisolate-erroneous-paths-*, even though it isn't isolating anything. Perhaps there should be another flag for this. > I note that the otherwise very similar -Wdangling-pointer warning > doesn't do this transformation either, should that also be something I > look into fixing here? With that same flag, perhaps. I wonder if it would make sense to remove the isolate-paths handling of locals in favor of the dangling-pointer handling? I don't know either file much at all. Jason