...
In this noncompliant code example, function increments the value referenced by its argument. It also ensures that its argument is not a null pointer. But the pointer could still be invalid, causing the function to corrupt memory or terminate abnormally.
Code Block | ||||
---|---|---|---|---|
| ||||
void incr(int *intptr) { if (intptr == NULL) { /* Handle error */ } *intptr++; } |
...
This incr()
function can be improved by using the valid()
function. The resulting implementation is less likely to dereference an invalid pointer or write to memory that is outside the bounds of a valid object.
Code Block | ||||
---|---|---|---|---|
| ||||
void incr(int *intptr) { if (!valid(intptr)) { /* Handle error */ } *intptr++; } |
...