Argparse: option_default is a use-after-free when default_value aliases out #62

Closed
opened 2026-12-05 04:48:03 +00:00 by faumaray · 1 comment
Owner

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

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.
## 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.
Author
Owner

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).

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).
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Void/Library#62