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
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue