From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id 41F0D3858D1E for ; Sat, 4 Mar 2023 15:00:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 41F0D3858D1E 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-x329.google.com with SMTP id bg16-20020a05600c3c9000b003eb34e21bdfso5639465wmb.0 for ; Sat, 04 Mar 2023 07:00:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk.org; s=google; t=1677942002; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=l/mTG3YA7Zv8r5xzBgPqqSBqQofIt/BC0GX7efRk+qc=; b=ZjJubOWiFHM+MayRx9mA9eJBTkektr7r6a2/KwBMOlGPcEPXSA1JRRzGfKeDlRMFeG 2UqpoUc8VlUev1xleBDV7zM/oi1T0+KXX/qoVny/5guD6WEOqZ1e3obPA1Qd/7wQP0r6 fhQ/NUA81kK1qUE9qx1B3Io2jcbUUQEnti2FHAArPAqsfXUrGJcmbcVAdioG9dXkPoyS SJ+m+vaJWEUQa49t08is7+Zimsd0qVziwmwKEu1gcMxolz9+GbkADcd27e1baD3eESqQ flr5CmKy+oydGpseCS+1teW4i7sWTkTXNY7WA0BJcQqMkSp6ngyYPJYOrhV8S2jY/IGM mKgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677942002; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=l/mTG3YA7Zv8r5xzBgPqqSBqQofIt/BC0GX7efRk+qc=; b=33KnMmjw2BVuV374+PDeSnjCuz2z0QLNo1uJUaxIP3JPhY4D+CCwDH6uRRsZxs6YIb 4g2wa7g8NkWsvIc64BVmA/qDPBMYF1ajpHSmudlAbcuhNNsbFny2tFVNUHoya1mkzdqi jAasSF5BYbdJDWh2ifQWrl3DEwfWAd1MU+C6Vfpf4y5mBdo1vhYfFSFm0PngKBZt+VYH IXNAVCYDd1OeELhH/FKy8UvINw+8AZrKKQecGhA3x2lZzQzG1loMjcnKnCPoDNrte3Ms y/B7ENFUE/pI8odzeWhdyG1A00kkDXpwW9pD5Gaaaae11JPLKvLOWGkgy1caG/Ak1qRE gG3w== X-Gm-Message-State: AO0yUKWL2/kJ2zeR8A662c5VhoBRY+p0LOQiwboe98l8Cve6fAk67D9l YJTHqauEYz4tQhNhI/m7uGuGQA== X-Google-Smtp-Source: AK7set8s9pBQW6njirpMwMUu10qDnOjq3b5Fbxq9dKtSsuItdnsVL80FQ+xHYXrDB0GRDsZbEu9w5g== X-Received: by 2002:a05:600c:3148:b0:3eb:3fea:a2ac with SMTP id h8-20020a05600c314800b003eb3feaa2acmr4845283wmo.22.1677942001880; Sat, 04 Mar 2023 07:00:01 -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 v2-20020adfe282000000b002c7066a6f77sm5148185wri.31.2023.03.04.07.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 04 Mar 2023 07:00:01 -0800 (PST) Message-ID: Date: Sat, 4 Mar 2023 15:00:00 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 From: Jonny Grant Subject: Re: std::string add nullptr attribute 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> Content-Language: en-GB 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 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. > >> 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. Well seeing something like this would be useful at build time for me: app.cpp: In function 'int connect_usb()': app.cpp:16:7: warning: unhandled throw std::logic_error("my string: construction from null is not valid") It's true it's very difficult to handle all uncatched exceptions. At least the software might restart that module (maybe using a watchdog if it finds that module unresponsive for a number of seconds, no heartbeat etc) after informing the user of an issue (my example would be when an unsupported USB device is connected to something in an embedded system, a car infotainment system - driver fails, but at least it could be restarted by the software), if we had the ID of the USB device, we can keep it in a forbid list, so it doesn't try to use it again. Otherwise the software would keep crashing in a multiple times while such a device was connected if that device wasn't forbidden. Anyway, this is all automotive specific. There's a standard committee paper "Unconditional termination is a serious problem" P2698R0. Not exactly the same, but it's a similar topic. Kind regards Jonny