From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id 0E23D384F4BD for ; Mon, 20 Feb 2023 19:39:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0E23D384F4BD Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=jguk.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=jguk.org Received: by mail-wm1-x336.google.com with SMTP id l2-20020a05600c1d0200b003e1f6dff952so1859948wms.1 for ; Mon, 20 Feb 2023 11:39:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk.org; s=google; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=/VywiSdUHCKSwJPBBw2z1cwetebPJma9uAekh6QPzWI=; b=jXDHlCi66xC8LDQe8jVFtb/qRVc0Auli74aHkrdm9D1kV8Q7CETCyA60lczLGQhVeB /DO0PirTgs+TQwuU6TkpaBnMNTHkCIqIxojqcKWLi/QWy9KFqj3CXVlgoWfYWgKJOMJO 7hCBVGVjL9JhTwTliHryfzX7UkOmTDTlp9Rnk1Pttym6JmrQpfHsRM9l+QjztlOCclod kUbhp4y+45gTSsUO6dDejmE0Yn/ttn+dCbZaoPlOCSWnJzOe65Iykh8uF8zdZaVEF7hR c+lDwzZMG+zdDwC4J7Jdyn4DBvmwmql1s9x6QxXDX8R5Lt0mkaYYqw08Tfq5Cmw0i1/A ZdWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=/VywiSdUHCKSwJPBBw2z1cwetebPJma9uAekh6QPzWI=; b=bRT0im2LDLaBb7h7V74jfugXFC0IPX0WxhfKt+Sg1azDNyUfWdDbJTumcVLCajIw+0 Q9/P97v4Xvj+JPwWrFbqBq/9i+Ekc9PqnaCHSMOji03SdkbMEwOEKozbFSnF/GrZpEVL SGiO8+U32+ML7yUuFY5TDSnfZYMJZ+8VdF4rFJJtfHR1RAHsvi8qd1UlrdUNmKoawprK O4hM+Ia6jm4TUdN8ZUCfSN7BbVpvRvSOtCxjg8bZX9BxecCRuWMttizJ36on9pqrOLpT EMW6ivrdJVGsmIJ8grITREbJPSVcZVVCjYMAAfKt5vp7rFC5hJvH6al2TjSqcL8l68Ti DhBg== X-Gm-Message-State: AO0yUKV8OF+YRZDU8EkEk4hFNjzUHESU/hVRtDumml5ldOGQ2vdTTqcH BWqet4xTNgOXQLZ5xjQ+Q7nRNw== X-Google-Smtp-Source: AK7set/cVsS2hft/KYZeNuWXaQbCRbXs1VZxU1S925AME0Qi5sjVSTT70DpncsQfmlJT6HkVpCY7NQ== X-Received: by 2002:a05:600c:3d88:b0:3df:de27:4191 with SMTP id bi8-20020a05600c3d8800b003dfde274191mr1460427wmb.16.1676921943881; Mon, 20 Feb 2023 11:39:03 -0800 (PST) Received: from [192.168.0.12] (cpc87345-slou4-2-0-cust172.17-4.cable.virginm.net. [81.101.252.173]) by smtp.gmail.com with ESMTPSA id fm18-20020a05600c0c1200b003dc1d668866sm2440729wmb.10.2023.02.20.11.39.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Feb 2023 11:39:03 -0800 (PST) Message-ID: <34bbe89a-ff3d-c2eb-ead2-0bb6b0b40c89@jguk.org> Date: Mon, 20 Feb 2023 19:39:02 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: std::string add nullptr attribute Content-Language: en-GB To: Jonathan Wakely Cc: Xi Ruoyao , gcc-help References: <7e6e3bbf-0dac-0632-0e8f-372bd32a6923@jguk.org> <6e30ed8e6c6f08407a5b8259e73fd18a492376b5.camel@xry111.site> <8cfbab8b-07e8-7dab-c829-6de77cc8cf39@jguk.org> <6b530d67-723a-a0c9-15bc-12b7341653a7@jguk.org> <96f99315a6ffd3dd3919b23a4ade2597747a580a.camel@xry111.site> <163945d9-6c24-d4e1-7029-980b988bd634@jguk.org> <241687c0-8dca-3c85-84e9-cfab85536324@jguk.org> From: Jonny Grant In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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 20/02/2023 19:35, Jonathan Wakely wrote: > > > On Mon, 20 Feb 2023, 19:21 Jonny Grant, > wrote: > > > > On 20/02/2023 13:44, Jonathan Wakely wrote: > > On Mon, 20 Feb 2023 at 12:59, Xi Ruoyao wrote: > >> > >> On Mon, 2023-02-20 at 11:30 +0000, Jonny Grant wrote: > >> > >>> Thank you for the suggestion, I gave that nonnull attribute a try, but > >>> it doesn't appear to warn for this example. > >>> > >>> https://godbolt.org/z/boqTj6oWE > >> > >> Ouch... The optimizer inlined make_std_string so both -Wnonnull and - > >> fanalyzer fails to catch the issue here. > >> > >> Adding noipa attribute for make_std_string will work, but will also > >> cause the generated code stupidly slow.  Maybe: > >> > >> #ifdef WANT_DIAGNOSTIC > >> #define MAKE_STD_STRING_ATTR __attribute__ ((noipa, nonnull)) > >> #else > >> #define MAKE_STD_STRING_ATTR > >> #endif > >> > >> std::string make_std_string(const char * const str) MAKE_STD_STRING_ATTR; > >> > >> It still looks very stupid though. > >> > >>> Feels useful to get build warnings if compiler knows nullptr is going > >>> to be dereferenced, as clang does. > >> > >> The problem is in this case nullptr is not dereferenced, at all.  So if > >> we want a warning here we'll have to invent some new __builtin or > >> __attribute__ to give the compiler a hint.  AFAIK there is no such > >> facility now. > > > > I think __attribute__((access(read_only, 1))) should do it. > > I tried here, but no luck, is this example the way you mean? gcc trunk. > > > I don't mean it does do it, I mean it should do it. Ah ok, couldn't find a bug report. Did you already report it? Jonny