Skip to content
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

Fix a slice index error when seeking past end of file, but within the last cached chunk #2722

Merged
merged 1 commit into from Apr 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion stream/vibe/stream/bufferedstream.d
Expand Up @@ -366,7 +366,7 @@ struct BufferedStream(S) {
ubyte[1] dummy;
state.peekBuffer = null;
state.iterateChunks!ubyte(offset, dummy[], (offset, chunk, scope bytes, buffer, buffer_begin, buffer_end) @safe nothrow {
if (buffer >= 0) {
if (buffer >= 0 && buffer_begin < state.buffers[buffer].fill) {
state.peekBuffer = state.buffers[buffer].memory[buffer_begin .. state.buffers[buffer].fill];
state.touchBuffer(buffer);
}
Expand Down Expand Up @@ -569,6 +569,26 @@ mixin validateRandomAccessStream!(BufferedStream!RandomAccessStream);
assert(ob[0] == 1);
}

@safe unittest { // regression seeking past end of file within the last chunk
import std.exception : assertThrown;
import vibe.stream.memory : createMemoryStream;
import vibe.stream.operations : readAll;

auto buf = new ubyte[](256);
foreach (i, ref b; buf) b = cast(ubyte)i;
auto str = createMemoryStream(buf, true, 1);
auto bstr = bufferedStream(str, 16, 4);

ubyte[1] ob;
bstr.read(ob[]);
assert(ob[0] == 0);
bstr.seek(10);
bstr.write([cast(ubyte)1]);
bstr.seek(10);
bstr.read(ob[]);
assert(ob[0] == 1);
}

unittest {
static assert(isTruncatableStream!(BufferedStream!TruncatableStream));
static assert(isClosableRandomAccessStream!(BufferedStream!ClosableRandomAccessStream));
Expand Down