News for hackers. Stuff that matters.
Re:Sshole by nh2 on Thursday November 05, 2026 @08:47AM (#50379008)
Any use of sum types would have prevented this immediately.

The whole thing exists because APIs that return a NULL value that "nobody checks", plus a string that you shouldn't use if you actually did check the null value.

In languages with sum types, this cannot happen.

Either you get your hash, or no result at all. You couldn't get eg NULL and *.

Re:Sshole by win311fwg on Thursday November 05, 2026 @08:56AM (#50379100)
> In languages with sum types, this cannot happen.

Funny, C has sum types (coproduct is a sum type) and it still happened.

The use of sum types may have helped, especially when combined with a tag which is trivial to do in C, but a language having sum types does not imply that the authors will use them. No language can save a reckless developer.

Re:Sshole by jppittma on Thursday November 05, 2026 @09:30AM (#50379519)
C has unions. C has sum types. People have been doing tagged unions in C for longer than rust has been a language
Re:Sshole by aliceryhl on Thursday November 05, 2026 @09:48AM (#50379758)
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).
Re:Sshole by jppittma on Thursday November 05, 2026 @10:02AM (#50379968)
Where are you going with those goal posts?
Re:Sshole by Philpax on Thursday November 05, 2026 @10:48AM (#50380608)
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.
Re:Sshole by jppittma on Saturday November 07, 2026 @01:54PM (#50406966)
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.

Re:Sshole by rini17 on Thursday November 05, 2026 @10:58AM (#50380768)
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.
Re:Sshole by cannonpalms on Thursday November 05, 2026 @11:20AM (#50381013)
I don't think the goal posts were moved. The comment you replied to referred to the "use"--not the existence--of sum types.
Re:Sshole by knome on Thursday November 05, 2026 @05:19PM (#50385300)
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.

Re:Sshole by K0nserv on Thursday November 05, 2026 @09:49AM (#50379774)
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?

Re:Sshole by Woodi on Friday November 06, 2026 @01:26AM (#50388877)
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.

Re:Sshole by creata on Thursday November 05, 2026 @11:08AM (#50380895)
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.
Re:Sshole by zarzavat on Friday November 06, 2026 @07:07AM (#50391403)
That's a universe apart from real sum types with exhaustiveness checking and pattern matching.
Re:Sshole by dolmen on Thursday November 05, 2026 @10:06AM (#50380017)
Nope.

Because in fact all the changes were about changing the function signatures. The function signature is enforced by the linker. The semantic of strings that go through isn't.

Returning 2 values is possible in C (via pointers input parameters). The main issue here is that libc is a strong API contract based on C function signatures, that nobody can change, only augment with new symbols. Such strong API compatibility constraints happen in any programming language, and workaround to fit new behaviours in old APIs happens everywhere.

Re:Sshole by K0nserv on Thursday November 05, 2026 @10:40AM (#50380497)
I don't think nh2 is saying that this is how the "issue" should've been solved here, but that it's a fatal flaw of C itself (yes I know, unions, but no one uses them).

But also, its not like there were many alternative languages when the POSIX standards were designed so it's fair that it works like this.

Re:Sshole by xorcist on Thursday November 05, 2026 @12:47PM (#50382029)
This argument is weak. C, and specifically libc, has errno. The error code is there. Not in band.

But there has to be something in the payload field when it is not applicable. A null is perfectly fine.

The argument for changing that to "", if I understand the above linked discussion correctly, was that some users of this api threw away errors and a dummy value would be harder to ignore. You could argue that these behaviours are less common in Rust or Go, but that's not a technical difference.

The world is not perfect and sometimes you really need to work around bad users. Even in Rust or Go. I am not convinced this was such a case. If you are convinced it was it would still have been better to select a less trivial dummy value (such as ":ERROR:*").

Re:Sshole by forresthopkinsa on Thursday November 05, 2026 @03:07PM (#50383789)
> But there has to be something in the payload field when it is not applicable

Not in languages with ergonomic sum types. A `Result` or `Either` will have no payload field on the error type. The compiler won't let you dereference it.

Re:Sshole by aidenn0 on Thursday November 05, 2026 @05:55PM (#50385716)
This is wrong. errno is not to be used to detect errors, only to get the cause of the error once an error has been detected.
Re:Sshole by lemonwaterlime on Friday November 06, 2026 @10:12AM (#50393300)
In Haskell, one would return a Maybe or an Either here.

    Maybe
    ——————
    Just hash
    Nothing

    For Either
    ————————————
    Left err
    Right hash
The program would be designed around not allowing this failure mode to even be possible.
Re:Sshole by aidenn0 on Thursday November 05, 2026 @05:54PM (#50385705)
Even C has a better type system than C: All modern C compilers support an extension that lets you flag a function's return-value as requiring a null check.