From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id 1C0A5385B51F for ; Mon, 20 Feb 2023 13:44:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1C0A5385B51F 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-x532.google.com with SMTP id g1so4632659edz.7 for ; Mon, 20 Feb 2023 05:44:52 -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=QRdAKzrBiuFcPMkGlvF157RHJ62HCpCgzaOiyhQRXRI=; b=EdC3Ngjc3snWEXVE1P6As8iCv8z6aTffN6VjfWMyDmQ4+0yHWlyrIA/59/YTj3BvDN d3lCvkUFCeyGwPY6y03R7v9aYUc1VgqZ9xrgxaFKV+q1IGeiFYSnY54E/6sGKzFSqieq aaGrQoCz5XSznRMkJueouLH7pGHJO4TFM99pjZWuudHRpx4A3X6TFLbOcfpk/is6JPM+ n0EPdRuid8nDurx8wRrAeiIqik9SYLlu2RwCOzToO2EimDPNy69rKxHrWFyBQYyXmXhO mLQg/+N/kmPK52AZXkJoj9gBoa1pv3zsUVnrNyP9oUaT5YRsaYAdGvNeyp3gnjgu8Scw zU5A== 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=QRdAKzrBiuFcPMkGlvF157RHJ62HCpCgzaOiyhQRXRI=; b=k77x1r3X9YWDG2HEPW/yZUTvoM6dPqQPxcnPmGehJ1aAnvW9cSwW/4EFoMvbqXqVWK Ndnua/8Q7KUtWBSWL1aavgvlnraKIbZfJx2+W2wwPEAtUSW4o0FdQ7HyWe/w6EgQXBLB YQf7ihja+c53VgcEs0ytYm7i6RLYeJqDPwqLOs6i43Yt6Onahq2DihpD9Xlc5S9gootB 8uwr3Tu5/h4fDkdAi4bRsyvvD7DqpuHH9hnc3MVN7BTdW11ufNNe91ayeLGwC9ACLcmA 0n4116kmfQgT12P8Xp00f4RlpW/h+VjlNBUw74Zl6fD/BzspAboepiXfDl9mZelHmC8C NJjg== X-Gm-Message-State: AO0yUKXFHEV4ChkvYnXolJjSuuZvbpKAvU0KqZ03Q64QVy572EGuf/WP U1J2uAuVSE3u8qhArwhdfmJT1nvYlzPjJ+1V21A= X-Google-Smtp-Source: AK7set/6/n0rueMGk79oAAarNCMp/PbhF8fvwy7edRVqbONxLGzwySvigeTsnrddqqmHRDpJrnuE8xFg2klHZBv2lnc= X-Received: by 2002:a05:6402:2485:b0:4ae:e50e:542d with SMTP id q5-20020a056402248500b004aee50e542dmr270466eda.4.1676900690344; Mon, 20 Feb 2023 05:44:50 -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> In-Reply-To: From: Jonathan Wakely Date: Mon, 20 Feb 2023 13:44:39 +0000 Message-ID: Subject: Re: std::string add nullptr attribute To: Xi Ruoyao Cc: Jonny Grant , gcc-help Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,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 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.