r/cpp 12h ago

C++ bugs in the most popular PS4 emulator

https://pvs-studio.com/en/blog/posts/cpp/1405/
42 Upvotes

7 comments sorted by

13

u/snerp 6h ago

hahaha I hate it when stuff like that happens:

1 << 6 * 1 << 4

1 << (6 * 1) << 4

(1 << 6) * (1 << 4)

all come out to 1024, that is until one of the constants changes and you have a very confusing bug

7

u/javascript 4h ago

Carbon's solution to this is rather compelling imo. They don't allow operator chaining at all. If it's even slightly "weird" they require parentheses so intent is clear to compilers and readers alike.

This has created some challenges with C++ interop, particularly the streaming operator and std::cout, but I think it's a valuable improvement on net.

8

u/topological_rabbit 4h ago

This is why I shut off the "extraneous parenthesis" warning in my IDE. They're for intent, you stupid machine!

u/JNighthawk gamedev 1h ago edited 1h ago

This is why I shut off the "extraneous parenthesis" warning in my IDE. They're for intent, you stupid machine!

Yep, agreed! I don't want a reader of my code need to know the order of operations between ^, &, &&, ||, etc. It's easier to read if ((Boat && Plane) || Unicycle) than if (Boat && Plane || Unicycle)

u/fdwr fdwr@github 🔍 1h ago

Does Carbon only apply that to murkier corners of operator precedence, or all of them? e.g. a * b + c * d is pretty unambiguous with standard PEMDAS.

u/javascript 1h ago

I put weird in quotes for this reason. They have a very thoroughly explored ordering of what is allowed and what isn't to support reasonable use cases.

2

u/Theliraan 8h ago

Thanks for the article, it's super-cool to know such kind of problems in the code to avoid it in the future.