Skip to content

Commit

Permalink
fix regex panics
Browse files Browse the repository at this point in the history
  • Loading branch information
TommYDeeee committed Apr 2, 2024
1 parent c3ca60c commit 98d00a0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub(crate) enum LogosToken {
#[regex(r"/(([^\\/\n])|(\\.))+/[a-zA-Z0-9]*", |lex| lex.slice().to_string())]
Regexp(String),
// Hexadecimal string
#[regex(r"\{[0-9A-Fa-f?~()|\[\] -]*\}", |lex| lex.slice().to_string())]
#[regex(r"=\s\{[\s0-9A-Fa-f?~()|\[\] -]*\}", |lex| lex.slice().to_string())]
HexString(String),
// Strings
#[regex(r#""([^"\n]|\\["\\])*""#, |lex| lex.slice().to_string())]
Expand Down Expand Up @@ -399,7 +399,8 @@ fn process_hex_string_token(hex_string: String) -> Vec<(SyntaxKind, usize)> {
let mut chars = hex_string.chars().peekable();
while let Some(ch) = chars.next() {
match ch {
' ' => tokens.push((SyntaxKind::WHITESPACE, 1)),
'=' => tokens.push((SyntaxKind::ASSIGN, 1)),
' ' | '\t' | '\n' | '\r' => tokens.push((SyntaxKind::WHITESPACE, 1)),
'-' => tokens.push((SyntaxKind::HYPHEN, 1)),
'{' => tokens.push((SyntaxKind::L_BRACE, 1)),
'}' => tokens.push((SyntaxKind::R_BRACE, 1)),
Expand Down
31 changes: 14 additions & 17 deletions src/parser/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub(super) fn meta_body(p: &mut Parser) {
}
_ => {
p.error("expected a valid metadata value");
m.abandon(p);
return;
}
}
Expand Down Expand Up @@ -599,7 +600,7 @@ fn primary_expr(p: &mut Parser) -> Option<CompletedMarker> {
T!['('] => {
p.bump(T!['(']);
expr(p, None, 1);
p.bump(T![')']);
p.expect(T![')']);
}
T![identifier] => {
p.bump(T![identifier]);
Expand Down Expand Up @@ -833,7 +834,7 @@ fn primary_expr_length(p: &mut Parser, len: usize, parentheses_count: &mut i32)
T![float_lit] | T![int_lit] | T![string_lit] | T![filesize] | T![entrypoint] => len + 1,
T![/] => regex_pattern_length(p, len),
T![-] | T![~] => term_length(p, len + 1, parentheses_count),
T!['('] => expr_length(p, len, parentheses_count, false),
T!['('] => expr_length(p, len, parentheses_count),
T![variable_count] => variable_count_length(p, len),
T![variable_offset] => variable_offset_length(p, len),
T![variable_length] => variable_length_length(p, len),
Expand Down Expand Up @@ -873,17 +874,17 @@ fn variable_count_length(p: &mut Parser, mut len: usize) -> usize {

fn range_length(p: &mut Parser, mut len: usize) -> usize {
len += 1;
len = expr_length(p, len, &mut 0, true);
len = expr_length(p, len, &mut 0);
len += 1;
len = expr_length(p, len, &mut 0, true);
len = expr_length(p, len, &mut 0);
len + 1
}

fn variable_offset_length(p: &mut Parser, mut len: usize) -> usize {
len += 1;
if p.nth(len) == T!['['] {
len += 1;
len = expr_length(p, len, &mut 0, false);
len = expr_length(p, len, &mut 0);
len += 1;
}
len
Expand All @@ -893,7 +894,7 @@ fn variable_length_length(p: &mut Parser, mut len: usize) -> usize {
len += 1;
if p.nth(len) == T!['['] {
len += 1;
len = expr_length(p, len, &mut 0, false);
len = expr_length(p, len, &mut 0);
len += 1;
}
len
Expand All @@ -905,18 +906,19 @@ fn term_length(p: &mut Parser, mut len: usize, parentheses_count: &mut i32) -> u
match p.nth(len) {
T!['['] => {
len += 1;
len = expr_length(p, len, parentheses_count, false);
len = expr_length(p, len, parentheses_count);
len + 1
}
T!['('] => {
*parentheses_count += 1;
len += 1;
if p.nth(len) == T![')'] {
len + 1
} else {
len = expr_length(p, len, parentheses_count, true);
len = expr_length(p, len, parentheses_count);
while p.nth(len) == T![,] {
len += 1;
len = expr_length(p, len, parentheses_count, true);
len = expr_length(p, len, parentheses_count);
}
len + 1
}
Expand All @@ -925,12 +927,7 @@ fn term_length(p: &mut Parser, mut len: usize, parentheses_count: &mut i32) -> u
}
}

fn expr_length(
p: &mut Parser,
mut len: usize,
parentheses_count: &mut i32,
expression_paranthesis: bool,
) -> usize {
fn expr_length(p: &mut Parser, mut len: usize, parentheses_count: &mut i32) -> usize {
// Check if the expression starts with `(`
if p.nth(len) == T!['('] {
len += 1;
Expand All @@ -953,11 +950,11 @@ fn expr_length(
{
len += 1;

len = expr_length(p, len, parentheses_count, false);
len = expr_length(p, len, parentheses_count);
}

// Check if the expression ends with `)`
if p.nth(len) == T![')'] && !expression_paranthesis {
if p.nth(len) == T![')'] {
len += 1;
*parentheses_count -= 1;
}
Expand Down
2 changes: 1 addition & 1 deletion yara.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ OfExpr =
ForExpr =
(
'for' Quantifier 'of' ('them' | PatternIdentTuple)
| 'for' Quantifier 'of' (IdentifierNode (',' IdentifierNode)*) 'in' Iterable
| 'for' Quantifier (IdentifierNode (',' IdentifierNode)*) 'in' Iterable
)
':' '(' BooleanExpr ')'

Expand Down

0 comments on commit 98d00a0

Please sign in to comment.