r/programminghorror • • Oct 26 '26 • Popcorn 1 Bravo 1

c Using garbage values to generate pseudorandom numbers

Post image

Thing I came up with instead of paying attention to class today.

I tested it on this computer (windows 11 if it matters), and the first time it's ran, an actual "pseudorandom" number is returned and the following calls return 1.0 always.

2.9k Upvotes

152 comments sorted by

View all comments

Show parent comment

295

u/[deleted] Oct 26 '26

[removed] — view removed comment

0

u/[deleted] Oct 27 '26

[deleted] — view deleted comment

31

u/ttlanhil Oct 27 '26

Only if you assume that it'll do something sensible.

You've asked for undefined behaviour, so it's perfectly fine for the compiler to replace your function with return 4 or 4.0

0

u/[deleted] Oct 27 '26

[deleted] — view deleted comment

21

u/Imaginary-Jaguar662 Oct 27 '26

That sounds logical.

But the operation is undefined, and as soon as optimizer gets to it the code will probably not compile as a "make a function call, do a memory read, do a cast, do a floating point division".

If you're lucky, it might get inlined as constant 0.0. It might also get inlined as NaN. Or it might get inlined as "return whatever happens to sit in convenient register right now as double".

Historically it gets inlined as "if this is QA build, return rand(). Else determine this result can be anything, so caller might hit error path. Error path executes fast and takes little RAM, so hardcode error path to caller"

12

u/vip17 Oct 27 '26

undefined behavior means anything can happen, including nasal daemons flying out of your nose

0

u/[deleted] Oct 27 '26

[deleted] — view deleted comment

10

u/vip17 Oct 27 '26

In assembly yes. But this is C or C++ code and those compilers have to conform to C or C++ standard

0

u/[deleted] Oct 27 '26

[deleted] — view deleted comment

7

u/ttlanhil Oct 27 '26

The Standard that defines how the language works.

It includes a bunch of things that result in undefined behaviour - and if you do any of them, it's valid for the computer to do anything, including delete your operating system and make demons fly out your nose.

Just because you can often get away with it doesn't mean you should (compilers can do anything including what you might actually want, but the next compiler update might break it)

→ More replies (2)

3

u/Ecstatic_Student8854 Oct 27 '26

This looks like C code. Undefined behavior in C is, well, undefined. The compiler is perfectly within its rights to emit any code. It probably won’t, but it could.

It would be perfectly valid for the compiler to, say, exit execution entirely. For it to replace the rest of the program’s execution with a shell, and indeed for it to just return the integer 4.

2

u/Tysonzero Oct 27 '26

That 4 is perfectly valid as it was chosen by a fair dice roll.

14

u/Top-Opportunity-5418 Oct 26 '26

Can someone explain

56

u/headedbranch225 Oct 27 '26

Recently emudhra had their private keys leaked (well cracked) and one of them was 4

https://scotthelme.co.uk/eight-bits-of-trust-emudhras-ca-keys-cracked-in-milliseconds/

A good writeup that I saw, it explains it quite well if you want to learn more

It happened the same day as the PHP vuln, I have no doubt if the PHP vulnerabilities hadn't happened at the same time it would be so much more widely covered, but if you see a much higher than usual number of references to 4 that is why

1

u/cheesengrits69 Oct 29 '26

The ol' "It's so dumb nobody would ever guess it" method

54

u/iiznobozzy Oct 26 '26

2

u/Glum-Objective3328 Oct 27 '26

Maybe a peak for relevant xkcds

1

u/Alfika07 Oct 27 '26

This panel came out in 2007. How did Randall Munroe predict the eMudhra leak?

3

u/Adventurous_Mail6397 Oct 27 '26

A Certificate Authority accidentally placed their test Private Keys into a key pool. One of the private keys was just 4.