Skip to content
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

Dangerous undefined value #21970

Closed
nine-fox opened this issue Nov 12, 2024 · 5 comments
Closed

Dangerous undefined value #21970

nine-fox opened this issue Nov 12, 2024 · 5 comments

Comments

@nine-fox
Copy link

Zig Version

0.14.dev

Steps to Reproduce and Observed Behavior

The following code is valid but extremely dangerous, because val is rubbish value. No any warnning messages.

When myfunc is from 3rd party, how do I know the returned value is valid?

Also, I can not even check val value using == undefined because 0.14 will emit an error.

I would suggest undefined value should not be returned when function marked with ?. At least a warning is needed?

fn myfunc() ?u32 {
return undefined;
}
test "aa" {
if (myfunc()) |val| {
std.debug.print("{}\n", .{val});
}
}

Expected Behavior

emit an error

@nine-fox nine-fox added the bug Observed behavior contradicts documented or intended behavior label Nov 12, 2024
@andrewrk
Copy link
Member

duplicate of #63 (not a bug)

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 2024
@andrewrk andrewrk removed the bug Observed behavior contradicts documented or intended behavior label Nov 12, 2024
@nektro
Copy link
Contributor

nektro commented Nov 12, 2024

related #211

undefined is not detectable at runtime and using it must be done with caution otherwise you may run into the aptly named undefined behavior. it is up to the programmer to use undefined safely in the cases where the compiler is not able to check it.

@nine-fox
Copy link
Author

nine-fox commented Nov 12, 2024

hi @andrewrk @nektrne thanks for your comments and I know the background to introduce undefined. However, compare to null which is much safer, then whats scenarios should use undefined?

If there are cases to be dangerous, I would think undefined should not be used in production. Make an option to turn undefined on/off?

@nine-fox
Copy link
Author

@nektro also in the code above, this case can be detected, bu no error?even a warning is better. Comptime undefined can be detected

@nektro
Copy link
Contributor

nektro commented Nov 12, 2024

undefined is most often used to leave a value uninitialized for later use. for instance:

var buffer: [1024]u8 = undefined;
const slice = foo(&buffer);

happy to discuss more in a Community

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants