Hacker News new | ask | show | jobs
by ndriscoll 28 days ago
You know that compilers can elide checks if it's a provably impossible case, right? So they could have checks in both places in the source code. Or I know that I was using static_assert in C on sizes of buffers/messages in my parsers for fibre channel firmware at IBM 10 years ago, and I'm like 99% certain clang static analyzer would do that kind of "what if this is too small" analysis across functions back then. You can also use e.g. phantom types or just like... structs/classes to enforce invariants (e.g. that it's been checked; your check four layers up could've been an inlined constructor of a trivial struct doing validation) purely at compile time. You can't infer what the actual source looks like at all based on disassembly.
1 comments

If you don't have a check in function A, but multiple functions away in the stack there is a check, I guess that could be a compiler-enforced thing. But it doesn't feel like one to me. Maybe compilers have just gotten better since I wrote C++/C code, and now that sort of pattern can be enforced.

ETA: Here's GLM 5.3's opinion, also posted above. https://gist.github.com/matthewdgreen/38430b5e91780b70fdb5e9...

That LLM seems to rather mindlessly reproduce the Rust marketing idea that it is basically impossible or a huge effort to write safe C code. But yes, compilers are also really good at removing run-time checks also across function boundaries. I am experimenting with the idea to use exactly this to prove static safety of C code (https://uecker.codeberg.page/2026-05-22.html) and this turns out to be quite promising.
Could be macros or forced inline helpers that get copy/pasted everywhere. It may be a more pleasant interface for engineers working with it.
> The destination turned out to be a heap allocation sized exactly for the validator's cap: a 24-byte header plus the 2048-byte maximum that handler is allowed to store. Exact fit. That's not compiler tooling — no compiler infers "this attribute's cap is 2048 and the buffer is 2072."

I haven't done C or C++ in a decade, but the fact that it's always allocating a max sized structure makes it sound like they just have a struct Thing { struct Header hdr; char[Thing::max_size] buf}, and then they are just doing `new(struct Thing) `? Then elsewhere they can just check that hdr->len < sizeof(Thing::buf) or whatever.

Like this doesn't sound some like profound memory kung-fu. It sounds like they're keeping it simple (and maybe better for fragmentation anyway) by always doing the statically defined max, and then they have some (probably generic) validation layer that checks the descriptor before copying.

Like the last time I did any C++, I was playing with statically allocated object pools where the "pointers" you got from allocating out of the pool knew the pool's size and which pool they belonged to at compile time. Their runtime representation was just whatever the smallest int was to be able to index the pool. Literally nothing else. It would no doubt look like dangerous magic if you decompiled because all of the checks were compile time. This, again, was over a decade ago, and I was actually quite inexperienced at the time. I'm sure I could go much fancier these days.