upvote
> My god, you only see this type of horrendous API in C don't you.

This is the appropriate place to put a link to this pair of classics on API design:

"How Do I Make This Hard to Misuse?" https://ozlabs.org/~rusty/index.cgi/tech/2008-03-30.html

"What If I Don't Actually Like My Users?" https://ozlabs.org/~rusty/index.cgi/tech/2008-04-01.html

reply
gets belongs in category -10
reply
It makes lot of sense when you think what encoding here does. Changes 3 arbitrary bytes to 4 bytes with only some values being possible. Null termination is one issue, but the output is a string such only length of the text itself makes sense.
reply
"No way to prevent this" say users of only language where this regularly happens

https://xeiaso.net/blog/series/no-way-to-prevent-this/

reply
What's "horrendous"? I don't see anything which stands out as being wrong. It specifies exactly the lengths of input and output.

gets(), on the other hand, that is "horrendous".

reply
Even in C, if you have a function that takes an input buffer and output buffer of differing lengths, the right API has the caller pass both lengths.

But yes, gets is worse.

reply
takes an input buffer and output buffer of differing lengths, the right API has the caller pass both lengths.

If there is no fixed relationship between the two, then that makes sense; but in this case there is a very simple and well-defined relationship between them. But to specify two lengths which must always agree; and if not, then what? You've effectively invented an additional artificial error condition, an additional branch, and burdened all the callers up the chain to do something, which is a treacherous path to complexity and the production of even more bugs.

Or are you just preaching the same mindless idiocy that lead to Microsoft's memcpy_s() (the "s" is for "stupid")?

reply
The artificial error condition is the whole point and the hoops you have to jump through to be able to reach it as you say are reasons to not use C because it’s hard to enforce invariants like this and easy to ignore them with mostly working code. Mostly working was fine last year, it isn’t anymore.
reply
Turning this into an attack on the language with otherwise nonsense arguments is how I know you're an ideological bully not worth arguing with, just like the ones who called this nothingburger a 9.8.
reply
The thing that jumps out to me is that it returns “the size of the output data minus one” but if I’m passing in a pointer it’s on me to make sure there’s enough memory allocated isn’t it? So the caller must make sure it’s calculated this correctly - in which case why return the size? Taking the size as an argument could be safer as you’d be checking the caller calculated enough space too.

(Honest questions in this it’s probably been 20 years since I did c)

reply
That part of the API seems particularly random indeed, especially when comparing it to other EVP_{En,De}code* functions which use the returned int for success/error.

The only reason for such a design I could think of is that the caller would be able to "detect" the overflow by comparing the returned value against the length of the buffer. But that is of course pointless, as the compiler is free to assume a buffer overflow cannot happen and thus optimize the check away...

reply
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.

reply
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.
reply
Yes, of course, this isn’t C, this is just another case of people writing C wrong.
reply
Perhaps we could use another language, where it's not so easy (or possible) to write it wrong?
reply
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.
reply
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.
reply
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.

reply
You should be comparing with assembly language, not C#.
reply