From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 29AD6384F033 for ; Fri, 10 Feb 2023 22:38:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 29AD6384F033 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-wr1-x42a.google.com with SMTP id h16so6481005wrz.12 for ; Fri, 10 Feb 2023 14:38:12 -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=SqFU8OK3Dldt3A/k++uKRAvcSi92J/8PW9sQ1gmkDfY=; b=pYNo98TYQntKUBL70X9dc4qHa2UhSTeq5rrFJiEMjZHqtAg1ZYa3tjcq6xaQiEvKpP KzCx4cd1kr0SnV73vhLy2ZiDfXvpilZnjTI+N4Ji8+6NatSTIzI1cWiqYP468S27rSBG b3F4wgrnnQpociz/o5EQ0LWUPNfkTJzOV31ZMW5PQTouAvKwWZ1hRa5+8AnUUQuXWo9S Wtbh5gWaCiMxthLqxToe6AdsBTAvYw2W/9rZfcFoVLxTnT9J7Fv8G3fBzSFaSg4/ULeE ojofxhHNHcraBtY5wdGbU6GDNSV/S5U9f0y/7zV+QiHMOIydrqIbLuUX7rNRc/8J8xbj cfVQ== 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=SqFU8OK3Dldt3A/k++uKRAvcSi92J/8PW9sQ1gmkDfY=; b=7YXY9jG2vtqz6HSMJA3AHTA0RTuND8UoOWdz50h2IHdBwaiQ1QtqFNFS+Ia+Q5QFrz XFql04Lz94AhTm5SH/anwlJKzj7mk98plUtbjQDPIa//LPl/b4MCa+6QNmx8uKx8RW6w sFHIM+fhpagbQB0YpTUI0P+JtV9ZMM5P/NvY8Ksu2RbwQIgZfd/gwYiEGTBzUy+I/V+k tVc2dPYpWtNhxCu2HA4jcFeSTHeN1Dl0MWETJbG4tldIa5I36y9MOINg6VCIPMmyD7vp Mpb9IohVixZr0FwOvvpMY7BSqdVTXzw4m53qpbl0kepplsgwl0y1X1tLG8uElhmGZSM5 0Xhw== X-Gm-Message-State: AO0yUKU8m2x3ITOl/qROX9DboOBh0J3+p7vNO1JJL3TICOD+V3lKpMFT 84I/sgd6ncSMamPKDfo0p+77Qg== X-Google-Smtp-Source: AK7set+aroFO8VTxHcsIWsJuYeKJMJUmK9gx1agZjRJMIIs4wYAEzLolMIyWMV3ZAeyr/gLjR54bNQ== X-Received: by 2002:adf:e5ce:0:b0:2c5:4d97:4a23 with SMTP id a14-20020adfe5ce000000b002c54d974a23mr586245wrn.70.1676068690660; Fri, 10 Feb 2023 14:38:10 -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 g7-20020a5d6987000000b002be063f6820sm4515894wru.81.2023.02.10.14.38.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Feb 2023 14:38:10 -0800 (PST) Message-ID: <6b530d67-723a-a0c9-15bc-12b7341653a7@jguk.org> Date: Fri, 10 Feb 2023 22:38:09 +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> 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=-4.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 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"; Sharing my example, gives compile error for 0, nullptr but not NULL (only for dumb direct calls) : // g++ -std=c++23 -Wall -O1 -o string3 string3.cpp #include #include void nullptr_compile_abort() __attribute__((error("nullptr compile error"))); class str_string { public: str_string(nullptr_t) { nullptr_compile_abort(); } str_string(int) { nullptr_compile_abort(); } str_string(void *) { nullptr_compile_abort(); } }; int main() { str_string y(nullptr); } >> >> >> g++ -std=c++23 -Wall -O1 -o string2 string2.cpp >> In constructor ‘str_string::str_string(nullptr_t)’, >> inlined from ‘int main()’ at string2.cpp:48:25: >> string2.cpp:20:50: error: call to ‘nullptr_compile_abort’ declared with attribute error: nullptr compile error >> 20 | str_string(nullptr_t) { nullptr_compile_abort(); } >> >> Jonny Maybe C++ guidelines not_null is a better approach to prevent construction? I've not tried it yet. Jonny