ziglang/zig

`readPackedIntBig` reads from end of array

apwadkar opened this issue · 0 comments

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));