Skip to content

Commit

Permalink
Merge pull request #216 from avast/strings-implicit-cast-to-bools
Browse files Browse the repository at this point in the history
Allow strings to have implicit conversion to bool same as YARA
  • Loading branch information
metthal committed May 30, 2022
2 parents 93c9579 + a0d8a17 commit 6dda35b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/yaramod/builder/yara_expression_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class YaraExpressionBuilder

/// @name Detection methods
/// @{
bool canBeBool() const { return _expr->isBool() || _expr->isFloat() || _expr->isInt() || _expr->isUndefined(); }
bool canBeBool() const { return _expr->isBool() || _expr->isFloat() || _expr->isInt() || _expr->isString() || _expr->isUndefined(); }
/// @}

/// @name Builder method
Expand Down
30 changes: 30 additions & 0 deletions tests/cpp/builder_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2140,5 +2140,35 @@ rule of_rule_builder
)", yaraFile->getTextFormatted());
}

TEST_F(BuilderTests,
StringAsBoolInConditionWorks) {
auto cond = (stringVal("abc") && stringVal("def")).get();

YaraRuleBuilder newRule;
auto rule = newRule
.withName("rule1")
.withCondition(std::move(cond))
.get();

YaraFileBuilder newFile;
auto yaraFile = newFile
.withRule(std::move(rule))
.get(true);

ASSERT_NE(nullptr, yaraFile);
EXPECT_EQ(R"(rule rule1 {
condition:
"abc" and "def"
})", yaraFile->getText());

EXPECT_EQ(R"(rule rule1
{
condition:
"abc" and
"def"
}
)", yaraFile->getTextFormatted());
}

}
}

0 comments on commit 6dda35b

Please sign in to comment.