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

+9 -4
View File
@@ -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;