News for hackers. Stuff that matters.
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.