4 ms·
There's a worse problem, which I've seen in the wild: int some_function(...); int some_other_function(...) { int r = -1; /* imagine
by jwhite 10y ago
There's a worse problem, which I've seen in the wild:
int some_function(...);
int some_other_function(...)
{
int r = -1;
/* imagine a bunch of other locals defined here */
r = some_function(...);
if (-1 == r)
{
return -1;
}
...
}
Now imagine someone changes the return type of some_function():
char *some_function(...);
int some_other_function(...)
{
int r = -1;
/* imagine a bunch of other locals defined here */
char *s;
s = some_function(...);
if (-1 == r)
{
return -1;
}
...
}
Oops! Every other reference to r has been removed except the declaration and the test. Now some_other_function() will always take the early exit, and because r is initialized in its definition the compiler does not complain. (In real life, hilarity ensued.) If it was 'int r;' with no initializer this would have been caught by the compiler.
Modern gcc is great at finding uninitialized and unused variables. -Wall -Wextra -Werror FTW.
EDIT: referred to some_function() where it should have been some_other_function().
- lstamour 10y agoI'm pretty sure static analysis like Clang would warn you about such a thing too... So maybe proficient programmers just need to keep their linters and tools up to date! ;-)