Skip to content

Commit

Permalink
fix: issue with for .. in .. loop returning undefined value instead…
Browse files Browse the repository at this point in the history
… of false.

Closes #87
  • Loading branch information
plusvic committed Feb 29, 2024
1 parent 889a7cd commit ce6f7e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/src/compiler/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,17 +1617,29 @@ fn emit_for_in_range(
|ctx, instr, n, loop_end| {
// Set n = upper_bound - lower_bound + 1;
set_var(ctx, instr, n, |ctx, instr| {
emit_expr(ctx, instr, &mut range.upper_bound);
emit_expr(ctx, instr, &mut range.lower_bound);
// Catch undefined values in upper_bound and lower_bound
// expressions. In such cases n = 0.
catch_undef(
ctx,
I64,
instr,
|ctx, instr| {
emit_expr(ctx, instr, &mut range.upper_bound);
emit_expr(ctx, instr, &mut range.lower_bound);

// Store lower_bound in temp variable, without removing
// it from the stack.
instr.local_tee(ctx.wasm_symbols.i64_tmp);
// Store lower_bound in temp variable, without removing
// it from the stack.
instr.local_tee(ctx.wasm_symbols.i64_tmp);

// Compute upper_bound - lower_bound + 1.
instr.binop(BinaryOp::I64Sub);
instr.i64_const(1);
instr.binop(BinaryOp::I64Add);
// Compute upper_bound - lower_bound + 1.
instr.binop(BinaryOp::I64Sub);
instr.i64_const(1);
instr.binop(BinaryOp::I64Add);
},
|instr| {
instr.i64_const(0);
},
)
});

// If n <= 0, exit from the loop.
Expand Down
6 changes: 6 additions & 0 deletions lib/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ fn for_in() {
condition_true!(r#"for 2 s in ("foo", "bar", "baz") : (s contains "ba")"#);
condition_true!(r#"for all x in (1.0, 2.0, 3.0) : (x >= 1.0)"#);
condition_true!(r#"for none x in (1.0, 2.0, 3.0) : (x > 4.0)"#);

// https://github.com/VirusTotal/yara-x/issues/87
#[cfg(feature = "test_proto2-module")]
condition_true!(
r#"not for any i in (0..test_proto2.int64_undef) : (true)"#
);
}

#[test]
Expand Down

0 comments on commit ce6f7e8

Please sign in to comment.