Skip to content

Commit

Permalink
feat: the relaxed_re_syntax option now handles invalid escape seque…
Browse files Browse the repository at this point in the history
…nces inside character classes
  • Loading branch information
plusvic committed May 15, 2024
1 parent 14b4efe commit f423fbd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/src/compiler/ir/ast2ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ pub(in crate::compiler) fn expr_from_ast(
ast::Expr::LiteralFloat(literal) => Ok(Expr::Const(
TypeValue::const_float_from(literal.value))),

ast::Expr::LiteralString(literal) => Ok(Expr::Const(TypeValue::const_string_from(literal.value.as_bytes()))),
ast::Expr::LiteralString(literal) => Ok(Expr::Const(
TypeValue::const_string_from(literal.value.as_bytes()))),

ast::Expr::Regexp(regexp) => {
re::parser::Parser::new()
Expand Down
28 changes: 25 additions & 3 deletions lib/src/compiler/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,19 +491,41 @@ fn globals_json() {
}

#[test]
fn invalid_escape_sequences() {
fn relaxed_re_syntax() {
let mut compiler = Compiler::new();

compiler.relaxed_re_syntax(true);
compiler
.add_source(r#"rule test { strings: $a = /\Release/ condition: $a }"#)
.add_source(r#"rule test_1 { strings: $a = /\X\Y\Z/ condition: $a }"#)
.unwrap()
.add_source(r#"rule test_2 { strings: $a = /xyz{/ condition: $a }"#)
.unwrap()
.add_source(r#"rule test_3 { strings: $a = /xyz[\>]/ condition: $a }"#)
.unwrap();

let rules = compiler.build();

assert_eq!(
Scanner::new(&rules)
.scan(b"Release")
.scan(b"XYZ")
.expect("scan should not fail")
.matching_rules()
.len(),
1
);

assert_eq!(
Scanner::new(&rules)
.scan(b"xyz{")
.expect("scan should not fail")
.matching_rules()
.len(),
1
);

assert_eq!(
Scanner::new(&rules)
.scan(b"xyz>")
.expect("scan should not fail")
.matching_rules()
.len(),
Expand Down
3 changes: 2 additions & 1 deletion lib/src/re/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ impl Parser {
break Err(err);
}
match err.kind() {
ErrorKind::EscapeUnrecognized => {
ErrorKind::EscapeUnrecognized
| ErrorKind::ClassEscapeInvalid => {
let span = err.span();
let mut s = re_src.into_owned();
// Remove the backslash (\) from the original regexp.
Expand Down

0 comments on commit f423fbd

Please sign in to comment.