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:
faumaray committed 2026-12-05 08:40:05 +03:00
1 parent 9cb86f8f97
commit 9a3fc5e217
2 files changed
+35 -4

No files matched your search

@@ -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;