Skip to content

Commit 9d6e81d

Browse files
fix: replace unsafe byte indexing in parse_value with .get() + error return
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
1 parent 379171a commit 9d6e81d

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/document.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,20 @@ impl PyDocument {
9494
match self.inner.query_exact(&r) {
9595
Ok(Some(feature)) => {
9696
let span = feature.location.byte_span;
97-
let raw = &source[span.0..span.1];
97+
let raw = source.get(span.0..span.1).ok_or_else(|| {
98+
PyErr::new::<pyo3::exceptions::PyValueError, _>(
99+
"Feature span is not valid in source",
100+
)
101+
})?;
98102
// Calculate the column offset (in bytes) of the value
99103
// start relative to the beginning of its line, so we can
100104
// dedent continuation lines.
101-
let line_start = source[..span.0].rfind('\n').map(|nl| nl + 1).unwrap_or(0);
105+
let prefix = source.get(..span.0).ok_or_else(|| {
106+
PyErr::new::<pyo3::exceptions::PyValueError, _>(
107+
"Feature span start is not valid in source",
108+
)
109+
})?;
110+
let line_start = prefix.rfind('\n').map(|nl| nl + 1).unwrap_or(0);
102111
let col = span.0 - line_start;
103112
if col == 0 {
104113
raw.to_string()

0 commit comments

Comments
 (0)