3 ms·
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
by Thorrez 8y 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.
- dearrifling 8y agoThat has nothing to do with memory safety, that's just plain UB.
- Thorrez 8y agoAccording 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.
- fxfan 8y agoHow does rust handle this?
- Thorrez 8y agoI've never used rust, but from what I've heard, it crashes in debug mode, and wraps (two's complement) in release mode.
- deleted 8y ago[deleted]
- steveklabnik 8y agoThat’s the default semantics, yes.
- vardump 8y ago> 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.