Skip to content

Commit 5d41c47

Browse files
committed
std.fs.File: fix positional seekBy using wrong position
positional seekBy should use the logical position, but it used the file position which includes the buffered data. the test did not catch this because it used an empty buffer. fixes #25087 #25020
1 parent 0b75a2a commit 5d41c47

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/std/fs/File.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ pub const Reader = struct {
11891189
pub fn seekBy(r: *Reader, offset: i64) Reader.SeekError!void {
11901190
switch (r.mode) {
11911191
.positional, .positional_reading => {
1192-
setPosAdjustingBuffer(r, @intCast(@as(i64, @intCast(r.pos)) + offset));
1192+
setPosAdjustingBuffer(r, @intCast(@as(i64, @intCast(r.logicalPos())) + offset));
11931193
},
11941194
.streaming, .streaming_reading => {
11951195
if (posix.SEEK == void) {

lib/std/fs/test.zig

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,7 @@ test "seek keeping partial buffer" {
21402140
try testing.expectEqualStrings("6789", &buf);
21412141
}
21422142

2143-
test "seekBy" {
2143+
test "seekBy streaming" {
21442144
var tmp_dir = testing.tmpDir(.{});
21452145
defer tmp_dir.cleanup();
21462146

@@ -2156,6 +2156,26 @@ test "seekBy" {
21562156
try testing.expectEqualStrings("t's test seekBy", buffer[0..15]);
21572157
}
21582158

2159+
test "seekBy positional" {
2160+
var tmp_dir = testing.tmpDir(.{});
2161+
defer tmp_dir.cleanup();
2162+
2163+
try tmp_dir.dir.writeFile(.{ .sub_path = "blah.txt", .data = "let's test seekBy" });
2164+
const f = try tmp_dir.dir.openFile("blah.txt", .{ .mode = .read_only });
2165+
defer f.close();
2166+
var read_buf: [32]u8 = undefined;
2167+
var reader = f.reader(&read_buf);
2168+
var buffer: [20]u8 = undefined;
2169+
const n1 = try reader.interface.readSliceShort(buffer[0..2]);
2170+
try testing.expectEqual(2, n1);
2171+
try testing.expectEqualStrings("le", buffer[0..2]);
2172+
try reader.seekBy(2);
2173+
2174+
const n2 = try reader.interface.readSliceShort(&buffer);
2175+
try testing.expectEqual(13, n2);
2176+
try testing.expectEqualStrings("s test seekBy", buffer[0..13]);
2177+
}
2178+
21592179
test "seekTo flushes buffered data" {
21602180
var tmp = std.testing.tmpDir(.{});
21612181
defer tmp.cleanup();

0 commit comments

Comments
 (0)