From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mengyan1223.wang (mengyan1223.wang [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id A34D6384000E for ; Tue, 6 Jul 2021 15:40:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A34D6384000E Received: from [IPv6:240e:35a:1098:c700:dc73:854d:832e:3] (unknown [IPv6:240e:35a:1098:c700:dc73:854d:832e:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@mengyan1223.wang) by mengyan1223.wang (Postfix) with ESMTPSA id 4BB6F66232; Tue, 6 Jul 2021 11:39:55 -0400 (EDT) Message-ID: <1976cf6269261bb38cce5b3a8f59a681e0bc2444.camel@mengyan1223.wang> Subject: Re: gcc warn when pointers not checked non-null before de-referencing. From: Xi Ruoyao To: Jonny Grant , gcc-help Date: Tue, 06 Jul 2021 23:39:38 +0800 In-Reply-To: <45a96f3c-5058-e8c4-08f0-d0c62fb27f1c@jguk.org> References: <0a9ccbb7-135a-b342-e5cb-35b7c6a44a00@jguk.org> <97eb7315fd136ff8a818925b1704760a856ffe64.camel@mengyan1223.wang> <0770e060-6388-fc27-1178-205b867bfae2@jguk.org> <4dd0f2168668d9d3dd919df6088d0dea4cfe0bb5.camel@mengyan1223.wang> <45a96f3c-5058-e8c4-08f0-d0c62fb27f1c@jguk.org> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3031.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, JMQ_SPF_NEUTRAL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Tue, 06 Jul 2021 15:40:03 -0000 On Sat, 2021-07-03 at 16:36 +0100, Jonny Grant wrote: > > > On 16/06/2021 14:36, Xi Ruoyao wrote: > > On Wed, 2021-06-16 at 14:01 +0100, Jonny Grant wrote: > > > > > 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) > > > { > > > // ... > > >  } > > > > For debugging use -fsanitize=undefined. > > > > And this is buggy anyway, no matter if there is an UB: > > > > for (unsigned i = 0; i <= N; i++) > >     make_some_side_effect_without_any_undefined_behavior(i); > > > > If N may be UINT_MAX, this is not UB, but a dead loop. Programming is > > just human-error prone, even if you use "some programming language > > claimed to be able to eliminate many human errors" (I'll not say its > > name, to prevent a flame war). > > > Hi Xi > > > Checking the UINT_MAX would at least prevent the continual running of > any such buggy loop where it increments right? and the code within the > loop does not modify 'i' > > for (unsigned i = 0; (i <= N) && (i != UINT_MAX); i++) >     make_some_side_effect_without_any_undefined_behavior(i); Even if i is signed, it will still "work" if you modify the && expression a little: for (int i = 0; i != UINT_MAX && i < N; i++) make_some_side_effect_without_any_undefined_behavior(i); The problem is, now the behavior when N == UINT_MAX is same with when N == (UINT_MAX - 1). This can really puzzle someone who will call your function. If I'm designing this function I'd make it to interpret N as [0, N), instead of [0, N]: // Do something for each integer in [0, N). void do_something(int N) { for (int i = 0; i < N; i++) do_something_once(i); } > Is there any way to have a way to make loop variables like this 'i' > const within the body of the loop, to avoid accidental changing of 'i' > by the body of the loop I don't think there is one in C. Perhaps, maybe use some "nasty" macros. -- Xi Ruoyao School of Aerospace Science and Technology, Xidian University