Fix iterator UB in ARGS_NOEXCEPT completion parsing#172
Merged
Taywee merged 1 commit intoMay 24, 2026
Conversation
Owner
|
Looks valuable, especially for any future UB guarding, which would benefit from being able to reproduce them first. Thanks for the PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix two iterator lifetime / memory-safety bugs in
ARGS_NOEXCEPTcompletion handling that could trigger undefined behavior during bash-completion parsing.Both issues were reachable through the public
ParseCLI/ParseArgsAPIs with attacker-controlled argv input.Bugs Fixed
1. End iterator increment + invalid dereference
In
ArgumentParser::ParseArgsValues, the completion path previously did:Control then returned to the caller loop:
for (; it != end; ++it)which performed
++iton 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:
so the caller’s increment lands legally on
end.2. Dangling iterator returned across stack frames
The completion replay path recursively parsed a temporary
curArgsvector and returned an iterator into that local container:return Parse(curArgs.begin(), curArgs.end());After
curArgswas destroyed, the caller compared the dangling iterator against the outer container’send(), triggering undefined behavior.The fix discards the recursive return value and returns the outer
enditerator instead: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
Fixed
The PoC includes: