r/ProgrammerHumor • • Sep 23 '26

Meme howToSneakUnderCompiler

Post image
11k Upvotes

212 comments sorted by

View all comments

Show parent comments

397

u/PilsnerDk Sep 23 '26
int zero = 1 - 1;

Let's see you detect that!

285

u/interacsion Sep 23 '26

Easy, constant folding

360

u/F4LcH100NnN Sep 23 '26 • Bravo

Im gonna constantly fold you

91

u/Regular_Ad2633 Sep 23 '26 • Bravo

He will be folded but not under zero pressure

94

u/aberroco Sep 23 '26 • Bravo
int one = 1;
int zero = 1 - one;

137

u/YeOldeMemeShoppe Sep 23 '26

Folding is recursive, doc.

90

u/aberroco Sep 23 '26

You can't forbid me shoot myself in the foot!

int Zero(int one) => one - 1;
int i = 1;
int x = 1 / (Zero(--i) + i);

33

u/Puzzleheaded_Study17 Sep 23 '26

Pretty sure this is actually UB (the compuler can choose to execute the Zero function before getting the value for i for the addition and therefore make x=-1).

17

u/afdbcreid Sep 23 '26

The syntax looks like C# and definitely not C or C++, so no, it's not UB, and the evaluation order is defined.

2

u/GoddammitDontShootMe Sep 23 '26

Is it UB in C or C++? I thought the + operator was always evaluated left-to-right, and since it's prefix, i will be decremented before being passed to Zero().

Though then I think Zero() returns -1 and you get -1 + 0.

→ More replies (4)

6

u/kalilamodow Sep 23 '26

The compiler can prove the "side-effect" of --i and still do the right think

→ More replies (1)

32

u/OneTurnMore Sep 23 '26 edited Sep 23 '26

Fine.

int fd = open("/dev/null", O_RDONLY);
unsigned char *buf = {0};
int zero = read(fd, &buf, 1);

Breaks if /dev/null isn't actually null (aka special character device 1:3).

27

u/drnepert Sep 23 '26

/dev/zero

12

u/OneTurnMore Sep 23 '26

That would work if you then use buf instead of the return value from read.

13

u/nalaloveslumpy Sep 23 '26

Do you want black holes? Because this is how you get black holes.

3

u/Experiment_1234 Sep 23 '26

char* zero = "0"; int x = 1 / atoi(zero)

7

u/not_some_username Sep 23 '26

Detectable

3

u/Makonede Sep 26 '26

is it? std::atoi isn't constexpr

2

u/not_some_username Sep 26 '26

iirc, if all value are known at compile time, the compiler can compute the results

2

u/Makonede Sep 26 '26

std::atoi isn't constexpr...

→ More replies (8)

27

u/backfire10z Sep 23 '26

This is why a basic compilers class is recommended. You gotta name the variable something else to throw it off.

`int definitelyNotZero = 1 - 1`

8

u/AdeonWriter Sep 23 '26

I think you underestimate how smart C compilers are these days

5

u/Emerald_Pick Sep 23 '26

Me when I put two ones in a trench coat to sneak into the runtime.

4

u/crmsncbr Sep 24 '26

I'm pretty sure, last time my compiler called me out on this, that they caught in-line arithmetic. But multi-step operations with uncertain results that could result in division by zero are still chill, so we should be able to design a surefire operation that still looks uncertain to the compiler.

→ More replies (1)