From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id E1F1A3858D1E for ; Mon, 20 Feb 2023 11:28:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E1F1A3858D1E 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-lf1-x12d.google.com with SMTP id s22so1188860lfi.9 for ; Mon, 20 Feb 2023 03:28:17 -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=M7jdTevBmWS5N17ZnR+g/4wFa1X1ap8uASSRW3yaWM8=; b=a3KBue2TmKLedVNbTkr3Hh4dLYEid4z3VSbEPDPlf8GdvuYzd6wh0wbvNEEqwFLKhd AVIOA8Yp5FXVoCP4qhW9Wz/cJOSJhOM4VA3ZPCL6WYgdkayQybOo4WqXupFaFeM0Kxkr fyXe1q4E9qc1hvHRrfG+xDvHJuXF+eirrY81w1vmg+inOb0fbbmOp6WmxPjB5bDpewc5 02O5W6lc7JLb+KR87UeHOERgL4cCHTYXrR/QwqRbQPpbJXHdpAt4O3oIV0dOmIa+dSAp jJIhD7BNGeeazmOqnYfjsO47zVEXs1CxK84XGYs5rmLMTLW7LusE9bF6PtmYwaTblcSH cGXw== 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=M7jdTevBmWS5N17ZnR+g/4wFa1X1ap8uASSRW3yaWM8=; b=daCBexucdtjRNCyD0tHDeignZZLadWOVD567C/jstaCIiutm6jc7dePQnvshumZAGr 84l6L+AXBDFPOeSkuDvHrHwHbLZD4QUIhctHeQ73q2VfJUwBuA5f6K7Cr3I4SQTHwvMg QaiHk4cLuxDgq1ky+Dm114BWHEu0R7guDD6AAKvV1qX1lZiblyPoj9CudipoW+/YlVQw rpEoRgm3BLzAbWxZOhJsQOQWlIm7ZULTfXRAHxvoYGAanEVN5gV5hzt/VxEB8pviLJ1y AbXu5O2Du93ZVRzm9TbM03iLRdos7MwN/QdRsldrH3GJ+m3CjG2kpO+bCB5amsmkW5ZY dL6w== X-Gm-Message-State: AO0yUKVsX2SG+/Ss9DSHHjR7RQfJCGc25ZDy669yWEBUFGNY1wUBbaX1 YPk6TcElSK2PnX+gRtnjNrdZQjBvJifw3+ZD X-Google-Smtp-Source: AK7set+Ycp50EvRTuUogL4bIiWwB+zG3UpNB2/hFgk65PpdtyaNvntCndRn2AZ+U85eE2JRn7n0Ajg== X-Received: by 2002:a5d:5306:0:b0:2c5:a1dc:8c0d with SMTP id e6-20020a5d5306000000b002c5a1dc8c0dmr959761wrv.61.1676839386707; Sun, 19 Feb 2023 12:43:06 -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 a18-20020adfe5d2000000b002be505ab59asm750290wrn.97.2023.02.19.12.43.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Feb 2023 12:43:06 -0800 (PST) Message-ID: Date: Sun, 19 Feb 2023 20:43:05 +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> 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=unavailable 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 11/02/2023 00:32, Jonathan Wakely wrote: > On Fri, 10 Feb 2023 at 22:38, Jonny Grant wrote: >> >> >> >> On 10/02/2023 22:03, Jonathan Wakely wrote: >>> On Fri, 10 Feb 2023 at 21:30, Jonny Grant wrote: >>>> >>>> >>>> >>>> On 09/02/2023 17:52, Jonathan Wakely wrote: >>>>> On Thu, 9 Feb 2023 at 16:30, Xi Ruoyao wrote: >>>>>> >>>>>> On Thu, 2023-02-09 at 14:56 +0000, Jonathan Wakely via Gcc-help wrote: >>>>>>>> Note, my code isn't like this, it is just an example to suggest >>>>>>>> adding the nullptr attribute, as its clearly already rejected at >>>>>>>> runtime. >>>>>>> >>>>>>> I assume you mean the nonnull attribute. That was added in 2020 and >>>>>>> then reverted because it broke some things: >>>>>> >>>>>> I remember I'd once made the same mistake when I suggested to add >>>>>> nonnull for ostream::operator<<(const string &) and I was lectured: >>>>>> nonnull is not only a diagnostic attribute, it also allows the compiler >>>>>> to assume the parameter is never null and rendering std::string(nullptr) >>>>>> an undefined behavior. >>>>> >>>>> Yes, I think that's what might have happened with the std::string change. >>>> >>>> My apologies, Jonathan, Xi, yes it is the __attribute__((nonnull)); I was mistaken to type as nullptr. >>>> >>>> I re-read, and it does seem nonnull is really an optimization that as a side effect may give some warnings. So I'm going to stop using it. >>>> https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes >>>> >>>> (there is a typo in that manual section saying "nonnul" - I don't know if you have a moment to make a change in git? I didn't get replies on gcc-patches to my patches...) >>>> >>>> I searched and see like someone investigated this problem and saw it removed NULL checks http://www.rkoucha.fr/tech_corner/nonnull_gcc_attribute.html >>>> >>>> I saw wget2 removed the nonnull attribute due to the optimizer removing checks against NULL too >>>> https://gitlab.com/gnuwget/wget2/-/issues/200 >>>> >>>>>> Then the example may just silently continue to run, instead of throwing >>>>>> an exception. It would be an ironic example: an attempt to improve >>>>>> diagnostic finally made diagnostic more difficult. >>>>> >>>>> Indeed. >>>>> >>>>> Maybe we can add __attribute__((access(read, 1))) instead, which says >>>>> that we will read from the pointer, which also implies it must be >>>>> non-null. >>>> >>>> I tried this with gcc 12, as read_only, but it didn't stop when compiling. Maybe you have an example that demonstrates please? >>>> >>>> void f(const char * p) __attribute__((access(read_only, 1))); >>>> >>>>> >>>>> N.B. in C++23 string(nullptr) produces an error, although >>>>> string((const char*)nullptr) doesn't, so in practice it only prevents >>>>> the dumbest calls with a literal 'nullptr' token, and not the more >>>>> realistic problems where you have a pointer that happens to be null. >>>> >>>> That's good it stops compiling, the error is not that clear "use of deleted function" for me though. >>>> >>>> string.cpp: In function ‘int main()’: >>>> string.cpp:13:26: error: use of deleted function ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::nullptr_t) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::nullptr_t = std::nullptr_t]’ >>>> 13 | std::string c(nullptr); >>>> >>>> >>>> >>>> >>>> I made my own test class str_string which stops the build a different way. It only works if the dumbest calls with 'nullptr' as you found in your test. >>>> >>>> void nullptr_compile_abort() __attribute__((error("nullptr compile error"))); >>>> >>>> str_string(nullptr_t) { nullptr_compile_abort(); } >>> >>> This doesn't work because std::is_constructible_v>> std::nullptr_t> would be true, and we want it to be false. >> >> Hmm, for me, this output is 0. >> std::cout << std::is_constructible_v << "\n"; > > For C++23, yes, but if you add a constructor like your > str_string(nullptr_t) it would become 1. > > Using a deleted function is observably different to using a > constructor that then produces an error when called. I noticed -Wanalyzer-null-dereference reports at build time a dereference. Also works if a function parameter. I wondered why std::string isn't detected by this static analyser option. :9:10: warning: dereference of NULL '0' [CWE-476] [-Wanalyzer-null-dereference] 9 | char b = *a; | ^ #include #include int main() { const char * a = nullptr; char b = *a; std::cout << b; }