Skip to content

std.fs.File.Reader.seekBy() with negative offset does not work as expected #25020

@mlarouche

Description

@mlarouche

Zig Version

0.15.1

Steps to Reproduce and Observed Behavior

I'm being slowly migrating my library zigimg to 0.15.1 and the new I/O reader and I hit a snag when trying to add seekBy() to negative offsets in my streaming abstraction. Seeking negative numbers within a file reader does not give the expected behavior

test "should read and seek properly within a file" {
    const TEST_FILE_CONTENTS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const TEST_FILENAME = "io_read_stream_test.dat";

    var temp_folder = std.testing.tmpDir(.{});
    defer temp_folder.cleanup();

    try temp_folder.dir.writeFile(.{ .sub_path = TEST_FILENAME, .data = TEST_FILE_CONTENTS });

    var read_file = try temp_folder.dir.openFile(TEST_FILENAME, .{});
    defer read_file.close();

    var read_buffer: [4096]u8 = @splat(0);
    var file_reader = read_file.reader(read_buffer[0..]);

    var reader = file_reader.interface;

    const read_inside_buffer = try reader.take(3);
    try std.testing.expectEq(3, file_reader.logicalPos());
    try std.testing.expectEq(u8, TEST_FILE_CONTENTS[0..3], read_inside_buffer[0..3]);

    try file_reader.seekBy(-3);

    // This is the test that's failling
    try std.testing.expectEq(0, file_reader.logicalPos()); 
}

Actual result: file_reader.logicalPos(): 33
Expected result: file_reader.logicalPos(): 0

Tested on Windows 11 24H2 (26100.4946)

Expected Behavior

Actual result: file_reader.logicalPos(): 33

Expected result: file_reader.logicalPos(): 0

Metadata

Metadata

Assignees

Labels

bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions