News for hackers. Stuff that matters.
Re:Zlib-rs is faster than C by uecker on Sunday March 16, 2025 @05:14PM (#43382376)
This description is still misleading. The preconditions for the correctness of an unsafe block can very much depend on the correctness of the code outside and it is easy to find Rust bugs where exactly this was the cause. This is very similar where often C out of bounds accesses are caused by some logic error elsewhere. Also an unsafe block has to maintain all the invariants the safe Rust part needs to maintain correctness.
Re:Zlib-rs is faster than C by lambda on Sunday March 16, 2025 @05:36PM (#43382585)
So, it's true that unsafe code can depend on preconditions that need to be upheld by safe code.

But using ordinary module encapsulation and private fields, you can scope the code that needs to uphold those preconditions to a particular module.

So the "trusted computing base" for the unsafe code can still be scoped and limited, allowing you to reduce the amount of code you need to audit and be particularly careful about for upholding safety guarantees.

Basically, when writing unsafe code, the actual unsafe operations are scoped to only the unsafe blocks, and they have preconditions that you need to scope to a particular module boundary to ensure that there's a limited amount of code that needs to be audited to ensure it upholds all of the safety invariants.

Ralf Jung has written a number of good papers and blog posts on this topic.

Re:Zlib-rs is faster than C by uecker on Sunday March 16, 2025 @05:47PM (#43382721)
And you think one can not modularize C code and encapsulate critical buffer operations in much safer APIs? One can, the problem is that a lot of legacy C code was not written this way. Also lot of newly written C code is not written this way, but the reason is often that people cut corners when they need to get things done with limited time and resources. The same you will see with Rust.
Re:Zlib-rs is faster than C by gf000 on Sunday March 16, 2025 @06:29PM (#43383131)
Even innocent looking C code can be chock-full of UBs that can invalidate your "local reasoning" capabilities. So, not even close.
Re:Zlib-rs is faster than C by wavemode on Sunday March 16, 2025 @06:57PM (#43383379)
Care to share an example?
Re:Zlib-rs is faster than C by capitainenemo on Sunday March 16, 2025 @07:06PM (#43383437)
sorting floats with NaN ? almost anything involving threading and mutation where people either don't realise how important locks are, or don't realise their code has suddenly been threaded?
Re:Zlib-rs is faster than C by masfuerte on Sunday March 16, 2025 @08:28PM (#43383963)

   int average(int x, int y) {
       return (x+y)/2;
   }
Re:Zlib-rs is faster than C by throwaway2037 on Monday March 17, 2025 @12:29AM (#43385221)
I assume you are hinting at 'int' is signed here? And, that signed overflow is UB in C? Real question: Ignoring what the ISO C language spec says, are there any modern hardware platforms (say: ARM64 and X86-64) that do not use two's complement to implement signed integers? I don't know any. As I understand, two's complement correctly supports overflow for signed arithmetic.

I might be old, but more than 10 years ago, hardly anyone talked about UB in C and C++ programming. In the last 10 years, it is all the rage, but seems to add very little to the conversation. For example, if you program C or C++ with the Win32 API, there are loads of weird UB-ish things that seem to work fine.

Re:Zlib-rs is faster than C by steveklabnik on Monday March 17, 2025 @12:44AM (#43385280)
> Ignoring what the ISO C language spec says, are there any modern hardware platforms (say: ARM64 and X86-64) that do not use two's complement to implement signed integers?

This is not how compilers work. Optimization happens based on language semantics, not on what platforms do.

Re:Zlib-rs is faster than C by jandrewrogers on Monday March 17, 2025 @12:58AM (#43385345)
At least in recent C++ standards, integers are defined as two’s complement. As a practical matter what hardware like that may still exist doesn’t have a modern C++ compiler, rendering it a moot point.

UB in C is often found where different real hardware architectures had incompatible behavior. Rather than biasing the language for or against different architectures they left it to the compiler to figure out how to optimize for the cases where instruction behavior diverge. This is still true on current architectures e.g. shift overflow behavior which is why shift overflow is UB.

Re:Zlib-rs is faster than C by oneshtein on Monday March 17, 2025 @02:00AM (#43385566)
AI rewrote to avoid undefined behavior:

  int average(int x, int y) {
    long sum = (long)x + y;
    if(sum > INT_MAX || sum < INT_MIN)
        return -1; // or any value that indicates an error/overflow
  
    return (int)(sum / 2);
  }
Re:Zlib-rs is faster than C by Jaxan on Monday March 17, 2025 @03:58AM (#43386128)
I’m not convinced that solution is much better. It can be improved to x/2 + y/2 (which still gives the wrong answer if both inputs are odd).
Re:Zlib-rs is faster than C by josefx on Monday March 17, 2025 @04:20AM (#43386231)
> long sum = (long)x + y;

There is no guarantee that sizeof(long) > sizeof(int), in fact the GNU libc documentation states that int and long have the same size on the majority of supported platforms.

https://www.gnu.org/software/libc/manual/html_node/Range-of-...

> return -1; // or any value that indicates an error/overflow

-1 is a perfectly valid average for various inputs. You could return the larger type to encode an error value that is not a valid output or just output the error and average in two distinct variables.

AI and C seem like a match made in hell.

Re:Zlib-rs is faster than C by cesarb on Monday March 17, 2025 @11:59AM (#43389904)
> There is no guarantee that sizeof(long) > sizeof(int), in fact the GNU libc documentation states that int and long have the same size on the majority of supported platforms.

That used to be the case for 32-bit platforms, but most 64-bit platforms in which GNU libc runs use the LP64 model, which has 32-bit int and 64-bit long. That documentation seems to be a bit outdated.

(One notable 64-bit platform which uses 32-bit for both int and long is Microsoft Windows, but that's not one of the target platforms for GNU libc.)

Re:Zlib-rs is faster than C by throwaway2037 on Monday March 17, 2025 @04:29AM (#43386269)
I don't know why this answer was downvoted. It adds valuable information to this discussion. Yes, I know that someone already pointed out that sizeof(int) is not guaranteed on all platforms to be smaller than sizeof(long). Meh. Just change the type to long long, and it works well.
Re:Zlib-rs is faster than C by gf000 on Monday March 17, 2025 @04:33AM (#43386284)
It literally returns a valid output value as an error.
Re:Zlib-rs is faster than C by oneshtein on Monday March 17, 2025 @11:22AM (#43389527)
An error value is valid output in both cases.
Re:Zlib-rs is faster than C by MaxBarraclough on Monday March 17, 2025 @06:50PM (#43393545)
The code is unarguably wrong.

average(INT_MAX,INTMAX) should return INT_MAX, but it will get that wrong and return -1.

average(0,-2) should not return a special error-code value, but this code will do just that, making -1 an ambiguous output value.

Even its comment is wrong. We can see from the signature of the function that there can be no value that indicates an error, as every possible value of int may be a legitimate output value.

It's possible to implement this function in a portable and standard way though, along the lines of [0].

[0] https://stackoverflow.com/a/61711253/ (Disclosure: this is my code.)

Re:Zlib-rs is faster than C by MaxBarraclough on Tuesday March 18, 2025 @04:19AM (#43396843)
Too late for me to edit: as josefx pointed out, it also fails to properly address the undefined behavior. The sums INT_MAX + INT_MAX and INT_MIN + INT_MIN may still overflow despite being done using the long type.

That won't occur on an 'LP64' platform, [0] but we should aim for proper portability and conformance to the C language standard.

[0] https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_m...

Re:Zlib-rs is faster than C by josefx on Monday March 17, 2025 @04:53AM (#43386391)
> Meh. Just change the type to long long, and it works well.

C libraries tend to support a lot of exotic platforms. zlib for example supports Unicos, where int, long int and long long int are all 64 bits large.

Re:Zlib-rs is faster than C by NobodyNada on Monday March 17, 2025 @11:09AM (#43389387)
Copypasting a comment into an LLM, and then copypasting its response back is not a useful contribution to a discussion, especially without even checking to be sure it got the answer right. If I wanted to know what an LLM had to say, I can go ask it myself; I'm on HN because I want to know what people have to say.
Re:Zlib-rs is faster than C by umanwizard on Tuesday March 18, 2025 @01:33AM (#43396082)
I always downvote all AI-generated content regardless of whether it’s right or wrong, because I would like to discourage people from posting it.
Re:Zlib-rs is faster than C by immibis on Monday March 17, 2025 @05:35AM (#43386613)
We're about to see a huge uptick in bugs worldwide, aren't we?
Re:Zlib-rs is faster than C by umanwizard on Tuesday March 18, 2025 @01:31AM (#43396071)
Please stop posting AI-generated content to HN. It’s clear the majority of users hate it, given that it gets swiftly downvoted every time it’s posted.
Re:Zlib-rs is faster than C by uecker on Monday March 17, 2025 @04:05PM (#43392246)
But this is also easy to protect against if you use the tools available to C programmers. It is part of the Rust hype that we would be completely helpless here, but this is far from the truth.
Re:Zlib-rs is faster than C by uecker on Saturday March 22, 2025 @10:35AM (#43445900)
You can tell a C compiler to trap or wrap around on overflow, or you use checked arithmetic to test explicitly for overflow.
Re:Zlib-rs is faster than C by pests on Monday March 17, 2025 @12:04AM (#43385097)
Re:Zlib-rs is faster than C by nicoburns on Sunday March 16, 2025 @08:24PM (#43383951)
You're a lot more limited more limited to the kinds of APIs you can safely encapsulate in C. For example, you can't safely encapsulate an interface that shares memory between the library and the caller in C. So you're forced into either:

- Exposing an unsafe API and relying on the caller to manually uphold invariants

- Doing things like defensive copying at a performance cost

In many cases Rust gives you the best of both worlds: sharing memory liberally while still having the compiler enforce correctness.

Re:Zlib-rs is faster than C by uecker on Monday March 17, 2025 @04:07PM (#43392262)
Rust is better at this yes, but the practical advantage is not necessarily that huge.
Re:Zlib-rs is faster than C by lambda on Sunday March 16, 2025 @11:19PM (#43384869)
There is no distinction between safe and unsafe code in C, so it's not possible to make that same distinction that you can in Rust.

And even if you try to provide some kind of safer abstraction, you're limited by the much more primitive type system, that can't distinguish between owned types, unique borrows, and shared borrows, nor can it distinguish thread safety properties.

So you're left to convention and documentation for that kind of information, but nothing checking that you're getting it right, making it easy to make mistakes. And even if you get it right at first, a refactor could change your invariants, and without a type system enforcing them, you never know until someone comes along with a fuzzer and figures out that they can pwn you

Re:Zlib-rs is faster than C by uecker on Monday March 17, 2025 @04:04PM (#43392234)
There is definitely a distinction between safe and unsafe code in C, it is just not a simple binary distinction. But this does not make it impossible to screen C for unsafe constructions and it also does not mean that detecting unsafe issues in Rust is always trivial.
Re:Zlib-rs is faster than C by GTP on Monday March 17, 2025 @06:21AM (#43386840)
Which is just a convoluted way of saying that it is possible to write bugs in any language. Still, it's undeniable that some languages make a better job at helping you avoid certain bugs than others.
Re:Zlib-rs is faster than C by dwattttt on Sunday March 16, 2025 @05:34PM (#43382566)
It's true, but I think it's only fair if you hold Rust to this analysis, other languages should too; the scrutiny you're implying you need in an unsafe Rust block needs to be applied to all C code, because all C code could depend on code anywhere else for its safety characteristics.

In practice (in both languages) you check what the actual unsafe code does (or "all" code in C's case), note code that depends on external actors for safety (it's not all C code, nor is it all unsafe Rust blocks), and check their callers (and callers callers, etc).

Re:Zlib-rs is faster than C by uecker on Sunday March 16, 2025 @05:44PM (#43382684)
What is true is that there are more operations in C which can cause undefined behavior and those are more densely distributed over the C code, making it harder to screen for undefined behavior. This is true and Rust certainly has an advantage, but it not nearly as big of an advantage as the "Rust is safe" (please do not look at all the unsafe blocks we need to make it also fast!) and "all C is unsafe" story wants you to believe.
Re:Zlib-rs is faster than C by pdimitar on Sunday March 16, 2025 @07:58PM (#43383793)
You sound pretty biased, gotta tell you. That snark is not helping any argument you think you might be doing -- and you are not doing any; you are kind of just making fun of Rust, which is pretty boring and uninformative for any reader.

From my past experiences with Rust, the team never had to think about data race once, or mutable volatile globals. And we all there suffered from those decades ago with C and sometimes C++ as well.

You like those and don't want to migrate? More power to ya! But badmouthing Rust with what seem fairly uninformed comments is just low. Inform yourself first.

Re:Zlib-rs is faster than C by dwattttt on Sunday March 16, 2025 @06:02PM (#43382883)
The places where undefined behaviour can occur are also limited in scope; you insist that that part isn't true, because operations outside those unsafe blocks can impact their safety.

That's only true at the same level of scrutiny as "all C operations can cause undefined behaviour, regardless of what they are", which I find similarly shallow.

Re:Zlib-rs is faster than C by gf000 on Sunday March 16, 2025 @06:36PM (#43383190)
Rust is plenty fast, in fact there are countless examples of safe rust that will trivially beat out C in performance due to no aliasing, enabling better vectorization among others. Let alone being simply a more expressive language and allowing writing better optimizations (e.g. small strings, vs the absolutely laughable c-strings that perform terribly, but also you can actually get away with sharing more stuff in memory vs doing defensive copies everywhere because it is safe to do so, etc)

And there is not many things we have statistics on in CS, but memory vulnerabilities being absolutely everywhere in unsafe languages, and Rust cleaning up the absolute majority of them even when only the new parts are written in Rust are some of the few we do know, based on actual, real life projects at Google/Microsoft among others.

A memory safe low-level language is as novel as it gets. Rust is absolutely not just hype, it actually delivers and you might want to get on with the times.

Re:Zlib-rs is faster than C by throwaway2037 on Monday March 17, 2025 @12:46AM (#43385295)

    > absolutely laughable c-strings that perform terribly
Not much being said here in 2025. Any good project will quickly switch to a tiny structure that holds char* and strlen. There are plenty of open source libs to help you.
Re:Zlib-rs is faster than C by saagarjha on Monday March 17, 2025 @05:40AM (#43386634)
I take that you consider most major projects written in C to not be "good"?
Re:Zlib-rs is faster than C by sophacles on Monday March 17, 2025 @11:19AM (#43389500)
Most major software projects are not good, no matter what language.
Re:Zlib-rs is faster than C by lambda on Sunday March 16, 2025 @11:55PM (#43385047)
What Rust provides is a way to build safe abstractions over unsafe code.

Rust's type system (including ownership and borrowing, Sync/Send, etc), along with it's privacy features (allowing types to have private fields that can only be accessed by code in the module that defined them) allows you to create fully safe interfaces around code that uses unsafe; there is provably no combination of uses of the interface which lead to undefined behavior.

Now, yeah, it's possible to also use unsafe in Rust just for applying a local optimisation. And that has fewer benefits than a fully encapsulated safe interface, though is still easier to audit for potential UB than C.

So you're right that it's on a continuum, but the distinction between safe and unsafe code means you can more easily find the specific places where UB could occur, and the encapsulation and type system makes it possible to create safe abstractions over unsafe code.

Re:Zlib-rs is faster than C by gf000 on Sunday March 16, 2025 @06:24PM (#43383088)
This is technically correct, but a bit pedantic.

Sure, you can technically just write your own vulnerability for your own program and inject it at an unsafe and see the whole world crumble... but the exact same is true for any form of FFI calls in any language. Is Java memory safe? Yeah, just because I can grab a random pointer and technically break anything I want won't change that.

The fact that a memory vulnerability error may either appear at no place at all OR at the couple hundred lines of code thorough the whole project is a night and day difference.

Re:Zlib-rs is faster than C by iknowstuff on Sunday March 16, 2025 @05:28PM (#43382514)
No. Correctness of code outside unsafe depends on correctness inside those blocks, not the other way around
Re:Zlib-rs is faster than C by uecker on Sunday March 16, 2025 @05:37PM (#43382600)
Sweet summer child.
Re:Zlib-rs is faster than C by iknowstuff on Sunday March 16, 2025 @05:59PM (#43382849)
tf are you talking about
Re:Zlib-rs is faster than C by dwattttt on Sunday March 16, 2025 @06:05PM (#43382911)
In a more helpful framing: safe Rust code doesn't need to worry about its own correctness, it just is.

Unsafe code can be incorrect (or unsound), and needs to be careful about it. Part of being careful is that safe code can call the unsafe code in a way that triggers that unsoundness; in that way, safe code can cause undefined behaviour in unsafe code.

It's not always the case that this is possible; there are unsafe blocks that don't need to depend on safe code for its correctness.

Re:Zlib-rs is faster than C by steveklabnik on Sunday March 16, 2025 @06:04PM (#43382906)
They are (rudely) talking about https://news.ycombinator.com/item?id=43382369