From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by sourceware.org (Postfix) with ESMTPS id 7BFCD385B51F for ; Mon, 20 Feb 2023 19:35:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7BFCD385B51F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x52a.google.com with SMTP id eg37so4856056edb.12 for ; Mon, 20 Feb 2023 11:35:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=wtpXRbK8GuIA52WIJICOY3j7cxFXn0AR9xxqtlh9Qrc=; b=Z0/S3p+NMzjqUvIJ2Uq2XaGPznPqyUgp0MmmoXonpNMd/+ObKlMGZ+Ihqgvsm+4aG7 2XZdv36u9Yr4s5o4fKnGuzdmkxVnrg8RMn1KZALXfIS/2djpY20E0dgPbUtzYPt21fJI V2wevMs36bw/hVL/LDFvawfjsRlZfVyacfRW+EzbhlP5hGlPTNaEO4iV2o/iuHa9FqaG IktGxwVXgxW6s3DPRUndWynS9q4kcGXIIHUqvHKEc8TN5u21wB16uOZadIBrV98QH4vP WgJXUXfHNag2CQcHiUsOVqjQ3o796T1JbUCOh3Cioe1Lco/vs1XYDxKcABjZr5h7hRV3 2MGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wtpXRbK8GuIA52WIJICOY3j7cxFXn0AR9xxqtlh9Qrc=; b=L5jmgulZYsC1lvx9rh0GIBRfAotVKbwxlGj/mlDVU+xgvBW+YGfKkb+yogiz/V3UEt RELHPfbTb71k4XmaLfgnjRcmWHxQYQsOwP/Ex0TRTYqNfd81ZWDdTzPpvFxQSTT8LFkM TczTtKJEa6RMNCKr/D23HJmOvysN/YyZbs3mMx1BaltxAuuydtJo5M0rm1ExBdCavKJg 9EGaJntGiJvdZed6vEWIGpHDgdtgOe/IuX+LSyDxjLFv9D+a0Hv1EoWaA8X7kfyOX0uR Qi30CCIHR+kf7ZJbASZ6xxsUWE93FLZcRsoAkcyczgfYwjmfZt6Q9+ZHRJDPJU42V9W6 iCIg== X-Gm-Message-State: AO0yUKUb4txaZZHq160a7YTS6uIRDOpvYWxKKtvwKy5iD66hCrK5/HEb XJpaAA4mdYaQ2v2F1rcSc3yS31RfeMUjXOaU5Pk= X-Google-Smtp-Source: AK7set/SmrJnHKVgTWgUsvvARORS2H4H4u9hT1StA+e0sDlsqtntt+QbfhUARuPgBhF185D5ygcd2LNt0JSD4bwY1zw= X-Received: by 2002:a17:907:72c1:b0:8c0:38e8:8c83 with SMTP id du1-20020a17090772c100b008c038e88c83mr5231336ejc.5.1676921752994; Mon, 20 Feb 2023 11:35:52 -0800 (PST) MIME-Version: 1.0 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> In-Reply-To: <241687c0-8dca-3c85-84e9-cfab85536324@jguk.org> From: Jonathan Wakely Date: Mon, 20 Feb 2023 19:35:41 +0000 Message-ID: Subject: Re: std::string add nullptr attribute To: Jonny Grant Cc: Xi Ruoyao , gcc-help Content-Type: multipart/alternative; boundary="00000000000067455b05f526c529" X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,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: --00000000000067455b05f526c529 Content-Type: text/plain; charset="UTF-8" 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. https://godbolt.org/z/haq18bvPP > --00000000000067455b05f526c529--