gcc warn when pointers not checked non-null before de-referencing.

Xi Ruoyao xry111@mengyan1223.wang
Wed Jun 16 13:36:38 GMT 2021


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).
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University



More information about the Gcc-help mailing list