Skip to content

std.Io.Reader: use readVec for fill functions #24706

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

Merged
merged 1 commit into from
Aug 7, 2025
Merged

std.Io.Reader: use readVec for fill functions #24706

merged 1 commit into from
Aug 7, 2025

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Aug 5, 2025

addresses #24695, as well as just being a better, simpler implementation for these functions now that readVec is back in the vtable.

@mrjbq7
Copy link
Contributor

mrjbq7 commented Aug 6, 2025

I believe the panic.z and fail_read.z cases both still fail, but with this error trace:

const std = @import("std");
const flate = std.compress.flate;

pub fn main() !void {
    const input_file = try std.fs.cwd().openFile("panic.z", .{});
    var input_buffer: [4096]u8 = undefined;
    var input_reader = input_file.reader(&input_buffer);

    var buffer: [flate.max_window_len]u8 = undefined;
    var decompress: flate.Decompress = .init(&input_reader.interface, .zlib, &buffer);

    // should decompress to 4160 bytes
    var out_buffer: [4160]u8 = undefined;
    var out_writer: std.Io.Writer = .fixed(&out_buffer);

    const written = try decompress.reader.streamRemaining(&out_writer);
    std.debug.print("decompressed {} bytes\n", .{written});
}

fails with

thread 17129927 panic: reached unreachable code
zig/build/stage3/lib/zig/std/debug.zig:559:14: 0x100be3e07 in assert (min)
    if (!ok) unreachable; // assertion failure
             ^
zig/build/stage3/lib/zig/std/Io/Reader.zig:1840:11: 0x100c7d90f in writableVectorPosix (min)
    assert(r.seek == r.end);
          ^
zig/build/stage3/lib/zig/std/fs/File.zig:1327:82: 0x100c7b0cf in readVec (min)
                const dest_n, const data_size = try io_reader.writableVectorPosix(&iovecs_buffer, data);
                                                                                 ^
zig/build/stage3/lib/zig/std/Io/Reader.zig:1064:56: 0x100c956c3 in fillUnbuffered (min)
    while (r.end < r.seek + n) _ = try r.vtable.readVec(r, &bufs);
                                                       ^
zig/build/stage3/lib/zig/std/Io/Reader.zig:1050:26: 0x100c953fb in fill (min)
    return fillUnbuffered(r, n);
                         ^
zig/build/stage3/lib/zig/std/Io/Reader.zig:489:15: 0x100c94ac3 in peek (min)
    try r.fill(n);
              ^
zig/build/stage3/lib/zig/std/Io/Reader.zig:570:23: 0x100c8cb37 in peekArray__anon_25952 (min)
    return (try r.peek(n))[0..n];
                      ^
zig/build/stage3/lib/zig/std/Io/Reader.zig:1117:46: 0x100c9081b in peekBits__anon_26138 (min)
    return std.mem.readInt(T, try r.peekArray(n), endian);
                                             ^
zig/build/stage3/lib/zig/std/compress/flate/Decompress.zig:610:27: 0x100c90b4f in takeBitsRuntime (min)
    const x = try peekBits(d, u16);
                          ^
zig/build/stage3/lib/zig/std/compress/flate/Decompress.zig:184:43: 0x100c90da3 in decodeLength (min)
        ml.base + try self.takeBitsRuntime(ml.extra_bits);
                                          ^
zig/build/stage3/lib/zig/std/compress/flate/Decompress.zig:423:54: 0x100c87d2b in streamInner (min)
                    const length = try d.decodeLength(sym.symbol);
                                                     ^
zig/build/stage3/lib/zig/std/compress/flate/Decompress.zig:248:23: 0x100c85157 in streamFallible (min)
    return streamInner(d, w, limit) catch |err| switch (err) {
                      ^
zig/build/stage3/lib/zig/std/compress/flate/Decompress.zig:171:23: 0x100c85afb in streamIndirectInner (min)
    _ = streamFallible(d, &writer, .limited(writer.buffer.len - writer.end)) catch |err| switch (err) {
                      ^
zig/build/stage3/lib/zig/std/compress/flate/Decompress.zig:244:31: 0x100c95947 in streamIndirect (min)
    return streamIndirectInner(d);
                              ^
zig/build/stage3/lib/zig/std/Io/Reader.zig:180:34: 0x100bf6a7b in stream (min)
    const n = try r.vtable.stream(r, w, limit);
                                 ^
zig/build/stage3/lib/zig/std/Io/Reader.zig:259:27: 0x100c754c7 in streamRemaining (min)
        offset += r.stream(w, .unlimited) catch |err| switch (err) {
                          ^
min.zig:25:58: 0x100c750df in main (min)
    const written = try decompress.reader.streamRemaining(&out_writer);
                                                         ^
zig/build/stage3/lib/zig/std/start.zig:627:37: 0x100c758ff in main (min)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x19ac72b97 in ??? (???)
???:?:?: 0x0 in ??? (???)
[1]    56574 abort      zig/build/stage3/bin/zig run min.zig
``

readVec has two updated responsibilities:
1. it must respect any existing already buffered data.
2. it must write to the buffer if data is empty
@andrewrk
Copy link
Member Author

andrewrk commented Aug 6, 2025

@mrjbq7 I believe you ran that before I had this branch even working locally. I tried your example after latest push and it prints

decompressed 4160 bytes

@mrjbq7
Copy link
Contributor

mrjbq7 commented Aug 6, 2025

@mrjbq7 I believe you ran that before I had this branch even working locally. I tried your example after latest push and it prints

Sorry, I didn't realize it was WIP.

Can confirm this branch fixes both panic.z and fail_read.z!

@andrewrk
Copy link
Member Author

andrewrk commented Aug 6, 2025

Thanks for checking!

@andrewrk andrewrk enabled auto-merge (rebase) August 6, 2025 22:36
@andrewrk andrewrk merged commit 04fe1bf into master Aug 7, 2025
12 checks passed
@andrewrk andrewrk deleted the fill branch August 7, 2025 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants