From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id AF34F385841B for ; Sat, 22 Jul 2023 05:20:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AF34F385841B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-666eec46206so2365192b3a.3 for ; Fri, 21 Jul 2023 22:20:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690003214; x=1690608014; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=t4EdDPwMoo5pbVr3qhz76YqpaeCXX9177BJpHsYgRYk=; b=LRhneYH04l1I59j590cWQh5iM69tL832llIJdpwMFKTcZnXpTkIJAhavSC2XbA/lPi AdBmyLNC8SramSQs9xvTXk4+ZKl6VJ/AJYwkTsr0G0S+CFeyrEuNeEVBcjcL93j95jiF AuzLB/rpo6o/+5Ps983RL2Ps9Or3JYp29bwHDHFWNDnHCi2lSUuSS05DeY9dzQRAEJkC FYS1/eXAMeuHPaWRbkoMV4Qcz+y5EpjdCVG5k0aEVLq9x2ACWYzeb7qFbWLZVgh2gzkV L+MbNH4Xutv00DA6yl9uEzAcyXiabYaLy1k+2bHtjKwRnZryfB4zR4XRHfWNSRsf1iIm WXmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690003214; x=1690608014; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=t4EdDPwMoo5pbVr3qhz76YqpaeCXX9177BJpHsYgRYk=; b=eHoQwZYOILmUK/L/4pj/XRmsVtXxaLoWSc3fW1zajPCut18mW8x0k2IvCkHQ4M3lVN Y/UJMEZhOpW9pf8Yl5rklejP5C/io8kQqT6xUYUEyMY7D/NwOwlw5TPMYafER7RPnM8N 8G0HeBiIll5ofh4adiz5dJx9qJYbuze8AQkr926PfgD237VcrjF2eYMdrgUBP5AMMy0a ItUSI0xVEPnbMHX8xByngu6I+SQDgkPtyatf1+To3mlmjiDUu/raTDzr6z8vFtjYSFDe Cjfuyg/Qv5XTZsVOGBhs45ur8PnpuRejXc99YNH7z0On37NNTBfFCXz032Jg3s5y38Ol O5Xw== X-Gm-Message-State: ABy/qLa7ZtofAnF/leNfT1PiyhhgMeKi6S4so/X9ufPAuPzOZozE7gfd ywXAqFE/fia4obhxaMLvy+Y= X-Google-Smtp-Source: APBJJlEK6Z6az03AYsqjzstF0k+DC56dRVV2QjHIEVaY+66S0dp7bX3ITmi9A+4zItHZSQxsjHpZyg== X-Received: by 2002:a05:6a20:4404:b0:121:e573:3680 with SMTP id ce4-20020a056a20440400b00121e5733680mr5089436pzb.62.1690003214376; Fri, 21 Jul 2023 22:20:14 -0700 (PDT) Received: from Thaum.localdomain (59-102-120-25.tpgi.com.au. [59.102.120.25]) by smtp.gmail.com with ESMTPSA id d22-20020aa78696000000b0067ab572c72fsm3780351pfo.84.2023.07.21.22.20.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jul 2023 22:20:14 -0700 (PDT) Date: Sat, 22 Jul 2023 15:20:10 +1000 From: Nathaniel Shead To: Jason Merrill Cc: gcc-patches@gcc.gnu.org, Patrick Palka Subject: Re: [PATCH v4 2/3] c++: Improve constexpr error for dangling local variables [PR110619] Message-ID: References: <033dbf6e-6585-f5fc-75de-5ac7a47c8250@redhat.com> <0ae263e3-992f-f88a-aa91-081e6f279823@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0ae263e3-992f-f88a-aa91-081e6f279823@redhat.com> X-Spam-Status: No, score=-5.6 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,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 Fri, Jul 21, 2023 at 05:44:51PM -0400, Jason Merrill wrote: > 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 see, thanks. From this I've found that my above patch isn't sufficient anyway, as compiling with -O2 causes the warning to appear twice as the suppression I did wasn't sufficient. As such I'll exclude this patch from the next revision since it's not actually necessary for the problem I was trying to solve, and I'll work on trying to solve this properly a bit later. > > 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 >