From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 5E3E6384640D for ; Wed, 16 Jun 2021 13:01:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5E3E6384640D 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-x42c.google.com with SMTP id y7so2579090wrh.7 for ; Wed, 16 Jun 2021 06:01:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk.org; s=google; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=KE1D7eARAl3lceuJ8hK8ZVADw3rFvDBN2zorIfZv784=; b=NnbS7gOXpHfTG+7i84L4xMmwyzpuXkpmrGfBK4pqA4HtzlW/f0U8viIIpXgdwV9BHA NpHGCWf1gTNMLG3AH/Lu8Duax0roxbZHEE0IGCfSuOgawyHvkfHHthG80cG+HMEp8IbF eIkknDi0OxKl1dnpSp3gTUrdhpiXwN1e1mhkSNJUnzGDNEuMHu7adOdMpwv5eo1RBZTG a6yKUvd716+C+46VYrUGAO7HWmnE6jWlIe/X7CaTeBmKjNRGNMWcim8fdHLFbTyb8nwL 5ehk4dLwPAbJPI5DqHlED5nzzIE/iiAibevegDWBhlYmUKQ4Df5ZzV20s99vdzwS8lrC YaZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=KE1D7eARAl3lceuJ8hK8ZVADw3rFvDBN2zorIfZv784=; b=ixw44OZL9dt1M2gh7SHBGpzl8on2bITK5VcgaAJLw+wmwaRckS3ojgtRNPUQy5kcuW UnL02vnFyvErKqUcscFAgzK4j3tTDTx8+gA9Ysl+4jijRNE7LO6+FPkfISwH3qk0BuRs +RIw1GAIyFHLqeNGNFVbhCBAP92QsfcTiGnMxChp0TAwiRKvyblliX2WZAtycurKJAX9 NuNCeoRUFXRmrrbGZ/0FYIV3lHzDMYqbJ5XqWWdX6/CVqn5LjJ7BHPC07TS0cP9UD21x gqu0Z5s/+WP/ihG18bA6646HpDHUZ5yoLOo/C0Y9Yh+bBTj61YpIX4NFnqKbwePYCxRd jpBA== X-Gm-Message-State: AOAM532DPNjcROGEQskGRbuBB7E/5ik1OnW+dRAUgZ3ZPEP5obYNhFhF 1CC2pGC1U0AsFeB1LOuxCo81GzLwdbDfYQ== X-Google-Smtp-Source: ABdhPJz34pTU/ujdgoi0M8FKXK27zuTh8nezMSBAQv7XJcVRfo71MsRnwQteVuVt+ZB3Ge1BOqdllw== X-Received: by 2002:adf:c393:: with SMTP id p19mr5349912wrf.92.1623848466445; Wed, 16 Jun 2021 06:01: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 62sm2218046wrm.1.2021.06.16.06.01.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Jun 2021 06:01:05 -0700 (PDT) Subject: Re: gcc warn when pointers not checked non-null before de-referencing. To: Xi Ruoyao , gcc-help References: <0a9ccbb7-135a-b342-e5cb-35b7c6a44a00@jguk.org> <97eb7315fd136ff8a818925b1704760a856ffe64.camel@mengyan1223.wang> From: Jonny Grant Message-ID: <0770e060-6388-fc27-1178-205b867bfae2@jguk.org> Date: Wed, 16 Jun 2021 14:01:05 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <97eb7315fd136ff8a818925b1704760a856ffe64.camel@mengyan1223.wang> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, BODY_8BITS, 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Jun 2021 13:01:09 -0000 On 14/06/2021 06:15, Xi Ruoyao wrote: > On Sun, 2021-06-13 at 23:30 +0100, Jonny Grant wrote: >> Hello >> >> This isn't real code, just an example to show. >> >> I've tried with:  -Wall -Wextra -O2  and some other warnings, but >> couldn't get this to generate a warning that *g was possibly de- >> referenced. May I ask, does GCC have a way to get warnings when pointers >> are not checked? >> I had a look but -Wnull-dereference didn't help. >> >> #include >> >> #include >> void f(int * g) >> { >>     *g = 1; >> >>     if(NULL == g) >>     { >>         exit(1); >>     } >> } >> >> Best regards Jonny > > It was explained by Chris Lattner at > http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html > > GCC (<= 4.4) had -Wunreachable-code which might work for this case. But > it was too unreliable (as Chris said, generally there is no reliable way > to do this) and removed in later releases. > Thank you for your reply and the link. And for Martin's reply. I guess a separate static analyser would do it, GCC is more focused on compilation so I shouldn't ask for it to have so many features it can't support. Chris Latner also mentioned integer overflow being undefined, that crops up too. There's no easy solution right, we need to hand write code the checks? It's human-error prone if we need to manually code each check. throwing in C++, or handling in C. if(N >= INT_MAX) { throw std::overflow_error("N >= INT_MAX would overflow in for loop"); } for (i = 0; i <= N; ++i) { // ... } Cheers, Jonny