fix: replace unsafe byte indexing in parse_value with .get() + error return#45
Merged
nathanjmcdougall merged 2 commits intoMay 25, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR prevents a potential Rust panic (and resulting Python process crash) in PyDocument::parse_value by replacing direct UTF-8 string byte slicing with safe .get(..) range access that returns a recoverable PyValueError when spans are invalid.
Changes:
- Replaced
&source[span.0..span.1]withsource.get(span.0..span.1).ok_or_else(...)?to avoid panics on invalid UTF-8 boundaries/out-of-range spans. - Replaced
source[..span.0]with a safe.get(..span.0)-based prefix extraction before computingline_start.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bfc7d81 to
dbaec3d
Compare
Direct byte indexing (source[span.0..span.1] and source[..span.0]) panics if yamlpath returns a span misaligned to UTF-8 boundaries, crashing the Python process. Replace with upfront is_char_boundary + bounds checks that return a recoverable error, then use direct indexing safely. Applied consistently to all 5 sites in document.rs: parse_value, apply_insert_at, and apply_complex_replace. Closes #44
dbaec3d to
74cdff7
Compare
…dexing-in-parse_value-can-panic-instead-of-returning-error # Conflicts: # src/document.rs
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.
Direct byte indexing (source[span.0..span.1] and source[..span.0]) panics if yamlpath returns a span misaligned to UTF-8 boundaries, crashing the Python process. Replace with .get() to return a recoverable PyValueError, matching the existing safe pattern in extract().
Closes #44