Configuration: fix option_default use-after-free when default aliases out
option_default and option_default_checked assigned to out before reading default_value; when the caller passed the same string for both, the view dangled. Copy the default first. The reflective parse() path calls both with aliased arguments. Closes #62
This commit is contained in:
1 parent
9cb86f8f97
commit
9a3fc5e217
2 files changed
+35
-4
No files matched your search
@@ -115,11 +115,14 @@ Parser& Parser::option_default(std::string_view lng,
|
||||
std::string& out,
|
||||
std::string_view default_value)
|
||||
{
|
||||
out = std::string{default_value};
|
||||
// Copy first: default_value may alias out (e.g. option_default(..., x, x)),
|
||||
// and assigning to out would free the buffer the view points into.
|
||||
const std::string default_copy{default_value};
|
||||
out = default_copy;
|
||||
options_.push_back(OptionEntry{.lng = std::string{lng},
|
||||
.sht = std::string{sht},
|
||||
.desc = std::string{desc},
|
||||
.def = std::string{default_value},
|
||||
.def = default_copy,
|
||||
.out_str = &out});
|
||||
return *this;
|
||||
}
|
||||
@@ -145,11 +148,13 @@ Parser& Parser::option_default_checked(std::string_view lng,
|
||||
std::string_view default_value,
|
||||
std::vector<std::string> allowed)
|
||||
{
|
||||
out = std::string{default_value};
|
||||
// See option_default: default_value may alias out.
|
||||
const std::string default_copy{default_value};
|
||||
out = default_copy;
|
||||
options_.push_back(OptionEntry{.lng = std::string{lng},
|
||||
.sht = std::string{sht},
|
||||
.desc = std::string{desc},
|
||||
.def = std::string{default_value},
|
||||
.def = default_copy,
|
||||
.out_str = &out,
|
||||
.allowed = std::move(allowed)});
|
||||
return *this;
|
||||
|
||||
@@ -120,6 +120,32 @@ Void::Testing::Test argparse_option_default_overridden()
|
||||
Void::Testing::expect_eq(out, std::string{"xml"});
|
||||
}
|
||||
|
||||
Void::Testing::Test argparse_option_default_aliasing_out_is_safe()
|
||||
{
|
||||
// default_value aliases out: the old code assigned to out before reading
|
||||
// the view, a use-after-free when the string exceeded the SSO buffer.
|
||||
std::string out = "a default longer than the small string buffer";
|
||||
Parser p{"test"};
|
||||
p.option_default("--build-dir", "-B", "build directory", out, out);
|
||||
const Args a{{"test"}};
|
||||
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
|
||||
Void::Testing::expect_eq(out,
|
||||
std::string{"a default longer than the small string buffer"});
|
||||
Void::Testing::expect(p.help().find(
|
||||
"a default longer than the small string buffer") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test argparse_option_default_checked_aliasing_out_is_safe()
|
||||
{
|
||||
std::string out = "default-mode-value-longer-than-ssbo";
|
||||
Parser p{"test"};
|
||||
p.option_default_checked("--mode", "-m", "mode", out, out,
|
||||
{"default-mode-value-longer-than-ssbo", "other"});
|
||||
const Args a{{"test", "--mode=other"}};
|
||||
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
|
||||
Void::Testing::expect_eq(out, std::string{"other"});
|
||||
}
|
||||
|
||||
Void::Testing::Test argparse_option_missing_value_is_error()
|
||||
{
|
||||
std::optional<std::string> out;
|
||||
|
||||
Reference in new issue
Block a user