How does Nim deal with the fact that null pointer dereferences, signed integer overflow, and shifts by more than the width of a type are undefined behavior in C, and therefore the optimizer can cause memory safety problems if they occur?
Edit: I just tested myself:
- Nim emits checks for signed overflow, but they don't seem to optimize to `jo`. Nim should be emitting these intrinsics where possible: http://clang.llvm.org/docs/LanguageExtensions.html#checked-a...
- Nim does not emit checks for shifts by more than the width of a type. Therefore you can get undefined behavior.
- Nim blindly dereferences null pointers, which is undefined behavior. In fact, I managed to get clang to optimize out the null pointer check here:
echo("Counting to ten: ")
for i in countup(1, 10):
var k: ref int = nil
var l = k[]
echo($l)
In debug mode, this program throws an exception; in release mode, this program prints zero over and over for me, because clang optimized out the null pointer dereference per C semantics. I could probably make this program write arbitrary memory without using any of Nim's unsafe features.I'm not sure if all three of these are Nim bugs, but they seem like it. Compiling to C is tricky.