Hacker News new | ask | show | jobs
by kvuj 13 days ago
Perhaps I'm just used to having programmed in C# professionally for a little bit, but these sorts of APIs where you have to do a calculation to determine the destination buffer size make me feel like a caveman. A minuscule typo and you get a CVE like this.

I know, I know, C is low level and high performance, but surely in the year of our lord 2026, we could have come out with something simpler.

Horrendous is a strong word, you're right.

4 comments

One can abstract buffer handling away in C just fine, eliminating all those problems. I don't know why programmers insist on open-coding low-level buffer management.
Yes, of course, this isn’t C, this is just another case of people writing C wrong.
Perhaps we could use another language, where it's not so easy (or possible) to write it wrong?
It is also easy to write it wrong in other languages. The coreutils rewrite had hundreds of issues, cloudflare brought down half of the internet because in the magic language where nothing can go wrong due to the advanced type system someone used an unchecked .unwrap (but this does not count, because this was Rust and not C - so could not possibly be the fault of the language, so only the programmer is to blame), and just recently Microsoft found a couple of memory safety issues in Rust's standard library. And let's better not talk about the supply chain.
Most of those were crashes. The unsoundness in the standard library is a problem, but I am yet to see a practical impact of them.

Rust people do not shy away from criticism. But rust code typically need not worry about mismatched buffer sizes at library boundaries. Especially when it comes to rce enabling bugs.

And yet, for any of those, the C world still get "why do we still use this language?" comments.
Not even that is usable as excuse, only those that never tried out the systems programming languages that either predated C, or where equally developed during the 1970's outside Bell Labs and UNIX ecosystem.

You could have come with something simpler in PL/I.

To be fair, most advanced languages couldn't be used easily/comfortably on the early UNIX hardware (as in: either they needed so much RAM that you had to save intermediate stages of each compiler pass to disk/tape, or the resulting code needed a lot of RAM and/or was slow).
The proper way to do it is to factor out the length calc into a utility function that the API provides. Also the calculated buffer length is passed in to the function, and an error is returned if it's too small.
You should be comparing with assembly language, not C#.