From patchwork Sun Dec 17 21:51:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 180076 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp901490dyi; Sun, 17 Dec 2023 13:51:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEb+qnQYs9WJwISVvHdvAUOI8MC1+rwOxOqD862tJ4CF1e9vGShrrMBnKft80Wg2fhuxF7r X-Received: by 2002:a05:620a:55b3:b0:77f:4e6e:f533 with SMTP id vr19-20020a05620a55b300b0077f4e6ef533mr17966645qkn.27.1702849912210; Sun, 17 Dec 2023 13:51:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702849912; cv=pass; d=google.com; s=arc-20160816; b=KZdooJPWgrk4uVEuGWR7ktF2L8tXgeWrsRSGlbYESM2XzYypVy3s0BxfxBNbM+o8WE idvym4ESS9dW2TwfV20RhJW556JbxFxIlKylJjlwjy5lVi01hzGbKSCgSAeMRsHhp2SM Knn4sD2Qoozq4OjXSwdDTVgVHrttZWJbuI/N8sD3qJqY4bdNjdreQq0zplz8mQ81eMTm FJvJhtQIQd6U6M/oLQ4IA631CdSSwodAmw7vG8NwYJ16ZmOIUJdOuIvN1i8O3KgjVOXH Lcgt5eUgGS/cDw4m7I3ZLdrhhJij+ZJkQvjjWsxErAV8rWaZUjQ8WU7JCs0w4mqeGKtP kzFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:subject:cc:to:from:date:message-id:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=eQpnf8bylhFo7q+8iIlAAcbLf+TEAdc9WqgwRyD6vsI=; fh=Px0ai+PRlAML1LDC3BukPvCf8nEgqik/SD+oF2SuaP0=; b=dAHkcwvpafJE8S48nVjTMSm5wJDDWgUpn4Wk/8EdaHOZ8bfaD1vcLcpEB0mXCMElZ7 88mvLgpE6H0lbMgGiizRMOPJiG+BAQiNyS4ya+d+t0islM305hjDiaH6OeME18jwvxrh 4tUwt78WTMNqFHLd4JGgosjKdeLLW+3fhvpxlrLHydVhbaTCtipBlbJ95jfmpZILJVT5 cde/obRGWysSYVPuJga3fn7ia0VmDbwpZqzfY52QVnswlrbSB0YLTMsXgEYwjBJIP6fk gshX4UoNtTKRCSNW/wF1LL+nneM3dlFMhGHKntB6g9GkvZxBvzNIIs8PHBy648cKHu6e 7G2w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Wy1XQdYc; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j28-20020a05620a001c00b0077d8655902dsi21788552qki.726.2023.12.17.13.51.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 13:51:52 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Wy1XQdYc; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E9C53385803D for ; Sun, 17 Dec 2023 21:51:51 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oa1-x2a.google.com (mail-oa1-x2a.google.com [IPv6:2001:4860:4864:20::2a]) by sourceware.org (Postfix) with ESMTPS id 378A3385842F for ; Sun, 17 Dec 2023 21:51:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 378A3385842F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 378A3385842F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::2a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702849890; cv=none; b=UZfGBhNHOyMIkrmvkZVeG7+qYTeAYKNxdfmBHGtNtodC76sb6+Hj0sYtPNmp3B2njw+5F3ymbbml5faXAAuGkwmVueVnom8i1GKEfwrqaCHCZj5H0Z8/mi0//sn06gHU4usxxoIg5/dKpSAO/RqeFkB0iVznP5QtE1Zty/gEd3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702849890; c=relaxed/simple; bh=+Nv8hMmQCHbbpbhj8Xe7CvrlpcFTHYNT1O67/yIxshU=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=wOIR70Xd3LFhZwa7iuEuR1YJ4K50dRP6bJGUuNWizLX4hq/qnPBPDBm7qFUwMahvLqeww2oVPNxbT6cZXbA4Ky7aHeGuFK4QTLFgqlRg021CulMzI4kxt3Mp526/BG4k2ddj8TcGByiwqKfDK5WeXPOqHEdJ/mkNlTOBWpBmZXA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-203bfb4c35bso370168fac.3 for ; Sun, 17 Dec 2023 13:51:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702849877; x=1703454677; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=eQpnf8bylhFo7q+8iIlAAcbLf+TEAdc9WqgwRyD6vsI=; b=Wy1XQdYc5VSvdluHfujvEOJoadXoq1Osg1Km6pc6VC40LBhvoLv2+TDWrYLLRROdBA 4nO2WpxqHK9p5ZLH6CSoyh1HlwEHFtGG/IJUo9q6r9Rs3k75YKcdlrK3cr+Nk2vAVqVs +S+hVDJ2jrjkaqpzDFFB6NBkgsUn0Lw1dCbFh7z4+yG+umOr9FtJPE3UpH4TYczhBtfU j6Rq2ri9CEofun6VmUvkOfZcWwhpzVNXwg6n257zPkj4+KXPNFgKoOxKUt1hjPbLkzrE S8V52nSN5kHc8s1CfOuwy4EmMPgH+oclgJun8xTeYynzJqd20TwuYR/m+OjDQX8S9jiC hlZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702849877; x=1703454677; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eQpnf8bylhFo7q+8iIlAAcbLf+TEAdc9WqgwRyD6vsI=; b=WBh6QfEANL+fkcDuOzibvxNtS48XoAAfz9BXgrgUolTkKo43UCQK7mg92luBYlqol4 u2KkbaepSOS2LFLE1n29dL4cCwQTFSo6JtBigCObnYv0lh+MMwIfAZ4Raxp1OLbxbvBz o8J4z7u40zPi505ZfxayptJGYtcDHTcxW+YUNjktA51nGQhix/4bfO0SCJsfJ89WNgWl X3EtGOn8fsfZ44bj2v42Y0LjZJB+oKp37CdtYoTDihHCRWGrBwNL+DZhXWY2NBfuUAAI nfqV4Rq8Uuh19705GXEMA9NcZhtFhnR/35K1d8pQX8MD+6ZqU2dejo+CFsVahflgubF1 NWLQ== X-Gm-Message-State: AOJu0Yzc4FeYv3lK/ikTAYob4WUzeC70RDaIYyouqZ0DYuHW3I6s5jll nrnU9WDXKpN7FbsYqcHVhF2c7G1otlc= X-Received: by 2002:a05:6358:63a8:b0:172:da29:a8c3 with SMTP id k40-20020a05635863a800b00172da29a8c3mr1042810rwh.63.1702849877004; Sun, 17 Dec 2023 13:51:17 -0800 (PST) Received: from Thaum. ([203.166.236.30]) by smtp.gmail.com with ESMTPSA id mj13-20020a17090b368d00b0028b70755c73sm1401351pjb.41.2023.12.17.13.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 13:51:16 -0800 (PST) Message-ID: <657f6d54.170a0220.7e557.2d05@mx.google.com> X-Google-Original-Message-ID: Date: Mon, 18 Dec 2023 08:51:11 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH] c++: Check null pointer deref when calling memfn in constexpr [PR102420] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785567549344884013 X-GMAIL-MSGID: 1785567549344884013 Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? An alternative approach for the lambda issue would be to modify 'maybe_add_lambda_conv_op' to not pass a null pointer, but I wasn't sure what the best approach for that would be. -- >8 -- Calling a non-static member function on a null pointer is undefined behaviour (see [expr.ref] p8) and should error in constant evaluation, even if the 'this' pointer is never actually accessed within that function. One catch is that currently, the function pointer conversion operator for lambda passes a null pointer as the 'this' pointer to the underlying 'operator()', so for now we ignore such calls. PR c++/102420 gcc/cp/ChangeLog: * constexpr.cc (cxx_bind_parameters_in_call): Check for calling non-static member functions with a null pointer. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-memfn2.C: New test. Signed-off-by: Nathaniel Shead Signed-off-by: Nathaniel Shead --- gcc/cp/constexpr.cc | 17 +++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C | 10 ++++++++++ 2 files changed, 27 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 051f73fb73f..9c18538b302 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -1884,6 +1884,23 @@ cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun, TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ arg = cxx_eval_constant_expression (ctx, x, vc_prvalue, non_constant_p, overflow_p); + /* Check we aren't dereferencing a null pointer when calling a non-static + member function, which is undefined behaviour. */ + if (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) + && integer_zerop (arg) + /* But ignore calls from within the lambda function pointer + conversion thunk, since this currently passes a null pointer. */ + && !(TREE_CODE (t) == CALL_EXPR + && CALL_FROM_THUNK_P (t) + && ctx->call + && ctx->call->fundef + && lambda_static_thunk_p (ctx->call->fundef->decl))) + { + if (!ctx->quiet) + error_at (cp_expr_loc_or_input_loc (x), + "dereferencing a null pointer"); + *non_constant_p = true; + } /* Don't VERIFY_CONSTANT here. */ if (*non_constant_p && ctx->quiet) break; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C new file mode 100644 index 00000000000..4749190a1f0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C @@ -0,0 +1,10 @@ +// PR c++/102420 +// { dg-do compile { target c++11 } } + +struct X { + constexpr int f() { return 0; } +}; +constexpr int g(X* x) { + return x->f(); // { dg-error "dereferencing a null pointer" } +} +constexpr int t = g(nullptr); // { dg-message "in .constexpr. expansion" }