M2-Planet
M2-Planet copied to clipboard
`&&` and `||` do not short circuit
From known_issues.org:
Both sides of && evaluate because it hasn't been shown to be worth the effort of implementation of short-circuit logic
Example of failing code
#include <stdlib.h>
#include <stdio.h>
int boom()
{
exit(EXIT_FAILURE);
}
int main(int argc, char* argv)
{
if((0 == argc) && boom())
{
fputs("impossible code\n", stderr);
}
return 0;
}
Work around code
#include <stdlib.h>
#include <stdio.h>
int boom()
{
exit(EXIT_FAILURE);
}
int main(int argc, char* argv)
{
if(0 == argc)
{
if(boom()) fputs("impossible code\n", stderr);
}
return 0;
}