Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,13 @@ impl PyDocument {
match self.inner.query_exact(&r) {
Ok(Some(feature)) => {
let span = feature.location.byte_span;
// Note: span.0 <= span.1 is guaranteed by tree-sitter node
// ranges, so we only check bounds and UTF-8 alignment.
// Note: span.0 <= span.1 is guaranteed by tree-sitter node construction.
if span.1 > source.len()
|| !source.is_char_boundary(span.0)
|| !source.is_char_boundary(span.1)
{
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
"Feature span is not valid in source",
"Feature span is out of bounds or not aligned to UTF-8 character boundaries",
Comment thread
nathanjmcdougall marked this conversation as resolved.
));
}
let raw = &source[span.0..span.1];
Expand Down Expand Up @@ -295,9 +294,11 @@ fn apply_insert_at(
.ok_or_else(|| format!("insert_at: item at index {resolved} not found"))?;

let item_start = item_feature.location.byte_span.0;
// Note: no reversed-span check needed; tree-sitter nodes guarantee start <= end.
if item_start > source.len() || !source.is_char_boundary(item_start) {
return Err("Feature span is not valid in source".to_string());
return Err(
"Feature span is out of bounds or not aligned to UTF-8 character boundaries"
.to_string(),
);
Comment thread
nathanjmcdougall marked this conversation as resolved.
}
let line_start = source[..item_start]
.rfind('\n')
Expand Down Expand Up @@ -358,11 +359,13 @@ fn apply_complex_replace(
.map_err(|e| format!("Query failed: {e}"))?;

let span = feature.location.byte_span;
// Note: span.0 <= span.1 is guaranteed by tree-sitter node ranges,
// so we only check bounds and UTF-8 alignment.
// Note: span.0 <= span.1 is guaranteed by tree-sitter node construction.
if span.1 > source.len() || !source.is_char_boundary(span.0) || !source.is_char_boundary(span.1)
{
return Err("Feature span is not valid in source".to_string());
return Err(
"Feature span is out of bounds or not aligned to UTF-8 character boundaries"
Comment thread
nathanjmcdougall marked this conversation as resolved.
.to_string(),
);
}

let content_with_ws = doc.extract_with_leading_whitespace(&feature);
Expand Down
Loading