From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id E2DC8385841A for ; Fri, 10 Feb 2023 21:30:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E2DC8385841A 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-x335.google.com with SMTP id f47-20020a05600c492f00b003dc584a7b7eso7155335wmp.3 for ; Fri, 10 Feb 2023 13:30:48 -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=+0tzxpvpv8TMtaxXNA9v4bZxiHSXaLaaAs+FxXd6FSQ=; b=ghWZc7kOWgESCMCWm0grA6A8Hir6t0QYfYsOALTtaDG0VKZ886r4Ijb7WbBsQngpiD xX+zf57qQp1gwD0QzB0mpIMR/b3thVFsef/xB8Wze0dchn2UgsdG18PJetEuQiEIjtKD m01uOW3f7G+gmqKcp4XK8wZiKJ9CRz91xFjxfsCV0gTl2voS8DPscJMcOxeu+1Ut2NxY A0lZRPYOkqr1BJ3pNC0P2vdg6qdbL4fEIngZo5wuisI8didFyJ3otYa+57xDL5Ec7kEc UyqvlIjmK3BUwHvkWnhxNGkL8AHJ5sFaaXHiJJqlXpa5T813TJ2Icpf9EtfU8IvQmJu1 CpTw== 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=+0tzxpvpv8TMtaxXNA9v4bZxiHSXaLaaAs+FxXd6FSQ=; b=URZpaQwlviy9TOESNoPhNhmDt++EArM2mtHtZtCNazK2XMbdcEQwFm/edZZd9l3hiI T8T9gNmemp81p2fO8u5LXPhas3Vsq5lemVmOhXRGQ6d71Ok/St3j1gZUFTfM6jcmqJS/ l/ZUVZttqnc7Z6Y6/hjMYPCqnSsHKrIkTKD9Cy2OR7nNdBnbBNx73bsVMkLRNVwPEBfi 2Q6/Fm/p6qj9t8b0/TKpEn3dDgI1UddrYuPnm+l9dKytue8RGkVjkpZdq/N0VdVW5Lie 1u7rpiNwDwevbhf/AqWuN300T4gJ2AwEzDtVi+bgDTo3H9FWJQQ8NMFubwDOgPn/Q2n5 Zk3g== X-Gm-Message-State: AO0yUKUQGDBl7GLkurhl7FTJ0J0BjKS0BoHTGqdXCm92KRVp/WKRsawi xIFKaFJ59J7DBJLj0qQXkY/ZEQ== X-Google-Smtp-Source: AK7set+C0M5Oq6vuPJ8dDWAahBawAkl+bM1Dq0PUEOj7aogbfW0GTIKg6wBhbK54Hxh1+HtIplaYgQ== X-Received: by 2002:a05:600c:2e94:b0:3da:fa71:a333 with SMTP id p20-20020a05600c2e9400b003dafa71a333mr15308420wmn.12.1676064647238; Fri, 10 Feb 2023 13:30:47 -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 h8-20020a05600c314800b003e11ad0750csm5109101wmo.47.2023.02.10.13.30.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Feb 2023 13:30:46 -0800 (PST) Message-ID: <8cfbab8b-07e8-7dab-c829-6de77cc8cf39@jguk.org> Date: Fri, 10 Feb 2023 21:30:45 +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 , Xi Ruoyao Cc: gcc-help References: <7e6e3bbf-0dac-0632-0e8f-372bd32a6923@jguk.org> <6e30ed8e6c6f08407a5b8259e73fd18a492376b5.camel@xry111.site> 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.1 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 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(); } 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