Skip to content

Commit

Permalink
Merge pull request #270 from avast/feat/remove-variables
Browse files Browse the repository at this point in the history
feat: Added Rule::removeVariables() to remove all variables from both AST & token stream
  • Loading branch information
metthal authored Jan 14, 2025
2 parents bbf9ff0 + ece2e4c commit fd31bb2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/yaramod/types/rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Rule
void removeString(const std::string& id);
void addTag(const std::string& tag);
void removeTags(const std::string& tag);
void removeVariables();
/// @}

private:
Expand Down
3 changes: 2 additions & 1 deletion src/python/yaramod_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ void addBasicClasses(py::module& module)
.def("remove_string", &Rule::removeString)
.def("get_meta_with_name", py::overload_cast<const std::string&>(&Rule::getMetaWithName), py::return_value_policy::reference)
.def("add_tag", &Rule::addTag)
.def("remove_tags", py::overload_cast<const std::string&>(&Rule::removeTags));
.def("remove_tags", py::overload_cast<const std::string&>(&Rule::removeTags))
.def("remove_variables", &Rule::removeVariables);

py::class_<Meta>(module, "Meta")
.def_property("key", &Meta::getKey, &Meta::setKey)
Expand Down
20 changes: 20 additions & 0 deletions src/types/rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,24 @@ void Rule::removeTags(TokenType type)
}
}

/**
* Removes all variables from a rule.
*/
void Rule::removeVariables()
{
auto firstToken = getFirstTokenIt();
auto lastToken = getLastTokenIt();

auto varBegin = _tokenStream->find(TokenType::VARIABLES, firstToken, lastToken);
if (varBegin == lastToken)
return;

auto varEnd = _tokenStream->find(TokenType::CONDITION, varBegin, lastToken);
if (varEnd == lastToken)
return;

setVariables({});
_tokenStream->erase(varBegin, varEnd);
}

}
70 changes: 70 additions & 0 deletions tests/cpp/parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8753,5 +8753,75 @@ rule expression_array
ASSERT_NE(parent_itr->getElements()[2]->as<BoolLiteralExpression>(), nullptr);
}

TEST_F(ParserTests,
RemoveVariablesNone) {
prepareInput(
R"(
rule remove_variables
{
condition:
false
}
)");

EXPECT_TRUE(driver.parse(input, ParserMode::Incomplete));
ASSERT_EQ(1u, driver.getParsedFile().getRules().size());

const auto& rule = driver.getParsedFile().getRules()[0];

rule->removeVariables();

EXPECT_EQ(0u, rule->getVariables().size());
EXPECT_EQ(R"(rule remove_variables {
condition:
false
})", rule->getText());

EXPECT_EQ(R"(
rule remove_variables
{
condition:
false
}
)", driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
RemoveVariables) {
prepareInput(
R"(
rule remove_variables
{
variables:
a = 1
b = 2
c = a + b
condition:
false
}
)");

EXPECT_TRUE(driver.parse(input, ParserMode::Incomplete));
ASSERT_EQ(1u, driver.getParsedFile().getRules().size());

const auto& rule = driver.getParsedFile().getRules()[0];

rule->removeVariables();

EXPECT_EQ(0u, rule->getVariables().size());
EXPECT_EQ(R"(rule remove_variables {
condition:
false
})", rule->getText());

EXPECT_EQ(R"(
rule remove_variables
{
condition:
false
}
)", driver.getParsedFile().getTextFormatted());
}

}
}

0 comments on commit fd31bb2

Please sign in to comment.