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

ConfigParser: fix use-out-of-scope leaks #1661

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
48 changes: 32 additions & 16 deletions libdnf/conf/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,32 @@ std::pair<std::string, size_t> ConfigParser::substitute_expression(const std::st
const auto & variable_key = res.substr(pos_variable, pos_after_variable - pos_variable);
const auto variable_mapping = substitutions.find(variable_key);

const std::string * variable_value = nullptr;
// No std::optional here.
bool variable_value_has_value{false};
std::string variable_value{""};

if (variable_mapping == substitutions.end()) {
if (variable_key == "releasever_major" || variable_key == "releasever_minor") {
const auto releasever_mapping = substitutions.find("releasever");
if (releasever_mapping != substitutions.end()) {
const auto & releasever_split = ConfigParser::split_releasever(releasever_mapping->second);
if (variable_key == "releasever_major") {
variable_value = &std::get<0>(releasever_split);
variable_value = std::get<0>(releasever_split);
variable_value_has_value = true;
} else {
variable_value = &std::get<1>(releasever_split);
variable_value = std::get<1>(releasever_split);
variable_value_has_value = true;
}
}
}
} else {
variable_value = &variable_mapping->second;
variable_value = variable_mapping->second;
variable_value_has_value = true;
}

const std::string * subst_str = nullptr;
// No std::optional here
std::string subst_str{""};
bool subst_str_has_value{false};

size_t pos_after_variable_expression;

Expand Down Expand Up @@ -153,20 +160,23 @@ std::pair<std::string, size_t> ConfigParser::substitute_expression(const std::st
// If variable is unset or empty, the expansion of word is
// substituted. Otherwise, the value of variable is
// substituted.
if (variable_value == nullptr || variable_value->empty()) {
subst_str = &expanded_word;
if (!variable_value_has_value || variable_value.empty()) {
subst_str = expanded_word;
subst_str_has_value = true;
} else {
subst_str = variable_value;
subst_str_has_value = true;
}
} else if (expansion_mode == '+') {
// ${variable:+word} (alternate value)
// If variable is unset or empty nothing is substituted.
// Otherwise, the expansion of word is substituted.
if (variable_value == nullptr || variable_value->empty()) {
const std::string empty{};
subst_str = &empty;
if (!variable_value_has_value || variable_value.empty()) {
subst_str = "";
subst_str_has_value = true;
} else {
subst_str = &expanded_word;
subst_str = expanded_word;
subst_str_has_value = true;
}
} else {
// Unknown expansion mode, continue after the ':'
Expand All @@ -176,7 +186,10 @@ std::pair<std::string, size_t> ConfigParser::substitute_expression(const std::st
pos_after_variable_expression = pos_after_word + 1;
} else if (res[pos_after_variable] == '}') {
// ${variable}
subst_str = variable_value;
if (variable_value_has_value) {
subst_str = variable_value;
subst_str_has_value = true;
}
// Move past the closing '}'
pos_after_variable_expression = pos_after_variable + 1;
} else {
Expand All @@ -186,20 +199,23 @@ std::pair<std::string, size_t> ConfigParser::substitute_expression(const std::st
}
} else {
// No braces, we have a $variable
subst_str = variable_value;
if (variable_value_has_value) {
subst_str = variable_value;
subst_str_has_value = true;
}
pos_after_variable_expression = pos_after_variable;
}

// If there is no substitution to make, move past the variable expression and continue.
if (subst_str == nullptr) {
if (!subst_str_has_value) {
total_scanned += pos_after_variable_expression - pos;
pos = pos_after_variable_expression;
continue;
}

res.replace(pos, pos_after_variable_expression - pos, *subst_str);
res.replace(pos, pos_after_variable_expression - pos, subst_str);
total_scanned += pos_after_variable_expression - pos;
pos += subst_str->length();
pos += subst_str.length();
} else {
total_scanned += 1;
pos += 1;
Expand Down
17 changes: 17 additions & 0 deletions tests/libdnf/conf/ConfigParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@ void ConfigParserTest::testConfigParserReleasever()
libdnf::ConfigParser::substitute(text, substitutions);
CPPUNIT_ASSERT(text == "major: , minor: ");
}
{
std::map<std::string, std::string> substitutions = {
{"var1", "value123"},
{"var2", "456"},
};
std::string text = "foo$var1-bar";
libdnf::ConfigParser::substitute(text, substitutions);
CPPUNIT_ASSERT(text == "foovalue123-bar");

text = "${var1:+alternate}-${unset:-default}-${nn:+n${nn:-${nnn:}";
libdnf::ConfigParser::substitute(text, substitutions);
CPPUNIT_ASSERT(text == "alternate-default-${nn:+n${nn:-${nnn:}");

text = "${unset:-${var1:+${var2:+$var2}}}";
libdnf::ConfigParser::substitute(text, substitutions);
CPPUNIT_ASSERT(text == "456");
}
}
Loading