Type safety could prevent nuclear war

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 4 15:10:23 PST 2016


On Thu, Feb 04, 2016 at 10:57:00PM +0000, tsbockman via Digitalmars-d wrote:
> The annual Underhanded C Contest announced their winners today.
> 
> As always, the results are very entertaining, and also an excellent
> advertisement for languages-that-are-not-C.
> 
> The first place entry is particularly ridiculous; is there any modern
> language that would make it so easy to commit such an awful "mistake"?
> 
> http://www.underhanded-c.org/#winner
> 
> Actually, I'm surprised that this works even in C - I would have
> expected at least a compiler (or linker?) warning; this seems like it
> should be easy to detect automatically.

The C preprocessor accepts all sorts of nasty, nonsensical things. For
example, the following code compiles and runs (without any warning(!) on
my Linux box's standard gcc installation), and prints "No":

	#include <stdio.h>
	#define if(a) if(!(a))
	int main() {
		int i = 1;
		if (i == 1)
			printf("Yes\n");
		else
			printf("No\n");
	}

Imagine if this nasty #define is buried somewhere under several layers
of #include's.

I'm pretty sure somebody can also concoct some nasty #define that will
break the standard #include headers in horrible ways by changing the
semantics of certain supposedly-built-in constructs.


T

-- 
Mediocrity has been pushed to extremes.


More information about the Digitalmars-d mailing list