M2-Planet icon indicating copy to clipboard operation
M2-Planet copied to clipboard

`&&` and `||` do not short circuit

Open gtker opened this issue 11 months ago • 0 comments

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;
}

gtker avatar Apr 16 '25 14:04 gtker