What every C Programmer should know about undefined behavior #2/3
Posted May 16, 2011 15:54 UTC (Mon) by
gowen (guest, #23914)
Parent article:
What every C Programmer should know about undefined behavior #2/3
void contains_null_check(int *P) {
int dead = *P;
if (P == 0)
return;
*P = 4;
}
This one is interesting because (a) something very much like it caused a real security hole in the linux kernel recently and (b) the ONLY reason it exists is because of C's "declarations go at the start of the block" rule.
Someone wants to declare a variable, and knows its good practice to initialise it and, in the interest of style wants to avoid
void contains_null_check(int *P) {
if (P == 0) return;
{
int dead = *P;
*P = 4;
}
}
Result: bug.
C++ (since RAII strongly encourages initialise-at-declaration) and c99 (should it ever catch on) should make this one considerably less common.
(
Log in to post comments)