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 3B0AD3858C35 for ; Mon, 18 Dec 2023 18:33:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B0AD3858C35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3B0AD3858C35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702924401; cv=none; b=br9IARH5lXAo9BsJI+IQtr+8G+KNlgl1YXx+uYK4fiVLzzAA/FI2klQ1YuWYUW48a2AcvcBmRONtxCLyrVWiT294J0r62kekBhPgAw6qr02EPmWI/+mM/MZbvIFNQpwasK64UXbArLytExUC2zcWb/GdQ08RK56cY3RsVlPijkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702924401; c=relaxed/simple; bh=PyOz+m5gdwBrn6ItEHaad+MN3YEc+CVRXhCzrF23cGM=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=AdetcMce9+BTuE4oZbJqNW11PtJsDyz9QJclU6pBNgXTVjhmmX9DxU6N5q+1K7+Y4fG843jutiXO2KN/floNmkkwL6kAH1+7F/yOCUGjhBvV6gVlZrh2etLsV68jQcDYa4CWVuB1BQpR0UDA/fQfFMqJrDg5P1WPCdV+OdahFh0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702924390; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MBmbs5dzSx9cC5QNaboAEjZLgI+RmFN+lwP066Uw61I=; b=WFNh5PKgLfYzquWO3RdYm83hUR85HvRX5GCmM/lWCVCxISf8BF6tVohjf2rLr6Z/4zFWW8 cirvwG2/RjQRNnOKwaAtF8WYR0s8Z33a+DCmhPoLEekzoAClqPKWyHM+9IwsHu0fi9PBOW TrFjkIGcdeJygX5aVvLSCmhcySinIwA= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-693-5BKK2SJFPT-PSAt-zVtrkQ-1; Mon, 18 Dec 2023 13:33:00 -0500 X-MC-Unique: 5BKK2SJFPT-PSAt-zVtrkQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-77f383fa40fso573260485a.0 for ; Mon, 18 Dec 2023 10:33:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702924380; x=1703529180; h=content-transfer-encoding:in-reply-to:from:references: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=MBmbs5dzSx9cC5QNaboAEjZLgI+RmFN+lwP066Uw61I=; b=Y4tfPwSleTL6jzP0yuTxlTYZNl5jAgj2EjpMcbPfOVgFYaDnJX2Fq9reNq5cVyN7K4 2wlBwUtysmcIDZjUu+cImzVQaG4PcXfusiN41V6O22TqthrtBD+JfbGsW9rDHsFvJ+v6 b0rdX0zgfsi45vv06S9yAbVr3SZHoF1Qg9LcweATrWkmFJ6fkg61Q0ThLye5NpJUEs4+ /Rl62wy6Q3cu4InHsdyP0fzsqjVTxDJkNZH0hybvjdOBVNe+Odw2FNc2Q1/Oayt3Mc0m pgLFo+OMMBlneCRc442jQNHQ4wLOHxI6g2aElzDdTg5sMFtGSecV5PP6jyFEU8OxbXtm bGiw== X-Gm-Message-State: AOJu0YwbLqD2eEtHg6Xfe+V4zkM4jpwWVjpUiaG5BokhXtOkSq9unD0u Baljy7M6at5RX83ICCxHn8hnRxy5zdkQPc6puskYNKoOLEyEIShPco8sXCrnljt414c/I/PvINs j0E+53qfaETSh9vlztg== X-Received: by 2002:a05:620a:1708:b0:77f:38f2:2d97 with SMTP id az8-20020a05620a170800b0077f38f22d97mr21902557qkb.93.1702924379844; Mon, 18 Dec 2023 10:32:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGjHc10+VHE7uAkys0JPB2isM5uzbiPb3cnBJPvbCF7p8fXi+zChvD5/BtWy9/ZGaR52AxnFg== X-Received: by 2002:a05:620a:1708:b0:77f:38f2:2d97 with SMTP id az8-20020a05620a170800b0077f38f22d97mr21902545qkb.93.1702924379471; Mon, 18 Dec 2023 10:32:59 -0800 (PST) Received: from [192.168.1.146] (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 az6-20020a05620a170600b0077fb0ba0f35sm2832439qkb.90.2023.12.18.10.32.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Dec 2023 10:32:58 -0800 (PST) Message-ID: <4201c638-a4a8-42cc-9b91-321545c1b972@redhat.com> Date: Mon, 18 Dec 2023 13:32:58 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++: Check null pointer deref when calling memfn in constexpr [PR102420] To: Nathaniel Shead , gcc-patches@gcc.gnu.org References: <657f6d54.170a0220.7e557.2d05@mx.google.com> From: Jason Merrill In-Reply-To: <657f6d54.170a0220.7e557.2d05@mx.google.com> 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.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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 12/17/23 16:51, Nathaniel Shead wrote: > 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. I don't see a way to do that. Better IMO would have been for the C++ committee to make the op() static in this case when we introduced static lambdas, but that didn't happen, so we're left with this kludge. > -- >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 > --- > 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) We might just stop here, since any CALL_FROM_THUNK_P is compiler internals, and we should have already complained (if appropriate) about calling the thunk. OK either way. > + && 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" }