-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
runtime safety to detect branch on undefined #3568
Comments
The control flow of your logic, when there is no command line parameter passed, is: var code_file: ?[]u8 = undefined;
if (code_file) |_| {} else { This is a branch on Here's a fully self-contained example: const std = @import("std");
const assert = std.debug.assert;
pub fn main() anyerror!void {
var code_file: ?[]u8 = undefined;
var args = std.process.args();
// Skip argv[0]
assert(args.skip() == true);
if (args.next(std.heap.direct_allocator)) |arg_err| {
code_file = try arg_err;
}
if (code_file) |_| {} else {
std.debug.warn("Usage: bfi [FILE]\n");
return;
}
} In safe builds, it compiles and outputs:
It's planned for safe modes to detect this branch on undefined and output instead "branch on undefined value" plus a stack trace. This will turn Undetectable Illegal Behavior into Detectable Illegal Behavior. I'll leave this issue open until this is solved. For your project, you want |
Well, one thing is certain: I'm never gonna mix up |
One more thing - Zig has valgrind integration with regards to
|
What about
Should the above be considered Illegal Behavior?
which can be rewritten just as
? |
Closing as a duplicate of #63 |
I've written a very simple argument "parser" that just reads in the first console argument given (after skipping argv[0]):
While rewriting some of the code I found that if I build with release-fast or release-small, the 2nd if-else{} is entirely optimized out.
Another check later on is also completely voided:
This will just try to load the file even if code_file is null.
Interestingly, I found out that if you used the variable code_file somewhere before the checks (e.g. print it to stdout), everything works as expected. Debug builds and release-safe are fine, too.
The build command is just simply "zig build", as well as, "zig build -Drelease-fast/small/safe" respectively, on a default created zig init-exe build.zig. The target is x86_64-linux-gnu, there's no actual dependency on libc though.
"zig version" is:
The compiler was compiled in "ReleaseWithDebInfo" config mode.
The text was updated successfully, but these errors were encountered: