Fix panic and infinite loop in parser ()

This commit is contained in:
Mees Delzenne 2024-06-07 12:35:49 +02:00 committed by GitHub
parent 7253c01190
commit 2201b6d26d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 37 additions and 4 deletions
core/src/syn

View file

@ -88,6 +88,23 @@ impl Location {
}
pub fn range_of_span(source: &str, span: Span) -> Range<Self> {
if source.len() == span.offset as usize {
// EOF span
let (line_idx, column) = LineIterator::new(source)
.map(|(l, _)| l.len())
.enumerate()
.last()
.unwrap_or((0, 0));
return Self {
line: line_idx + 1,
column: column + 1,
}..Self {
line: line_idx + 1,
column: column + 2,
};
}
// Bytes of input before substr.
let offset = span.offset as usize;
let end = offset + span.len as usize;

View file

@ -213,8 +213,8 @@ impl<'a> Lexer<'a> {
Token {
kind: TokenKind::Eof,
span: Span {
offset: self.last_offset.saturating_sub(1),
len: 1,
offset: self.last_offset,
len: 0,
},
}
}

View file

@ -344,7 +344,6 @@ impl Parser<'_> {
match peek.kind {
t!("(") => {
self.pop_peek();
dbg!("called");
self.parse_inner_subquery(ctx, Some(peek.span)).await
}
t!("IF") => {

View file

@ -42,7 +42,10 @@ impl Parser<'_> {
loop {
match self.peek_kind() {
// consume any possible empty statements.
t!(";") => continue,
t!(";") => {
self.pop_peek();
continue;
}
t!("eof") => break,
_ => {
let stmt = ctx.run(|ctx| self.parse_stmt(ctx)).await?;

View file

@ -1,4 +1,13 @@
use crate::{sql, syn::parser::mac::test_parse};
mod limit;
mod stmt;
mod streaming;
mod value;
#[test]
fn multiple_semicolons() {
let res = test_parse!(parse_query, r#";;"#).unwrap();
let expected = sql::Query(sql::Statements(vec![]));
assert_eq!(res, expected);
}

View file

@ -167,3 +167,8 @@ fn scientific_number() {
assert!(matches!(res, Value::Number(Number::Float(_))));
assert_eq!(res.to_string(), "0.000097f")
}
#[test]
fn empty_string() {
test_parse!(parse_value, "").unwrap_err();
}