Skip to content

Commit

Permalink
fix: for .. of .. loops not working correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Aug 1, 2023
1 parent 16e15df commit b0f38cc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
3 changes: 1 addition & 2 deletions yara-x/src/compiler/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,6 @@ fn emit_of_expr_tuple(
of: &mut Of,
) {
let expressions = cast!(&mut of.items, OfItems::BoolExprTuple);

let next_item = of.stack_frame.new_var(Type::Bool);
let num_expressions = expressions.len();
let mut expressions = expressions.iter_mut();
Expand Down Expand Up @@ -1357,7 +1356,7 @@ fn emit_for_of_pattern_set(
) {
let num_patterns = for_of.pattern_set.len();
let mut pattern_ids = for_of.pattern_set.iter();
let next_pattern_id = for_of.stack_frame.new_var(Type::Integer);
let next_pattern_id = for_of.variable;

emit_for(
ctx,
Expand Down
11 changes: 8 additions & 3 deletions yara-x/src/compiler/ir/ast2ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ fn of_expr_from_ast(
of: &ast::Of,
) -> Result<Expr, CompileError> {
let quantifier = quantifier_from_ast(ctx, &of.quantifier)?;

// Create new stack frame with 5 slots:
// 1 slot for the loop variable, a bool in this case.
// 4 up to slots used for loop control variables (see: emit::emit_for)
let stack_frame = ctx.vars.new_frame(5);

let (items, num_items) = match &of.items {
Expand Down Expand Up @@ -665,8 +667,10 @@ fn for_of_expr_from_ast(
) -> Result<Expr, CompileError> {
let quantifier = quantifier_from_ast(ctx, &for_of.quantifier)?;
let pattern_set = pattern_set_from_ast(ctx, &for_of.pattern_set);

let mut stack_frame = ctx.vars.new_frame(4);
// Create new stack frame with 5 slots:
// 1 slot for the loop variable, a pattern ID in this case
// 4 up to slots used for loop control variables (see: emit::emit_for)
let mut stack_frame = ctx.vars.new_frame(5);
let next_pattern_id = stack_frame.new_var(Type::Integer);
let mut loop_vars = SymbolTable::new();

Expand All @@ -692,6 +696,7 @@ fn for_of_expr_from_ast(
pattern_set,
condition,
stack_frame,
variable: next_pattern_id,
})))
}

Expand Down
1 change: 1 addition & 0 deletions yara-x/src/compiler/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ pub(in crate::compiler) struct Of {
/// `for 1 of ($a,$b) : (..)`)
pub(in crate::compiler) struct ForOf {
pub quantifier: Quantifier,
pub variable: Var,
pub pattern_set: Vec<PatternId>,
pub condition: Expr,
pub stack_frame: VarStackFrame,
Expand Down
26 changes: 26 additions & 0 deletions yara-x/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2100,6 +2100,32 @@ fn for_of() {
"#,
b"foobar"
);

rule_true!(
r#"
rule test {
strings:
$a = "foo"
$b = "bar"
condition:
for 1 of them : ( # == 2 )
}
"#,
b"foobarbar"
);

rule_true!(
r#"
rule test {
strings:
$a = "foo"
$b = "bar"
condition:
for 1 of them : ( @ > 0 )
}
"#,
b"foobarbar"
);
}

#[test]
Expand Down

0 comments on commit b0f38cc

Please sign in to comment.