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

readPackedIntBig reads from end of array #19953

Open
apwadkar opened this issue May 12, 2024 · 0 comments
Open

readPackedIntBig reads from end of array #19953

apwadkar opened this issue May 12, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@apwadkar
Copy link

Zig Version

0.13.0-dev.73+db890dbae

Steps to Reproduce and Observed Behavior

Godbolt: https://godbolt.org/z/EK87ob593

const std = @import("std");

test "readParsedInt" {
    const slice = [10]u8{0, 0, 0, 1, 0, 0, 0, 2, 3, 1};
    const val = std.mem.readPackedInt(u32, &slice, 0, .big);
    try std.testing.expectEqual(1, val);
}

It reads the last 4 bytes and returns 0x00020301.

Expected Behavior

I was expecting this to read the first 4 bytes of the slice as big-endian (0x00000001), but it seems that this function reads from the back of the array instead.

I've pinpointed the cause to here.

const read_bytes = bytes[(end - byte_count)..end];
const val = @as(uN, @truncate(readInt(LoadInt, bytes[(end - load_size)..end][0..load_size], .big) >> bit_shift));
@apwadkar apwadkar added the bug Observed behavior contradicts documented or intended behavior label May 12, 2024
@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label May 13, 2024
@Vexu Vexu added this to the 0.13.0 milestone May 13, 2024
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 standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

2 participants