Hacker News new | ask | show | jobs
by Thorrez 2655 days ago
When a simple

    int foo(int x) {
      return x+1;
    }
leads to undefined behavior (aka the standard says the program can delete your hard drive) if x is too big, that's not a language that guarantees memory safety.
2 comments

That has nothing to do with memory safety, that's just plain UB.
According to the spec, it can lead to all the same problems that any UB leads to, including all the problems that any memory unsafety leads to. The code is allowed by the spec to cause memory unsafety.

But to list some direct memory unsafety possibilities: indexing off the end of an array, indexing off the end of a vector, dereferencing a bad pointer, dereferencing a null pointer, dereferencing a bad iterator, double delete.

How does rust handle this?
I've never used rust, but from what I've heard, it crashes in debug mode, and wraps (two's complement) in release mode.
> it crashes in debug mode, and wraps (two's complement) in release mode.

Well, that's indeed a nice feature in debug mode. Mostly wraparound is undesired and wraparound bugs are pretty common. Shame the performance cost is too much to do same in release.

Whenever wraparound semantics it is actually wanted, one can use an appropriate type.

That’s the default semantics, yes.