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

StringReader off-by-one panic #20998

Closed
daddycocoaman opened this issue Mar 12, 2024 · 0 comments
Closed

StringReader off-by-one panic #20998

daddycocoaman opened this issue Mar 12, 2024 · 0 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@daddycocoaman
Copy link
Contributor

daddycocoaman commented Mar 12, 2024

Describe the bug

When attempting to read one byte at the end of the buffer with StringReader, V will panic. This is because the StringReader.needs_fill_until and StringReader.needs_fill will return true if the requested length is equal to or greater than the length of the buffer. The buffer should only be filled if the requested length is greater than the length of the buffer, not equal to.

Reproduction Steps

import io.string_reader

fn main() {

    source := "ABCD"
	mut reader := string_reader.StringReader.new(source: source)

	println(reader.read_all_bytes(true)!)

	reader = string_reader.StringReader.new(source: source)
        println(reader.read_bytes(1)!)
	println(reader.read_bytes(1)!)
	println(reader.read_bytes(1)!)
	println(reader.read_bytes(1)!)
}

Expected Behavior

StringReader will read the last byte of the buffer if the length to read requested is equal to the end of the buffer.

Current Behavior

[65, 66, 67, 68]
[65]
[66]
[67]
V panic: result not set (reader is not set)
v hash: a867ed6
print_backtrace_skipping_top_frames is not implemented

Possible Solution

I actually don't understand why the reader object disappears in the fill_buffer functions. Even if the buffer is at the end of the stream and too many bytes are requested, shouldn't it still be there? At any rate, the needs_fill functions should probably be changed to check for only greater than, not greater than or equal to.

Additional Information/Context

No response

V version

V 0.4.4 f789874.a867ed6

Environment details (OS name and version, etc.)

V full version: V 0.4.4 f789874.a867ed6
OS: windows, Microsoft Windows 11 Enterprise v26058 ARM 64-bit Processor
Processor: 8 cpus, 64bit, little endian, 

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@daddycocoaman daddycocoaman added the Bug This tag is applied to issues which reports bugs. label Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

1 participant