Skip to content

Commit

Permalink
Fix parser panics in edge cases
Browse files Browse the repository at this point in the history
Will partially address #5049
  • Loading branch information
IGI-111 committed Sep 29, 2023
1 parent fc9b6aa commit 2dec2a6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
59 changes: 59 additions & 0 deletions sway-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,62 @@ pub fn parse_module_kind(
}
parser.parse()
}

#[cfg(test)]
mod tests {
use crate::*;

#[test]
fn parse_invalid() {
// just make sure these do not panic
let _res = parse_file(&Handler::default(), Arc::from("script; fn main(256߄"), None);
let _res = parse_file(
&Handler::default(),
Arc::from(
"script;
fn karr() {
let c: f828 = 0x00000000000000000000000vncifxp;
abi Zezybt {
#[mfzbezc, storage(r#
true }
}
cug",
),
None,
);
let _res = parse_file(
&Handler::default(),
Arc::from(
"script;
corefn main() {
let a: b256 = 0x000>0000000scri s = \"flibrary I24;
use core::primitives::*;
use std::assert::assert;
///\u{7eb}",
),
None,
);
let _res = parse_file(
&Handler::default(),
Arc::from("script; \"\u{7eb}\u{7eb}"),
None,
);
}
#[test]
fn parse_overflow() {
let _res = parse_file(
&Handler::default(),
Arc::from(
"script;
fn main() {
let a: b256 =
0x000000([[[[[[[[[[[[[[[[[[[[[[[[[[[[[000000000000000000000009000(([[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[",
),
None,
);
}
}
2 changes: 1 addition & 1 deletion sway-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl<'a, 'e> Parser<'a, 'e> {
(self.full_span.end() + 1).saturating_sub(trim_offset),
self.full_span.source_id().cloned(),
)
.unwrap_or(Span::dummy())
}
.unwrap(),
};
self.emit_error_with_span(kind, span)
}
Expand Down
21 changes: 17 additions & 4 deletions sway-parse/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ pub fn lex_commented(
character = next_character;
index = next_index;
}
if !(character.is_xid_start() || character == '_') {
let kind = LexErrorKind::InvalidCharacter {
position: index,
character,
};
let span = span_one(&l, index, character);
error(l.handler, LexError { kind, span });
continue;
}
}

// Don't accept just `_` as an identifier.
Expand Down Expand Up @@ -466,10 +475,14 @@ fn lex_string(
},
)
};
let (next_index, next_character) = l
.stream
.next()
.ok_or_else(|| unclosed_string_lit(l, l.src.len() - 1))?;
let (next_index, next_character) = l.stream.next().ok_or_else(|| {
// last character may not be a unicode boundary
let mut end = l.src.len() - 1;
while !l.src.is_char_boundary(end) {
end -= 1;
}
unclosed_string_lit(l, end)
})?;
parsed.push(match next_character {
'\\' => parse_escape_code(l)
.map_err(|e| e.unwrap_or_else(|| unclosed_string_lit(l, l.src.len())))?,
Expand Down

0 comments on commit 2dec2a6

Please sign in to comment.