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 5BCB03858D32 for ; Sun, 12 Mar 2023 22:10:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5BCB03858D32 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 t15so9620637wrz.7 for ; Sun, 12 Mar 2023 15:10:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk.org; s=google; t=1678659006; 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=zySXZxjeygJmS7YhNDMtv76/3SmUdcvKC/jiPktSvQs=; b=Zn6doyS9AhdXYGcc9/adP7arHbM8D1E8Yx5BJF15YCSvUTaOv4BP7iAOa5jDmSZJXx qvl4CwGIsijTwYWe1+2JWCXVbCl4tB0f1onpm5YeMoCYO4tyTeYZbaUvR+Iwg089Otxw eYmQrmITjYZZCUhWDx6nFKRQyr9xo6t91G5GaLrTj943gWtJjmwi8y7RgQaJh5iZBtiG MYstOVNwufUOb355xyp3V3C7ZvJzJBr7tJzPHs4kT2IRX9EF7Mxv9rO5MorP8SyLBAtu 2pAGYVtNdIAv/rwp4wechtCtnrwE2T4Pu2VNAGi/rsMyXNkM1xjCKyaPAbrdHEuUZzZ0 RGlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678659006; 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=zySXZxjeygJmS7YhNDMtv76/3SmUdcvKC/jiPktSvQs=; b=UYDXdSq47GR5n3W8K1g/y2em4EqzLDLrgFT81m2h9a7l7wgmPyfcg9L7COUjMFT6UE u2xu4S5CufE6SfXZLKanQLP/x/e2d149qJIgUDWfWgd3Gv7+Bcx9gojkuFS75Yi4oQYl xS82dNMSWQu+PndtsJ856w86TeVKPlPIYujiUSWUXLSRUBwwV1/y8tGYXsYfeHxCkE16 2sP0ILDm1YabUcHGdfKtWuaYOXLM6KRsgeiM4BQv+pQtVhiM3g7yn4N7tq1j/4fU4kE1 qrA5Czp4NCS3QE98nuwTknyK/vVaCqIdN3usJ1hIX4zsl2arI0bPqago9xiRNIRB7CEH 59Qw== X-Gm-Message-State: AO0yUKXWOEGB6dlmOoVfRKbF9FJnygON3iDPgRhkGRajhWwpct8UNVOt JHevAqIucQW5SDrbvPo8wIezPg== X-Google-Smtp-Source: AK7set93CFi93tTwWn/WxfQlTzviEtwWvi5VprBFrRRoFZOj5N5gMyLrT9ZkVvgjWeXJtx4dreZuxQ== X-Received: by 2002:adf:fc44:0:b0:2ce:a96f:4d1f with SMTP id e4-20020adffc44000000b002cea96f4d1fmr2512306wrs.19.1678659006008; Sun, 12 Mar 2023 15:10:06 -0700 (PDT) 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 h16-20020adffd50000000b002c56af32e8csm6050818wrs.35.2023.03.12.15.10.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 12 Mar 2023 15:10:05 -0700 (PDT) Message-ID: Date: Sun, 12 Mar 2023 22:10:04 +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: 7bit X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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. 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); } Regards, Jonny