Fix integer overflow in TextHelpAppendable.indexOfWrap - #437
Merged
Conversation
startPos + width overflowed to a negative value when width is Integer.MAX_VALUE (the default TextStyle.UNSET_MAX_WIDTH), so indexOfWrap returned a negative wrap position and makeColumnQueue threw StringIndexOutOfBoundsException. Compute the wrap boundary as a long.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an integer overflow in TextHelpAppendable.indexOfWrap when a TextStyle uses the unbounded sentinel width (UNSET_MAX_WIDTH / Integer.MAX_VALUE), preventing negative wrap indexes that can crash wrapping logic (e.g., via makeColumnQueue).
Changes:
- Compute the wrap boundary (
startPos + width) usinglongarithmetic to avoidintoverflow. - Update boundary comparisons and limits to use the
longboundary safely. - Add unit tests covering the overflow scenario with
Integer.MAX_VALUEwidth.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/main/java/org/apache/commons/cli/help/TextHelpAppendable.java | Avoids int overflow in wrap-boundary calculation by using long, preventing negative wrap indices. |
| src/test/java/org/apache/commons/cli/help/TextHelpAppendableTest.java | Adds regression assertions for the unbounded-width overflow scenario. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Member
|
Thank you @rootvector2 , merged 🚀 |
garydgregory
added a commit
that referenced
this pull request
Aug 8, 2026
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.
indexOfWrapcomputes the wrap boundary asstartPos + widthinintmath.widthisInteger.MAX_VALUEfor anyTextStyleleft at the defaultUNSET_MAX_WIDTH, so oncestartPos > 0(any text containing a break character) the sum overflows negative,indexOfWrapreturns a negative index, andmakeColumnQueuecallssubSequence(startPos, <negative>), throwingStringIndexOutOfBoundsException. Found while reviewing thehelptext wrapping.Repro:
Computing the boundary as a
longkeeps every in-range result identical. The addedindexOfWrapassertion returns the negative index before the change and the string length after.mvn; that'smvnon the command line by itself.