Merge pull request #2280 from smoelius/query-new-oob

Fix OOB in `Query::new`
This commit is contained in:
Andrew Hlynskyi 2023-05-20 11:45:07 +03:00 committed by GitHub
commit b4c73a2b0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View file

@ -4575,3 +4575,19 @@ fn test_query_max_start_depth() {
}
});
}
#[test]
fn test_query_error_does_not_oob() {
let language = get_language("javascript");
assert_eq!(
Query::new(language, "(clas").unwrap_err(),
QueryError {
row: 0,
offset: 1,
column: 1,
kind: QueryErrorKind::NodeType,
message: "clas".to_string()
}
);
}

View file

@ -1401,7 +1401,7 @@ impl Query {
let suffix = source.split_at(offset).1;
let end_offset = suffix
.find(|c| !char::is_alphanumeric(c) && c != '_' && c != '-')
.unwrap_or(source.len());
.unwrap_or(suffix.len());
message = suffix.split_at(end_offset).0.to_string();
kind = match error_type {
ffi::TSQueryError_TSQueryErrorNodeType => QueryErrorKind::NodeType,