From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 815263858D33 for ; Wed, 22 Feb 2023 20:27:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 815263858D33 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-x331.google.com with SMTP id j19-20020a05600c1c1300b003e9b564fae9so1724733wms.2 for ; Wed, 22 Feb 2023 12:27:39 -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:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=T5CxA06ykW2bfqIFDdpW40ZO/rOY5BBcb4U7CMqWWAM=; b=pPGmFfDOxrlBxL5ZwpYHcsGl5SG7pCr+yVkF8W0lq7pjl1AQ45s4ENEzyIHk69dyFW E55heJ8YH+09UmKKLsEdHEnnMvBIS1JOv0o8tSSnKO+GnILpRYduDS6C9pot759Dg2nn 31xqjiWvAwHcViof0qkVIvT2I1GvS33j2hCZVtO06+HuO7AKVsIHUMRYMpxpbtELUTBa 4amEONSz7dTYW5C5wjASZF4I7K8IIPVQG2wSzdOL0BAhTsF/2lMj4p10cqwl3Agz4Nho bZSZgTbS7FwE7J34FZVhMwGiA3puydJKUFXcH9rYJuGJYa6XTj6frcs9JkGfEda7P1rc KjVA== 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:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=T5CxA06ykW2bfqIFDdpW40ZO/rOY5BBcb4U7CMqWWAM=; b=HWWO/6ge5LP7eRESMlG+b8tamhaXp1yJ/44YQF3J+4X1OxSfavPi6YQKWOVGmsB0Xx NFFGaRiK5OJSC5SEtXX5HyP4QWPnjNSF9RV9h26H7d7eh9v2fm3U9HNBWIPyIEk5Wbjb dbyS0rDouJp1Je4/BRu11LWtwKRPa7L9RC8f+TcA3NeDo4eky4pskRLrFCiYpM4JK0vd fd/LE2thXVrmgHhLidTP0GVGGHzfHhuDkmLhOQrE3Eidwf7S62E/3KXi+/bPzoqhCF8U R2/4rh6Yy2vjgYCgQWivxdD6Lw4oP5jenWJkVhVZ9cQR0klyWclVqv0ECAzQQLcjxKw5 Mcrg== X-Gm-Message-State: AO0yUKXmrRGDTDNXKNtSTdnAPZHS6LC2HzcPhPXi1NrqaCDATQwyJpFJ np6dzy3n+M4ZaKaf2GjwITLSRg== X-Google-Smtp-Source: AK7set/hHlW8jweLIwzBIvq7QhGCAmkGY/OxHYOx4f9If/qFIRWYtYhiM+716oYuQei5gxNJnaGZAA== X-Received: by 2002:a05:600c:2b46:b0:3df:f7ba:14e1 with SMTP id e6-20020a05600c2b4600b003dff7ba14e1mr7993357wmf.8.1677097657251; Wed, 22 Feb 2023 12:27:37 -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 t23-20020a05600c2f9700b003dc521f336esm8495584wmn.14.2023.02.22.12.27.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Feb 2023 12:27:36 -0800 (PST) Message-ID: <0039507d-6fed-fad0-dd9d-127cfb3afe05@jguk.org> Date: Wed, 22 Feb 2023 20:27:36 +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 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> Content-Language: en-GB 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,KAM_SHORT,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. Ok I see. I filed an issue with a simpler test case. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108893 Kind regards Jonny