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 108BF3858C2F for ; Mon, 20 Feb 2023 19:21:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 108BF3858C2F 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 p18-20020a05600c359200b003dc57ea0dfeso1862659wmq.0 for ; Mon, 20 Feb 2023 11:21:19 -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=z+qcNnCf2NEJkIs9Yol52rN/4q3U1Rwq6vMwR+5ayVE=; b=fbYk9630ird1aUdDDxwGx6TnThD+mM/9fd6w3jlWmofO/YGfBhnqmy/bSA+7VV8z9D ivw88+0R4m8sz1mPQRVbB1j1f7hoqPy7q8qr3+unVuMW4jLoz8HpLTWxv9kKOw75drEK ZEr1VEaFORUQWxu7wZNfoVVpbMsKakqesC1IBF9dUku3DQ5G4z72e2a/6JPIy8D4eIyk 40PKifxCZWyNMCSyt3IpoEOTYjU6zRoC3Gc6mSnuqH4yXpMkPwBmTeFMH5sMcp0yn5N/ T/2fiDHfQ94HLTFe0W5IjG3zejsAnEy8DKxcFYCYUZHPMiBGzJi53F2u30Qny93zeFA1 ufzw== 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=z+qcNnCf2NEJkIs9Yol52rN/4q3U1Rwq6vMwR+5ayVE=; b=LUttgQWEh0nIAl3h5YKmGSb4+/B5XHgHYESE4w5NK9kFqNpS0DyZJM6qKVzlCJdRLw f+QqIklxXvit/M5kGXeIViaapj0KUrdwk2FO58bsek5+H7eVdB0/HHQE7ChOCYC7daWB GlxaU2PGwTSzXwJaUM1Ek4n37g13W3AOOcRjW2vp1QnW6M8N8N2pZPr6UoGfZkRccV7S /PHFGNEc1zm3Nk1y3QKK1tfQkk5pG4l9Y+BUjIZjcte0HSdE05bNK0WaS+RN0QygirNa WHD65dL0MO8Qs/m7K0I5ejLhf+jpSlsxKiMFKH4dvLNoZBlVk5G47NuR1IG6JmpQ0swI a11A== X-Gm-Message-State: AO0yUKUXCerfrErCuT2c7QJ6nkw8ej1DiLVN15aqT7jcx2XdMPSHVeUU SPVE8P962Z004pl+Xt/04MFr4A== X-Google-Smtp-Source: AK7set8EXUiiTa5AVX3t+80KqiGXZ73owMY4VuOO+LwK3fX/ccOt1jUEmZEzGi3KqxifXtoLlw2NvA== X-Received: by 2002:a05:600c:180f:b0:3e0:10d:f1c with SMTP id n15-20020a05600c180f00b003e0010d0f1cmr934145wmp.37.1676920876877; Mon, 20 Feb 2023 11:21:16 -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 m4-20020a05600c4f4400b003e2052bad94sm12820225wmq.33.2023.02.20.11.21.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Feb 2023 11:21:16 -0800 (PST) Message-ID: <241687c0-8dca-3c85-84e9-cfab85536324@jguk.org> Date: Mon, 20 Feb 2023 19:21:15 +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 , Xi Ruoyao Cc: 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> From: Jonny Grant In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 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. https://godbolt.org/z/haq18bvPP Kind regards Jonny