News for hackers. Stuff that matters.
Re:The C++ Iceberg by H8crilA on Wednesday May 08, 2024 @04:33AM (#40295670)
No, it's the former that's more dangerous, and it has been there from the beginning. Every time you dereference a bad pointer (already free'd, corrupted, badly casted, ...), every time you go over the bounds of some array, every time you read memory that hasn't been initialized, every time you (write) access the same memory from two threads without synchronization, etc. - this is the bread and butter of binary exploitation. Exploits are fully expected under C/C++ spec, after all the behavior is undefined.
Re:The C++ Iceberg by flohofwoe on Wednesday May 08, 2024 @04:41AM (#40295717)
But at the same time it is totally expected in C and C++ that range checking is the responsibility of the programmer, and most of those problems are trivial to catch with Valgrind or ASAN. E.g. yes, it is a problem, yes, 70% of all CVEs are caused by memory corruption, but the problem is totally obvious and trivial to debug. It's basically part of the contract when deciding to use C or C++.

Compared to such 'obvious' memory corruption problems caused by invalid pointers or missing range checks, obscure code generation issues caused by UB are much harder to identify and the behaviour may also change randomly between compiler versions - this is the actually scary thing about UB, and it's also a relatively "new" thing.

Re:The C++ Iceberg by H8crilA on Wednesday May 08, 2024 @05:00AM (#40295807)
The point is exactly that "it is totally expected", and it has caused massive losses over the decades. It has been abused for everything ranging from turning your friend's PC off, to ransomware, to state-on-state espionage and even causing nuclear incidents in Iran's enrichment facilities.

The behavior is undefined.

Re:The C++ Iceberg by flohofwoe on Wednesday May 08, 2024 @05:14AM (#40295893)
Sheesh, the sky didn't fall, the world didn't end, and humanity survived. UB won't end the world. Relax.
Re:The C++ Iceberg by H8crilA on Wednesday May 08, 2024 @06:22AM (#40296314)
Perhaps we're not in a discussion that's worth continuing, but I'm talking about programming language design. There are languages with UB, and there are languages without UB - and I think in 2024 the verdict is clear.
Re:The C++ Iceberg by flohofwoe on Wednesday May 08, 2024 @07:28AM (#40296757)
Maybe from your point of view. But from elsewhere UB can be a useful tool because it allows wiggle room for compilers to support a wide range of platforms without sacrificing efficiency. It doesn't have to be as crazy as in modern C/C++ compilers, but some sane UB definitely makes sense (even Rust has that inside unsafe blocks).

One could also argue that most UB in C/C++ should actually be IB, but that's a different discussion.

Re:The C++ Iceberg by tialaramex on Wednesday May 08, 2024 @10:30AM (#40298515)
This "Wiggle room" would be at most implementation defined and in some cases just unspecified values, these probably aren't desirable but yes they're less dangerous than UB.

Rust has UB if you don't obey the rules (Safe Rust doesn't because the rules are not your responsibility in Safe Rust, the unsafe superpowers allow you to do things the enforcement can't check but now all rules are your responsibility) but it's not somehow magically "more sane" than C++ it's ultimately lowering to the same LLVM IR that Clang uses. It's tolerable because you're only confronting the risk when you explicitly choose to do so. If you don't need to squeeze that last few drops of performance you can leave it. If you don't have the confidence, if your experts are too busy to review it, if you haven't had a proper night's sleep, you can write safe Rust today.

Re:The C++ Iceberg by lolc on Wednesday May 08, 2024 @10:53AM (#40298749)
> the problem is totally obvious and trivial to debug.

We live in parallel universes that must have intersected in this thread.

Re:The C++ Iceberg by flohofwoe on Wednesday May 08, 2024 @12:56PM (#40300309)
Debugging tools got a lot better in the last two decades, memory corruption issues and leaks are by far not as scary anymore as in the 90s and early 2ks, for instance check out the realtime memory debugging panel in Visual Studio that's been there since VS2015. Also as mentioned at the top of the thread: compiler warnings, ASAN, UBSAN, TSAN and static analyzers are essential these days when writing C or C++ code.

The only barrier is to actually know about and use those tools in the development workflow.

Rust is still superior of course (for memory safety at least), because it actually prevents most of those issues in the first place, but it's not like the rest of the world has been twiddling thumbs in the meantime.

Re:The C++ Iceberg by Leherenn on Wednesday May 08, 2024 @06:03PM (#40303169)
It certainly got better, but it's still not trivial. With the sanitizers, you need to have code that actually triggers the UB, coverage is not enough. If your UTF-8 parsing code UB only on some very specific input, then unless your tests or own environment happen to have this input, you'll be none the wiser.

Fuzz testing helps, but it's not commonly used in my experience.