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 CA2153858D32 for ; Mon, 13 Mar 2023 10:11:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA2153858D32 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 x3so46319896edb.10 for ; Mon, 13 Mar 2023 03:11:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678702270; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=5HHT/+7w5/xuPCgw1bnubiIcY0J3E7C3JaRf03Erk1w=; b=R1gu+F5iKcskDP+1NNJdP23aCf+RmPtF3hv2RQI2M23oYX/AdueGmdQjnWzlqB45Iz 2oLIZBhY5nlhKIE08xRwaUDdgAJ5dG85CQmovDRj8Qy9ZGVazc78U8jToZjd/YsrsBpB tcglA8ZWNzqeYlmSMoJJio1p8aUusX7oB+dLt6rZyTrEsUQNVOwcbivhK3UPEmBxdsXG 3McC5l4/EeJ+resDkfKy4sQmAEfLdR+lc5msNj79/LL0VRoMSPljhBd1xkwauYn1UCoV s0hZwezGjEwlaJoYohB1fsHJm9MpmkZuq/WcJxZ8lKNV6bjm8tgRLUJSWnS0MBGT8zn1 3XvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678702270; 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=5HHT/+7w5/xuPCgw1bnubiIcY0J3E7C3JaRf03Erk1w=; b=4BBXX5GeECizKEGOaspu87w6i0wjWx7mrMui9g24yXFdmUybivAoH08WkoYw2TMJ3N r8KxIid0wqS7BRH1WA0EOE4hPXaqXbLSjxwiLX3iOw16YeLWItIor6x/FIgbv+IvoSCP X41QU5umJWdK0QWksMG2iD+98EH57A6UBnkMP9g7GeX0Dp8xv2C537KxKzluhbOq2Cxy 0mBZv75UdgWXOevB7OOZoTOvXZkc4UHFWKuxLdRu8K5+d6ma8y+NI5hf4bXH2bXFzq0/ esHlXIExdf+Vp1zHMIXUm9YmCandIH+UJha8DyVSzLQZ7Dbxm/XeqYFyk1Rodst6okve 4Oew== X-Gm-Message-State: AO0yUKWsy1Vz6kayWr2RrlCZtj66VSJi5XVS458Zwwu5XAIHdgz+sudD sZCowSGrNhNQhgb9oNOnFL5elqizTHHsMrIG16E= X-Google-Smtp-Source: AK7set9hebQF9rCDaOj9OWf7wipiw3IPTx7+XvQOAfuGZQtkoGO/Eiq4QgmydUOLR7dSryjnn+kBt75Gskn3n1606kk= X-Received: by 2002:a50:a6d2:0:b0:4fc:fc86:5f76 with SMTP id f18-20020a50a6d2000000b004fcfc865f76mr827890edc.6.1678702270122; Mon, 13 Mar 2023 03:11:10 -0700 (PDT) MIME-Version: 1.0 References: <7e6e3bbf-0dac-0632-0e8f-372bd32a6923@jguk.org> <6e30ed8e6c6f08407a5b8259e73fd18a492376b5.camel@xry111.site> In-Reply-To: From: Jonathan Wakely Date: Mon, 13 Mar 2023 10:10:58 +0000 Message-ID: Subject: Re: std::string add nullptr attribute To: Jonny Grant Cc: Xi Ruoyao , 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 Sun, 12 Mar 2023 at 22:10, 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. > > How about adding a method that is called by these operators that has the __attribute__ ((nonnull)) ? > > example: > https://godbolt.org/z/bqW86PP34 > > >> 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. > > > > 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. > > There is a way to generate a build error for even string((const char*)nullptr) > > I made another example that detects nullptr being passed around (should such stupid code occur) at build time providing optimizer is on. With -O0 it just gives the error always; so I put in an __OPTIMIZE__ check. This example doesn't need -fanalyzer. > > https://godbolt.org/z/TdGnno4K5 > > #if __OPTIMIZE__ > void nullptr_compile_abort() __attribute__((error("nullptr compile error"))); > #endif > > static void f2(const char * str) > { > #if __OPTIMIZE__ > if (str == nullptr) nullptr_compile_abort(); > #endif > } > > int main() > { > f2((const char *)nullptr); > } This causes compilation to fail for code which is never executed at run-time, which is not permitted by the standard. You can use __attribute__((warning(""))) instead, but that is broken^W inconvenient for inline functions. You need a non-inline definition of the function, which means exporting a new function from the shared library just for this diagnostic. All these techniques you're rediscovering have been tried before :-)