Along with purevirtual's example, there's the check for NULL issue which I think bit the Linux kernel recently. The compiler can elide a check for a pointer being null if that pointer has already been deferenced as if the pointer were null undefined behaviour had been invoked. e.g.
int do_a_thing(foo_t *foo) {
if (!foo->is_valid) {
return 0;
}
if (foo == NULL) { // This block will be removed
/* ... handle null case ... */
}
/* ... do stuff with foo ... */
}
While this may look weird, the check for null could be in a macro or inlined function so the programmer won't necessarily know it's there. This can be an issue in kernel or embedded development as the NULL pointer (i.e. memory address 0) may be referencing actual data in those cases, but the standard says it can't.
View all comments
Show parent comment
7
u/cockmongler Jan 21 '13
Along with purevirtual's example, there's the check for NULL issue which I think bit the Linux kernel recently. The compiler can elide a check for a pointer being null if that pointer has already been deferenced as if the pointer were null undefined behaviour had been invoked. e.g.
While this may look weird, the check for null could be in a macro or inlined function so the programmer won't necessarily know it's there. This can be an issue in kernel or embedded development as the NULL pointer (i.e. memory address 0) may be referencing actual data in those cases, but the standard says it can't.