Open andrewrk opened 5 years ago
Most undefined behavior that cannot be detected at compile-time can be detected at runtime.
So you are saying that we need to add a no-safety sub section in https://ziglang.org/documentation/master/#Undefined-Behavior that will be continuously populated? Do you have examples of such cases? e.g. from existing github issues
So you are saying that we need to add a no-safety sub section in https://ziglang.org/documentation/master/#Undefined-Behavior that will be continuously populated?
I believe that the subsection listing all no-safety behavior should be added.
Do you have examples of such cases? e.g. from existing github issues
undefined
[*]T
[*c]T
@bitCast
@intToPtr
@ptrCast
@memcpy
@memset
@fieldParentPtr
Here is an undefined behavior caused by incorrectly using @qualCast
. The UB is currently unchecked (zig version 0.11.0-dev.6306+693b12f8e, stage3 built following the wiki):
const std = @import("std");
const s = "hello";
pub fn main() void {
comptime {
var s2 = @qualCast(*[5:0]u8, s);
s2[0] = 'H';
}
}
$ zig run comptime_qual-cast-ub.zig
Segmentation fault at address 0x2086bf
/tmp/zigtmp/comptime_qual-cast-ub.zig:5:5: 0x20b544 in main (comptime_qual-cast-ub)
comptime {
^
/home/manlio/src/contrib/zig/github.com/perillo/zig/lib/std/start.zig:606:22: 0x20aa70 in posixCallMainAndExit (comptime_qual-cast-ub)
root.main();
^
/home/manlio/src/contrib/zig/github.com/perillo/zig/lib/std/start.zig:376:5: 0x20a521 in _start (comptime_qual-cast-ub)
@call(.never_inline, posixCallMainAndExit, .{});
^
zsh: IOT instruction (core dumped) ~/src/contrib/zig/github.com/perillo/zig/build/stage3/bin/zig run
const std = @import("std");
const debug = std.debug;
const s = "hello";
pub fn main() void {
var s2 = @qualCast(*[5:0]u8, s);
s2[0] = 'H';
std.debug.print("s[0]: {c}\n", .{s[0]});
}
$ zig run runtime_qual-cast-ub.zig
Segmentation fault at address 0x2086bf
/tmp/zigtmp/comptime_qual-cast-ub.zig:5:5: 0x20b544 in main (comptime_qual-cast-ub)
comptime {
^
/home/manlio/src/contrib/zig/github.com/perillo/zig/lib/std/start.zig:606:22: 0x20aa70 in posixCallMainAndExit (comptime_qual-cast-ub)
root.main();
^
/home/manlio/src/contrib/zig/github.com/perillo/zig/lib/std/start.zig:376:5: 0x20a521 in _start (comptime_qual-cast-ub)
@call(.never_inline, posixCallMainAndExit, .{});
^
zsh: IOT instruction (core dumped) ~/src/contrib/zig/github.com/perillo/zig/build/stage3/bin/zig run
The language reference should have a comprehensive list of every of possible undefined behavior that is possible, with a description of either how safety checks work for it, and under what circumstances the safety check will catch the problem or not, or the docs should explain why there cannot be safety checking for this particular kind of undefined behavior.
With this it will be obvious how close (or distant) zig is from a safe language.