Skip to content

defaultDiscard leaves buffer in bad state #25620

@marler8997

Description

@marler8997

Zig Version

0.15.2

Steps to Reproduce and Observed Behavior

Run the following test:

test "discardAll that has to call discard multiple times on an indirect reader" {
    var fr: Reader = .fixed("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    var indirect_buffer: [3]u8 = undefined;
    var tri: std.testing.ReaderIndirect = .init(&fr, &indirect_buffer);
    const r = &tri.interface;

    try r.discardAll(10);
}

An assert trips in defaultDiscard:

std\Io\Reader.zig:199:11: 0x7ff6564c14d9 in defaultDiscard
    assert(r.seek == r.end);
          ^

Expected Behavior

Test should pass and no assert should trip.

Notes

The defaultDiscard implementation rightly asserts that the buffer is empty before being called, however, it goes on to call stream which for some implementations can read into the buffer. Consequently, if you call discardAll with a size bigger than the buffer, it can call defaultDiscard multiple times in succession which will trigger the assert if the previous one left data in the buffer. Solution is probably to have defaultDiscard clear the buffer after calling stream.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions