-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
breakingImplementing this issue could cause existing code to no longer compile or have different behavior.Implementing this issue could cause existing code to no longer compile or have different behavior.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.This issue is limited in scope and/or knowledge of Zig internals.standard libraryThis issue involves writing Zig code for the standard library.This issue involves writing Zig code for the standard library.
Milestone
Description
Zig Version
0.9.1 (windows, chocolatey),0.10.0-dev.4166+cae76d829
Steps to Reproduce
- create file repro.zig
/// repro.zig
const std = @import("std");
const expect = std.testing.expect;
const PackedStruct = packed struct {
a: u48,
b: u48,
c: u16,
};
test "reading a packed struct" {
const file = try std.fs.cwd().openFile("repro.zig", .{});
const reader = file.reader();
_ = try reader.readStruct(PackedStruct);
const pos_from_reading_struct = try reader.context.getPos();
try reader.context.seekTo(0);
_ = try reader.readBytesNoEof(@bitSizeOf(PackedStruct) / 8);
const pos_from_reading_bytes = try reader.context.getPos();
std.log.warn("{}, {}", .{ pos_from_reading_struct, pos_from_reading_bytes });
try expect(pos_from_reading_struct == pos_from_reading_bytes);
}zig test repro.zig
Expected Behavior
Test passes.
Log output should be 14. 14
Actual Behavior
Test fails. Log output is 16, 14.
This is because @sizeOf(PackedStruct) is 16.
However, using sizeOf in readStruct is undesirable for reading consecutive packed structs, because now the seeker position is wrong, affecting future reads downstream. To correct this manually, the developer has to check if packed struct requires padding, and manually wind the seeker back using seekBy.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
breakingImplementing this issue could cause existing code to no longer compile or have different behavior.Implementing this issue could cause existing code to no longer compile or have different behavior.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.This issue is limited in scope and/or knowledge of Zig internals.standard libraryThis issue involves writing Zig code for the standard library.This issue involves writing Zig code for the standard library.