Hacker News
points by jppittma 2 days ago | 0 comments
C has unions. C has sum types. People have been doing tagged unions in C for longer than rust has been a language
[–] creata 2 days ago
It is extremely unidiomatic to use tagged unions in C for error handling. It's definitely possible, but awkward, and the compiler doesn't check that you checked the tag.
[–] K0nserv 2 days ago
I don't think I've ever seen a C function that uses unions for this purpose though?

I can think of two common patterns that I've seen frequently:

* Return NULL for errors (sometimes with a global errorno() function).

* Return an integer error with any return values as out parameters.

So if it is possible to improve, why doesn't anyone do it?

[–] Woodi 2 days ago
Because usually NULL is good enough and you need to have domain use case to make library for it.

> * Return an integer error with any return values as out parameters.

You must first standarize all that (usually negative) integers, make docs, check for it, do not make mistakes in your if's or case's...

And errno is just that - integers with a meaning. errno is even thread local now and quite safe to use but people on internet scream loudly when someone try to use that not app global varible :) I guess their llms statistically have less new texts feeded in ;)

At some point it is just better to return pointer to struct with all info you can imagine.

And how you think other languages implemented their Error type returns ? Just a struct or union with tag. Because current CPU's operate on simple type registers there is no other sane way to do it.

[–] aliceryhl 2 days ago
Yet the authors of crypt did not use them. The C language is not designed in a way to encourage people to write code that correctly uses tagged unions (or nullable pointers for that matter).
[–] knome 2 days ago
because POSIX dictates the shape of crypt from a function written in 1973 and unions/structs were only introduced to the C language in 1976.

the fault for sshhole isn't just "C bad", it's a series of poor choices in workarounds falling together to create a vulnerability.

being condescending about decisions made 50 years ago, before any of the better languages you're mad the developer didn't use even existed, is silly.

if you want to lay blame, lay it at the feet of more recent devs that started returning values instead of NULL because people were using crypt incorrectly. likely sha512 returned a fake value instead of a real one because prior hashes couldn't fail. the interface should have been updated then, but instead they worked around it.

[–] jppittma 2 days ago
Where are you going with those goal posts?
[–] Philpax 2 days ago
Nowhere. It's the same argument as upthread. A language that forces downstream consumers of an API to correctly deal with all possible outcomes would have prevented this.
[–] jppittma 15 hours ago
You can’t force downstream to do anything. I can write a library in C that hints just as hard as rust does though, using basically the same tools/apis, since they’re all there. The only difference is that it’s less conventional in C. That’s probably a smaller lift than rewrite it in rust.

Memory is where C is broken. I feel like without `defer` or destructors the language meaningfully is incomplete.

[–] rini17 2 days ago
Goalposts are fine. If crypt did return union type, C still allows cowboy programmers to straight extract the char* without any check whether it's valid. So we'd end up in exactly the same situation.
[–] cannonpalms 2 days ago
I don't think the goal posts were moved. The comment you replied to referred to the "use"--not the existence--of sum types.
[–] zarzavat 1 day ago
That's a universe apart from real sum types with exhaustiveness checking and pattern matching.