From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 2B0D03858C83 for ; Tue, 21 Feb 2023 15:04:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2B0D03858C83 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-x32f.google.com with SMTP id p26so3472128wmc.4 for ; Tue, 21 Feb 2023 07:04:34 -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=vH1jTdThv50NSwHfiIvfR1Yc0feM5junULRu5XrBWsw=; b=IzTEHLeLaATDKoB2F+0UmFXNG0Bf6EAyYph9lnY2Knf4J3yoqyPuXttkUneS+s3CO9 5NFf1qiTS+8CUlTJZj6axJRdPDmB58eQL6fqGmEpwI1/2F5xoj0jTjWG0EBzYsES7tjl i7Q1pJfZldLMIq0NpiTvGQGu5RZ1bKj2jr2JiJ0q5F2Uofcro51fo4dbJbzuuQ2YDXIS OGFstfKpeq0fh3gEGf7x+SxKf4p51gEH4axlAgPgHILSmqmqmdcXpCkMZHiCDWPI3dxr R3hvPNtiRiaMPBDTyM40OrqtTFfNmCRoNtiZ/bxlFd7JELAJxzeaslOGQrVLtbfUwET0 ZjfA== 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=vH1jTdThv50NSwHfiIvfR1Yc0feM5junULRu5XrBWsw=; b=gCkEB/lD7porf9Q6zMjrh3quhQnB5ZhBGUAnolP/mnmdgvOO+HM+29MonJuk63Zdn/ vNHl6vs/1cZ5Xm28JwdDUKOnVNNSeZlZr7vnpNYJ9njP4AcxjkZMbwhC5CaXy/RGIs+K A/+Ko3RnIshgEltaNGREPh1QVXbNpl0Ge1ZuTZQkAD9kABxnXwMrSOKSQd+kVScjgSYG WjmT2hwAR5MgzgsQDOZm3s+j4XWJuwB0Uo77WYm3xA2sqYzzRe+ngqKowNCSiJ5GFJnl MahxmsMeXU/2Y2RuFJg7Bs7w2y7zVJ13UoaG/meXda5bO4YGeaCsT/2eFXGSGwBwBTzc vjbw== X-Gm-Message-State: AO0yUKVpbDw2CAgOt9e0L+OpO18DHIhlGzURXmVuCqvGUWmJOBRg3awW d8No6sd2x1GV1dVIFvkZvuGMAw== X-Google-Smtp-Source: AK7set+J5UiE2TzqHxj02OE3lVuS+tX3LkH7vVY5aRskfD88Y68/VHzVgieSDk0IGbKxRZiKWNoNYw== X-Received: by 2002:a05:600c:130f:b0:3df:57aa:db52 with SMTP id j15-20020a05600c130f00b003df57aadb52mr3705101wmf.4.1676991872878; Tue, 21 Feb 2023 07:04:32 -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 26-20020a05600c229a00b003dc49e0132asm4602080wmf.1.2023.02.21.07.04.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Feb 2023 07:04:32 -0800 (PST) Message-ID: <7e75ed38-6bb0-1547-a961-06126b47aec0@jguk.org> Date: Tue, 21 Feb 2023 15:04:31 +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: Xi Ruoyao , Jonathan Wakely Cc: 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> <96f99315a6ffd3dd3919b23a4ade2597747a580a.camel@xry111.site> <163945d9-6c24-d4e1-7029-980b988bd634@jguk.org> 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,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 20/02/2023 12:59, Xi Ruoyao wrote: > On Mon, 2023-02-20 at 11:30 +0000, Jonny Grant wrote: > >> Thank you for the suggestion, I gave that nonnull attribute a try, but >> it doesn't appear to warn for this example. >> >> https://godbolt.org/z/boqTj6oWE > > Ouch... The optimizer inlined make_std_string so both -Wnonnull and - > fanalyzer fails to catch the issue here. > > Adding noipa attribute for make_std_string will work, but will also > cause the generated code stupidly slow. Maybe: > > #ifdef WANT_DIAGNOSTIC > #define MAKE_STD_STRING_ATTR __attribute__ ((noipa, nonnull)) > #else > #define MAKE_STD_STRING_ATTR > #endif > > std::string make_std_string(const char * const str) MAKE_STD_STRING_ATTR; > > It still looks very stupid though. Hi, thank you for your reply and investigation. I raised this issue here that you spotted https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108871 > >> Feels useful to get build warnings if compiler knows nullptr is going >> to be dereferenced, as clang does. > > The problem is in this case nullptr is not dereferenced, at all. So if > we want a warning here we'll have to invent some new __builtin or > __attribute__ to give the compiler a hint. AFAIK there is no such > facility now. > > And you cannot simply justifying to add a new facility because "I feel > it useful". Generally you need to show the benefit will be at least > equally great than the maintenance burden introduced into the GCC code > base. And unfortunately usually we can only measure the burden after > really writing all the code... So it's not easy to convince someone to > develop such a new feature. > >> Personally I feel runtime should equally handle possible nullptr by >> constructing strings in a try catch block so any exceptions are >> handled or logged at least... > > A portable runtime should not assume std::string(NULL) will raise an > exception because other C++ standard libraries may behave differently. > The portable solution is to make a wrapper around std::string > constructor and check if the parameter is NULL. > >> Personally I would be pleased if GCC had a warning I could enable to >> report any logic_error exceptions it knew would execute. > > Or maybe when a program will definitely raise an uncatched exception. > But is the feature really useful? This will not happen for anything > other than simple toy programs. >