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

Unwrapping undefined optional is not safety-checked in safe modes #22215

Open
mnemnion opened this issue Dec 13, 2024 · 2 comments
Open

Unwrapping undefined optional is not safety-checked in safe modes #22215

mnemnion opened this issue Dec 13, 2024 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@mnemnion
Copy link

Zig Version

v0.14.0-dev.2238+1db8cade5

Steps to Reproduce and Observed Behavior

This passes in Debug mode, but fails in ReleaseFast:

test "unwrapped undefined optional" {
    var opt_block: ?[4096]u8 = undefined;
    opt_block.?[0] = '!';
    try expectEqual('!', opt_block.?[0]);
    try expect(opt_block != null);
    opt_block = null;
    try expect(opt_block == null);
}

Expected Behavior

Expect a panic when the some field is 0xaa on unwrap. The condition is detectable, because using @as([4096]u8, undefined) sets some to 0x01.

@mnemnion mnemnion added the bug Observed behavior contradicts documented or intended behavior label Dec 13, 2024
@nektro
Copy link
Contributor

nektro commented Dec 13, 2024

related #63
related #211

@mnemnion
Copy link
Author

And the master issue #2301.

Maybe worth keeping this open as a specific case, because a fix for optionals which use the some + data structure is comparatively straightforward: unwrapping is already null-checked in safe modes, this could check specifically for 0x01 and handle both types of error on the cold path, which should be broadly as efficient when the program is not immediately on the verge of crashing.

Not as straightforward for pointers, no idea how any later niche optimizations would interact with Debug and ReleaseSafe, but that's a problem for later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants