From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 83F93385B800 for ; Mon, 20 Feb 2023 12:00:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 83F93385B800 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-x429.google.com with SMTP id t15so994000wrz.7 for ; Mon, 20 Feb 2023 04:00:07 -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=lFDNDkPjyls5aQGmSoMXXgr3mdN72GpA1Jq4QYrKQKk=; b=E0bMAoUZFJNS4vE0RAa0C75P3NrNXfs4Myx3xadEm1CwDXevN2/xw7YsykWwkvu8w1 GhJffxl69/G5GEG0HU0wLuLEkiKPk4dZSVfOxG7YvY/kxWZ7KwkfRNzHtmGT5qr7Icgz UBjYySMUqVk3ZQglbZXFInjEg426OWfF6GIwKUC9c1tZnNEJTb6LmgiRFV/Ozwa8HbAt KXh7N+x890UdKGNAoP5ObqMoz4bKFytzbxIj2d/KBjZnTcrvyPGBTBxgpt4uHu/PL+yP dGacDLmJiC5v+eyTkIR6G5SY3Gh+erC+pwniBuGP2lyheNR5PT4007c54V02+vyq/oJ1 /Muw== 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=lFDNDkPjyls5aQGmSoMXXgr3mdN72GpA1Jq4QYrKQKk=; b=hCfDu7j6v9no1pOm2sbWlCKtqsqNvxEQ9dQx7caLUa1E4EtcaWRjJznvJogchyXxN6 PB7evX6lwGF32akTRW0EsX1/Xvj4L9OtAoB1BxeTuglK72noItlYWsb4doSpZMnO1jj5 N0gtdIxmljvFVQI9yZZHmKt3u5hY/EvWLrQCZ61zfMjdwl7B7ad0UjKEEyDoDqOa8HSS 3Gq1x2vQEmNegyu0qUQrOoyqNxDU1qHFRFgAtETtdlPSe/cV0Ky6LwUGAaYdVjggA0Os jFco16Bna1vBL+qb+cvMalVDU/ZHrFdZw1DNzaFjrxbi/HKV8kEkKumO7fjFVjmi3evP 1lHQ== X-Gm-Message-State: AO0yUKXTNEUiaTlIFpogDJo7fNHJpGhzdM/O2V3SxSWC5HWspXvz+icg FiB3C4HpFIktceRawpRcN60xzw== X-Google-Smtp-Source: AK7set/c95PpQzsYphalaPetvicQ6HH7xM5p/wdGyqP/YZr6lXS4bQ1AkWQypLk4aMEyzjbrOwK0Tw== X-Received: by 2002:adf:decf:0:b0:2c5:98f2:5b1d with SMTP id i15-20020adfdecf000000b002c598f25b1dmr1819693wrn.63.1676894406148; Mon, 20 Feb 2023 04:00:06 -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 b13-20020a056000054d00b002c546d169ddsm2351739wrf.11.2023.02.20.04.00.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Feb 2023 04:00:05 -0800 (PST) Message-ID: Date: Mon, 20 Feb 2023 12:00:05 +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: Segher Boessenkool , Marc Glisse via Gcc-help Cc: Gabriel Ravier , Marc Glisse , Xi Ruoyao , Jonathan Wakely References: <6b530d67-723a-a0c9-15bc-12b7341653a7@jguk.org> <96f99315a6ffd3dd3919b23a4ade2597747a580a.camel@xry111.site> <1e62a6f51b748454abe88438f2040486722c5803.camel@xry111.site> <7e54f0b6-0c58-e340-46a3-b46fa321a201@inria.fr> <20230220112838.GO25951@gate.crashing.org> From: Jonny Grant In-Reply-To: <20230220112838.GO25951@gate.crashing.org> 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 11:28, Segher Boessenkool wrote: > On Mon, Feb 20, 2023 at 12:18:36PM +0100, Marc Glisse via Gcc-help wrote: >> On Mon, 20 Feb 2023, Gabriel Ravier via Gcc-help wrote: >> >>> This is the kind of thing that makes me wonder why there isn't some kind >>> of `__builtin_unreachable_do_not_optimize()` builtin that allows one to >>> mark places in code that should never be reached and should thus be warned >>> about if such a thing happens while at the same time never doing any >>> optimization on the basis of the presence of the call. >> >> -fsanitize=unreachable -fsanitize=null and others prevent the kind of >> optimization you are worried about. > > Or even just __builtin_trap(), or abort(), or similar. Just a printf() > thing if you really want to just warn. __builtin_trap() gets compiled so there isn't a symbol (unless I wrap it) pushq %rbp movq %rsp, %rbp ud2 I made an example which does create a string with the file and line number (which would aid checking assembler output for null dereference issues), but oddly the optimizer doesn't remove the string, as that code is never reached. I'm probably misunderstanding I guess. .LC0: .string "/app/example.cpp:8" https://godbolt.org/z/Yzqb5ov6M #define xto_str(s) to_str(s) #define to_str(s) #s void f(const char * str) { if(nullptr == str) { __builtin_printf(__FILE__ ":" xto_str(__LINE__)); } __builtin_printf("%c", str[0]); } int main() { const char * a = "a"; f(a); } > > "Never doing any optimisation" based on is of course not a > reasonable expectation; but you *can* ask for reachable code not to be > optimised away. This is the default, just don't mark reachable code as > unreachable :-) > > > Segher