Skip to content

Fix iterator UB in ARGS_NOEXCEPT completion parsing#172

Merged
Taywee merged 1 commit into
Taywee:masterfrom
metsw24-max:noexcept-completion-iterator-ub
May 24, 2026
Merged

Fix iterator UB in ARGS_NOEXCEPT completion parsing#172
Taywee merged 1 commit into
Taywee:masterfrom
metsw24-max:noexcept-completion-iterator-ub

Conversation

@metsw24-max
Copy link
Copy Markdown
Contributor

@metsw24-max metsw24-max commented May 23, 2026

Fix two iterator lifetime / memory-safety bugs in ARGS_NOEXCEPT completion handling that could trigger undefined behavior during bash-completion parsing.

Both issues were reachable through the public ParseCLI / ParseArgs APIs with attacker-controlled argv input.


Bugs Fixed

1. End iterator increment + invalid dereference

In ArgumentParser::ParseArgsValues, the completion path previously did:

it = end;
return "";

Control then returned to the caller loop:

for (; it != end; ++it)

which performed ++it on an already-end iterator, followed by an invalid iterator dereference on the next iteration.

The fix parks the iterator on the last consumed element instead:

it = valueIt;

so the caller’s increment lands legally on end.


2. Dangling iterator returned across stack frames

The completion replay path recursively parsed a temporary curArgs vector and returned an iterator into that local container:

return Parse(curArgs.begin(), curArgs.end());

After curArgs was destroyed, the caller compared the dangling iterator against the outer container’s end(), triggering undefined behavior.

The fix discards the recursive return value and returns the outer end iterator instead:

Parse(curArgs.begin(), curArgs.end());
return end;

Validation

Added a sanitizer-backed PoC that reproduces both issues against the pre-fix version and verifies clean execution on the fixed version.

Pre-fix

  • ASan crash
  • invalid iterator dereference
  • dangling iterator UB

Fixed

  • clean execution
  • exit code 0

The PoC includes:

  • standalone reproducer
  • ASan/UBSan build script
  • buggy vs fixed differential validation

@Taywee
Copy link
Copy Markdown
Owner

Taywee commented May 24, 2026

Looks valuable, especially for any future UB guarding, which would benefit from being able to reproduce them first. Thanks for the PR.

@Taywee Taywee merged commit 599b3e0 into Taywee:master May 24, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants