What every C Programmer should know about undefined behavior #2/3
What every C Programmer should know about undefined behavior #2/3
Posted May 17, 2011 5:36 UTC (Tue) by gowen (guest, #23914)In reply to: What every C Programmer should know about undefined behavior #2/3 by cmccabe
Parent article: What every C Programmer should know about undefined behavior #2/3
I don't think that word means what you think it means. Clue: It doesn't mean "someone who doesn't share my opinion".
> 1. Initializing something before checking if it's NULL has nothing to do
> with whether you decide to put declarations at the start.
Combined with a coding rule that variables must be initialised when declared, it really kind of does. And thats a common coding rule, because using an unitialised variable is - surprise - undefined behaviour. The vertical space between a variables declaration and its initialisation represent a region in which using that variable is a bug. It is good practice to keep that space as small as possible ("but no smaller" being the extra advice thats forgotten in this case). Yes, its not an unavoidable bug. But its an unnecessary vector for bug transmission.
So, as mentioned above this clash of good advice - or rather, a slightly blind application of usually-good advice - combined with C89's archaism on variable declaration resulted in a bug.
For the recent Linux hole, why do *you* think the (presumably experienced) coder initialised the variable on declaration - which sadly preceded the NULL check?
> 2. C99 doesn't have a "declarations go at the start of the block rule."
I did actually mention that. So zero out of ten for reading the whole post.
