Skip to content

Commit

Permalink
fix: show optional expressions in ir
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Mar 1, 2024
1 parent c18f67d commit 030b954
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/redos/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Expr {
/// Non-greedy means as little as possible, e.g. `.*?b` would match only `ab` in `abab`.
greedy: bool,
},
/// Optional expression, e.g. `a?` means `a` is optional
Optional(Box<Expr>),
/// Atomic non-capturing group, e.g. `(?>ab|a)` in text that contains `ab` will match `ab` and
/// never backtrack and try `a`, even if matching fails after the atomic group.
AtomicGroup(Box<Expr>),
Expand Down Expand Up @@ -82,13 +84,19 @@ pub fn to_expr(tree: &ExprTree, expr: &RegexExpr, config: &VulnerabilityConfig)
} => {
let range = hi - lo;

if range > config.max_quantifier {
let expression = if range > config.max_quantifier {
to_expr(tree, child, config).map(|child| Expr::Repeat {
child: Box::new(child),
greedy: *greedy,
})
} else {
to_expr(tree, child, config)
};

if *lo == 0 {
expression.map(|e| Expr::Optional(Box::new(e)))
} else {
expression
}
}
// Delegates essentially forcibly match some string, so we can turn them into a token
Expand Down

0 comments on commit 030b954

Please sign in to comment.