Void::Argparse::Parser::option_default(lng, sht, desc, std::string &out, std::string_view default_value) reads default_value after assigning to out. When the caller passes the same string for both (a natural call such as option_default("--build-dir", "-B", "Build directory", opts.build_arg, opts.build_arg)), the assignment frees the buffer the view points into and the following std::string{default_value} reads freed memory.
Reproduction
AddressSanitizer build (-fsanitize=address,undefined) of a CLI that calls option_default(..., x, x) where x is longer than the small-string buffer:
==515130==ERROR: AddressSanitizer: heap-use-after-free ...
#8 Void::Argparse::Parser::option_default(...) Argparse.cpp:62
#9 main ...
freed by thread T0 here:
#7 Void::Argparse::Parser::option_default(...) Argparse.cpp:53
Argparse.cpp:53 is out = std::string{default_value}; and the later read is std::string{default_value} in the OptionEntry initializer.
Suggested fix
Parser&Parser::option_default(...,std::string&out,std::string_viewdefault_value){conststd::stringdefault_copy{default_value};// safe even when it aliases out
out=default_copy;options_.push_back({...,default_copy,nullptr,&out,nullptr});return*this;}
Acceptance
Calling option_default with default_value aliasing out is ASan/UBSan clean and yields the expected default.
A unit test covers the aliasing case.
## Summary
`Void::Argparse::Parser::option_default(lng, sht, desc, std::string &out, std::string_view default_value)` reads `default_value` after assigning to `out`. When the caller passes the same string for both (a natural call such as `option_default("--build-dir", "-B", "Build directory", opts.build_arg, opts.build_arg)`), the assignment frees the buffer the view points into and the following `std::string{default_value}` reads freed memory.
## Reproduction
AddressSanitizer build (`-fsanitize=address,undefined`) of a CLI that calls `option_default(..., x, x)` where `x` is longer than the small-string buffer:
```
==515130==ERROR: AddressSanitizer: heap-use-after-free ...
#8 Void::Argparse::Parser::option_default(...) Argparse.cpp:62
#9 main ...
freed by thread T0 here:
#7 Void::Argparse::Parser::option_default(...) Argparse.cpp:53
```
`Argparse.cpp:53` is `out = std::string{default_value};` and the later read is `std::string{default_value}` in the `OptionEntry` initializer.
## Suggested fix
```cpp
Parser &Parser::option_default(..., std::string &out, std::string_view default_value) {
const std::string default_copy{default_value}; // safe even when it aliases out
out = default_copy;
options_.push_back({..., default_copy, nullptr, &out, nullptr});
return *this;
}
```
## Acceptance
- Calling `option_default` with `default_value` aliasing `out` is ASan/UBSan clean and yields the expected default.
- A unit test covers the aliasing case.
option_default and option_default_checked now copy default_value into a local string before assigning to out, so an aliased call (option_default(..., x, x)) no longer reads freed memory. The stored help default also uses the copy.
The reflective parse<Opts>() path calls both with aliased arguments (sink->text for out and default), so it was affected too.
Regression tests: argparse_option_default_aliasing_out_is_safe and argparse_option_default_checked_aliasing_out_is_safe (defaults longer than the SSO buffer).
Verification: with the source fix reverted, the new test fails under ASan with heap-use-after-free ... Argparse.cpp:119 in Parser::option_default; with the fix, Argparse 26/26 passes under both a Debug build and an ASan+UBSan build (no leaks, no UB).
Fixed in 9a3fc5e on `main`.
- `option_default` and `option_default_checked` now copy `default_value` into a local string before assigning to `out`, so an aliased call (`option_default(..., x, x)`) no longer reads freed memory. The stored help default also uses the copy.
- The reflective `parse<Opts>()` path calls both with aliased arguments (`sink->text` for out and default), so it was affected too.
- Regression tests: `argparse_option_default_aliasing_out_is_safe` and `argparse_option_default_checked_aliasing_out_is_safe` (defaults longer than the SSO buffer).
Verification: with the source fix reverted, the new test fails under ASan with `heap-use-after-free ... Argparse.cpp:119 in Parser::option_default`; with the fix, Argparse 26/26 passes under both a Debug build and an ASan+UBSan build (no leaks, no UB).
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Summary
Void::Argparse::Parser::option_default(lng, sht, desc, std::string &out, std::string_view default_value)readsdefault_valueafter assigning toout. When the caller passes the same string for both (a natural call such asoption_default("--build-dir", "-B", "Build directory", opts.build_arg, opts.build_arg)), the assignment frees the buffer the view points into and the followingstd::string{default_value}reads freed memory.Reproduction
AddressSanitizer build (
-fsanitize=address,undefined) of a CLI that callsoption_default(..., x, x)wherexis longer than the small-string buffer:Argparse.cpp:53isout = std::string{default_value};and the later read isstd::string{default_value}in theOptionEntryinitializer.Suggested fix
Acceptance
option_defaultwithdefault_valuealiasingoutis ASan/UBSan clean and yields the expected default.Fixed in
9a3fc5eonmain.option_defaultandoption_default_checkednow copydefault_valueinto a local string before assigning toout, so an aliased call (option_default(..., x, x)) no longer reads freed memory. The stored help default also uses the copy.parse<Opts>()path calls both with aliased arguments (sink->textfor out and default), so it was affected too.argparse_option_default_aliasing_out_is_safeandargparse_option_default_checked_aliasing_out_is_safe(defaults longer than the SSO buffer).Verification: with the source fix reverted, the new test fails under ASan with
heap-use-after-free ... Argparse.cpp:119 in Parser::option_default; with the fix, Argparse 26/26 passes under both a Debug build and an ASan+UBSan build (no leaks, no UB).