Guard joined-value completion against prefix/separator overlap#178
Merged
Taywee merged 1 commit intoJun 2, 2026
Merged
Conversation
Owner
|
Makes sense to me. 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.
Harden the bash-completion path against invalid prefix/separator combinations that can cause
std::string::substr()to throwstd::out_of_range.When a custom
LongSeparator()overlaps the configured long-option prefix (for example,LongSeparator("-")with the default"--"prefix),find(longseparator)may return a position inside the prefix itself. The completion handler then constructs anargstring shorter thanlongprefixand subsequently calls:which throws
std::out_of_range.Root Cause
The joined-value completion path assumed that any discovered separator would occur at or beyond the end of the long-option prefix. That invariant was not enforced.
For overlapping prefix/separator configurations,
argmay become shorter thanlongprefix, making:invalid.
In throwing builds, the resulting
std::out_of_rangeescapes as a non-argsexception. UnderARGS_NOEXCEPT, the throwingsubstr()call breaks the no-throw expectation and may result in termination when exceptions are disabled.Fix
Only attempt joined-value completion when the separator is located at or beyond the end of the long-option prefix:
if (separator != chunk.npos && separator >= longprefix.size())This prevents invalid
substr()calls while preserving existing behavior for valid completion inputs.Tests
Added two regression tests:
test/completion_separator_prefix_overlap.cxxargs::Completionpath instead of leakingstd::out_of_range.test/noexcept_completion_separator_prefix_overlap.cxxARGS_NOEXCEPT, ensuring completion is reported through the parser error state without throwing.Both tests fail on the unpatched code and pass with this change.
Files Changed
args.hxxtest/completion_separator_prefix_overlap.cxxtest/noexcept_completion_separator_prefix_overlap.cxx