Fix Reader.peek() IndexError when index exceeds buffer length#912
Open
Fix Reader.peek() IndexError when index exceeds buffer length#912
Conversation
When peek() is called with an index beyond the available data, update() may not be able to add enough data to the buffer (e.g. for string inputs where raw_buffer is None). The second buffer access then raises an IndexError instead of returning the '\0' sentinel. Add a bounds check after update() so that peek() returns '\0' when the requested position is past the end of the buffer, consistent with how the null terminator is used throughout the parser. Fixes yaml#904
Author
|
Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #904
Problem
Reader.peek(index)raisesIndexErrorwhen the requestedindexexceeds the available data in the buffer. This happens because after callingupdate(), the buffer may still not have enough data (e.g., for string inputs whereraw_bufferisNoneandupdate()returns immediately). The second buffer access then crashes.Fix
Added a bounds check after
update()inpeek(). If the requested position is still past the end of the buffer, return'\0'instead of indexing into the buffer. This is consistent with how'\0'is used as the end-of-stream sentinel throughout the parser.Tests
Added tests covering:
All 1280 existing legacy tests plus 3 existing modern tests continue to pass.