Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix meta placement after comment. #188

Merged
merged 2 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog

* Fix adding meta to add it after comment of last present meta if present ([#102](https://github.com/avast/yaramod/issues/102), [#188](https://github.com/avast/yaramod/pull/188))
* Separate includes and imports with blank lines ([#187](https://github.com/avast/yaramod/pull/187), [#130](https://github.com/avast/yaramod/issues/130))

# v3.11.0 (2021-08-30)
Expand Down
10 changes: 9 additions & 1 deletion src/types/rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,15 @@ void Rule::addMeta(const std::string& name, const Literal& value)
else
{
insert_before = _metas.back().getValueTokenIt();
++insert_before;
while (
insert_before->getType() != TokenType::NEW_LINE &&
insert_before->getType() != TokenType::STRINGS &&
insert_before->getType() != TokenType::CONDITION &&
insert_before->getType() != TokenType::RULE_END &&
insert_before != _tokenStream->end()
) {
++insert_before;
}
}
_tokenStream->emplace(insert_before, TokenType::NEW_LINE, _tokenStream->getNewLineStyle());
auto itKey = _tokenStream->emplace(insert_before, TokenType::META_KEY, name);
Expand Down
30 changes: 30 additions & 0 deletions tests/cpp/yaramod_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ rule rule_with_added_metas
)", yarafile->getTextFormatted());
}

TEST_F(YaramodTests,
AddMetaToRulesWithComments) {
yaramod::Yaramod ymod;

std::stringstream input;
input << R"(
rule rule_with_commented_meta {
meta:
author = "Mr. Avastien" // added by Service
condition:
true
})";
auto yarafile = ymod.parseStream(input);

ASSERT_EQ(1u, yarafile->getRules().size());
const auto& rule = yarafile->getRules()[0];

rule->addMeta("bool_meta", Literal(false));
EXPECT_EQ(R"(
rule rule_with_commented_meta
{
meta:
author = "Mr. Avastien" // added by Service
bool_meta = false
condition:
true
}
)", yarafile->getTextFormatted());
}

TEST_F(YaramodTests,
SetMeta) {
yaramod::Yaramod ymod;
Expand Down