fix(lib): fix segfault on ts_query_new with incompatible grammar version, close #1318
This commit is contained in:
parent
0e26fbe5e6
commit
52e6c900c3
5 changed files with 72 additions and 38 deletions
|
|
@ -12,7 +12,7 @@ use std::process::Command;
|
|||
use std::sync::Mutex;
|
||||
use std::time::SystemTime;
|
||||
use std::{fs, mem};
|
||||
use tree_sitter::{Language, QueryError};
|
||||
use tree_sitter::{Language, QueryError, QueryErrorKind};
|
||||
use tree_sitter_highlight::HighlightConfiguration;
|
||||
use tree_sitter_tags::{Error as TagsError, TagsConfiguration};
|
||||
|
||||
|
|
@ -667,28 +667,31 @@ impl<'a> LanguageConfiguration<'a> {
|
|||
&injections_query,
|
||||
&locals_query,
|
||||
)
|
||||
.map_err(|error| {
|
||||
if error.offset < injections_query.len() {
|
||||
Self::include_path_in_query_error(
|
||||
error,
|
||||
&injection_ranges,
|
||||
&injections_query,
|
||||
0,
|
||||
)
|
||||
} else if error.offset < injections_query.len() + locals_query.len() {
|
||||
Self::include_path_in_query_error(
|
||||
error,
|
||||
&locals_ranges,
|
||||
&locals_query,
|
||||
injections_query.len(),
|
||||
)
|
||||
} else {
|
||||
Self::include_path_in_query_error(
|
||||
error,
|
||||
&highlight_ranges,
|
||||
&highlights_query,
|
||||
injections_query.len() + locals_query.len(),
|
||||
)
|
||||
.map_err(|error| match error.kind {
|
||||
QueryErrorKind::Language => Error::from(error),
|
||||
_ => {
|
||||
if error.offset < injections_query.len() {
|
||||
Self::include_path_in_query_error(
|
||||
error,
|
||||
&injection_ranges,
|
||||
&injections_query,
|
||||
0,
|
||||
)
|
||||
} else if error.offset < injections_query.len() + locals_query.len() {
|
||||
Self::include_path_in_query_error(
|
||||
error,
|
||||
&locals_ranges,
|
||||
&locals_query,
|
||||
injections_query.len(),
|
||||
)
|
||||
} else {
|
||||
Self::include_path_in_query_error(
|
||||
error,
|
||||
&highlight_ranges,
|
||||
&highlights_query,
|
||||
injections_query.len() + locals_query.len(),
|
||||
)
|
||||
}
|
||||
}
|
||||
})?;
|
||||
let mut all_highlight_names = self.highlight_names.lock().unwrap();
|
||||
|
|
|
|||
|
|
@ -133,6 +133,7 @@ pub const TSQueryError_TSQueryErrorNodeType: TSQueryError = 2;
|
|||
pub const TSQueryError_TSQueryErrorField: TSQueryError = 3;
|
||||
pub const TSQueryError_TSQueryErrorCapture: TSQueryError = 4;
|
||||
pub const TSQueryError_TSQueryErrorStructure: TSQueryError = 5;
|
||||
pub const TSQueryError_TSQueryErrorLanguage: TSQueryError = 6;
|
||||
pub type TSQueryError = u32;
|
||||
extern "C" {
|
||||
#[doc = " Create a new parser."]
|
||||
|
|
|
|||
|
|
@ -202,6 +202,7 @@ pub enum QueryErrorKind {
|
|||
Capture,
|
||||
Predicate,
|
||||
Structure,
|
||||
Language,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -1231,6 +1232,19 @@ impl Query {
|
|||
|
||||
// On failure, build an error based on the error code and offset.
|
||||
if ptr.is_null() {
|
||||
if error_type == ffi::TSQueryError_TSQueryErrorLanguage {
|
||||
return Err(QueryError {
|
||||
row: 0,
|
||||
column: 0,
|
||||
offset: 0,
|
||||
message: LanguageError {
|
||||
version: language.version(),
|
||||
}
|
||||
.to_string(),
|
||||
kind: QueryErrorKind::Language,
|
||||
});
|
||||
}
|
||||
|
||||
let offset = error_offset as usize;
|
||||
let mut line_start = 0;
|
||||
let mut row = 0;
|
||||
|
|
@ -2105,21 +2119,27 @@ impl fmt::Display for LanguageError {
|
|||
|
||||
impl fmt::Display for QueryError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"Query error at {}:{}. {}{}",
|
||||
self.row + 1,
|
||||
self.column + 1,
|
||||
match self.kind {
|
||||
QueryErrorKind::Field => "Invalid field name ",
|
||||
QueryErrorKind::NodeType => "Invalid node type ",
|
||||
QueryErrorKind::Capture => "Invalid capture name ",
|
||||
QueryErrorKind::Predicate => "Invalid predicate: ",
|
||||
QueryErrorKind::Structure => "Impossible pattern:\n",
|
||||
QueryErrorKind::Syntax => "Invalid syntax:\n",
|
||||
},
|
||||
self.message
|
||||
)
|
||||
let msg = match self.kind {
|
||||
QueryErrorKind::Field => "Invalid field name ",
|
||||
QueryErrorKind::NodeType => "Invalid node type ",
|
||||
QueryErrorKind::Capture => "Invalid capture name ",
|
||||
QueryErrorKind::Predicate => "Invalid predicate: ",
|
||||
QueryErrorKind::Structure => "Impossible pattern:\n",
|
||||
QueryErrorKind::Syntax => "Invalid syntax:\n",
|
||||
QueryErrorKind::Language => "",
|
||||
};
|
||||
if msg.len() > 0 {
|
||||
write!(
|
||||
f,
|
||||
"Query error at {}:{}. {}{}",
|
||||
self.row + 1,
|
||||
self.column + 1,
|
||||
msg,
|
||||
self.message
|
||||
)
|
||||
} else {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -131,6 +131,7 @@ typedef enum {
|
|||
TSQueryErrorField,
|
||||
TSQueryErrorCapture,
|
||||
TSQueryErrorStructure,
|
||||
TSQueryErrorLanguage,
|
||||
} TSQueryError;
|
||||
|
||||
/********************/
|
||||
|
|
|
|||
|
|
@ -2069,6 +2069,15 @@ TSQuery *ts_query_new(
|
|||
uint32_t *error_offset,
|
||||
TSQueryError *error_type
|
||||
) {
|
||||
if (
|
||||
!language ||
|
||||
language->version > TREE_SITTER_LANGUAGE_VERSION ||
|
||||
language->version < TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION
|
||||
) {
|
||||
*error_type = TSQueryErrorLanguage;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TSQuery *self = ts_malloc(sizeof(TSQuery));
|
||||
*self = (TSQuery) {
|
||||
.steps = array_new(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue