diff --git a/overflow.zig b/overflow.zig new file mode 100644 index 0000000..4cd0a5e --- /dev/null +++ b/overflow.zig @@ -0,0 +1,4 @@ +test "integer overflow at compile time" { + const x: u8 = 255; + const y = x + 1; +} diff --git a/runtime_overflow.zig b/runtime_overflow.zig new file mode 100644 index 0000000..082f96a --- /dev/null +++ b/runtime_overflow.zig @@ -0,0 +1,4 @@ +test "integer overflow at runtime" { + var x: u8 = 255; + x += 1; +} diff --git a/undefined.zig b/undefined.zig new file mode 100644 index 0000000..7ba1938 --- /dev/null +++ b/undefined.zig @@ -0,0 +1,15 @@ +const std = @import("std"); + +pub fn main() void { + // if you remove this line, the program does not compile because it detects + // the overflow at compile time. A little confusing that it's not + // @setCompileTimeSafety but that's ok. + @setRuntimeSafety(false); + + var x: u8 = 255; + x += 1; // <-- this is an overflow + + // this prints 0 on my machine but is apparently undefined. I wonder what + // it prints on other machines? + std.debug.print("X: {}\n", .{x}); +}