Some Dark Corners of C

24
Some dark corners of C -std=c99 -pedantic -Wall -Warn-me-harder Stolen from Rob Kendrick <rjek+dark [email protected]> Rob Kendrick <[email protected]>

description

Secrets of C, how to cheat through it.

Transcript of Some Dark Corners of C

Page 1: Some Dark Corners of C

Some dark corners of C-std=c99 -pedantic -Wall -Warn-me-harder

Stolen from Rob Kendrick <rjek+dark [email protected]>

Rob Kendrick <[email protected]>

Page 2: Some Dark Corners of C

Dark corner of C

Here it is!

Page 3: Some Dark Corners of C

Shadowy escape

Returns 3840 Returns 42

Page 4: Some Dark Corners of C

Bizarre keyword reusevoid foo(int x[static 10]);

Passed array must be at least 10 entries.

void foo(char x[static 1]);

Free compile-time non-NULL check!

Page 5: Some Dark Corners of C

Warning: Don't breathe this

Yes, that's right, it makes demons fly out of your nose!

(On my system, 1,179,602,721.)

If you do this, I will find you.

Yes, single quotes.

Page 6: Some Dark Corners of C

[ ] is just + in disguise

Page 7: Some Dark Corners of C

Pointer aliasing

Compiler doesn't

know that *z won't also be *x, so loads it twice :(

Page 8: Some Dark Corners of C

Pointer aliasing: Fixed?

Only one load, at the

cost of beauty.

Page 9: Some Dark Corners of C

Pointer aliasing: Fixed!

'restrict' means we

promise not to mess about.

Page 10: Some Dark Corners of C

Counting up ...

Page 11: Some Dark Corners of C

... vs counting down

Page 12: Some Dark Corners of C

const confusion

"Constant" integer foo with value 10.And a "constant" pointer to it?

Oops. binds left. But if there's nothing to its left, it binds to the right.

Page 13: Some Dark Corners of C

Munchy munchy.

The C specification says that when there is such an ambiguity, munch as much as possible. (The "greedy lexer rule".)

Page 14: Some Dark Corners of C

Munchy munchy. Again.

Alas, not

But

Parser go boom.

Page 15: Some Dark Corners of C

C keywordsauto break case char const continue default do

double else enum extern float for goto if int long register restrict return short signed sizeof static struct switch typedef union unsigned void

volatile while

Wait, what? is a bizarreness left over from B. The only place it's valid, it's also the default. And that's to say a

variable should be automatically managed, ie, placed on the stack. is its antonym. Ish.

Page 16: Some Dark Corners of C

Smallest C programWhat's the smallest C program that will compile and link?

(Don't run this.)

Alas it produces a warning. So we can do this:

Page 17: Some Dark Corners of C

Global variables are filthyfoo1.c:

foo2.c:

Page 18: Some Dark Corners of C

Global variables are filthyfoo1.c:

foo2.c:

Page 19: Some Dark Corners of C

Global variables are filthyfoo1.c:

foo2.c:

Page 20: Some Dark Corners of C

// comments are evil

C89: 5 C++: 10 C99: 10

Page 21: Some Dark Corners of C

Portable lossless floats

Page 22: Some Dark Corners of C

Ruin somebody's dayWhat would you sneak into somebody's headers to drive them mad?

No valid C program will fail to compile with these.

Page 23: Some Dark Corners of C

Recommended reads

Sadly very few modern books on the subject of C :(

Page 24: Some Dark Corners of C

Thanks for ideas/input...David Thomas

Daniel SilverstoneClive Jones

Peter Van Der LindenErlend Hamberg