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

std.json.parseFromSlice divide by zero #23168

Open
MasonRemaley opened this issue Mar 9, 2025 · 1 comment · May be fixed by #23220
Open

std.json.parseFromSlice divide by zero #23168

MasonRemaley opened this issue Mar 9, 2025 · 1 comment · May be fixed by #23220
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@MasonRemaley
Copy link
Contributor

Zig Version

0.14.0

Steps to Reproduce and Observed Behavior

This test:

test "zero sized" {
    const Struct = struct {
        a: []const enum { a } = &.{}, // <-- note that this enum is 0 sized
    };
    const header = try std.json.parseFromSlice(
        Struct,
        std.testing.allocator,
        "{\"a\": [\"a\"] }",
        .{},
    );
    defer header.deinit();
}

Results in this output:

test
└─ run test
   └─ zig test Debug native 1 errors
/home/mason/Documents/zig/lib/std/mem.zig:4219:70: error: division by zero here causes undefined behavior
    return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];

If you replace the zero sized enum with a non zero sized type, then it works as expected.

I caught this while upgrading a project to 0.14.0, so presumably the bug was introduced some time in the past few months, but I haven't checked exactly which version I was running previously.

Expected Behavior

I expected the test to pass.

@MasonRemaley MasonRemaley added the bug Observed behavior contradicts documented or intended behavior label Mar 9, 2025
@samy-00007
Copy link
Contributor

This actually seems to be an issue with bytesAsSlice, which is now called by remap (previously, resize did not call the function), as it doesn't check for the element size.

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

Successfully merging a pull request may close this issue.

2 participants