fix int overflow in Conversion count and position bounds guards - #1779
Open
alhudz wants to merge 1 commit into
Open
fix int overflow in Conversion count and position bounds guards#1779alhudz wants to merge 1 commit into
alhudz wants to merge 1 commit into
Conversation
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.
Repro: call any
Conversionarray/hex/binary converter with a large count or position, e.g.Conversion.binaryToInt(new boolean[]{true}, 0, 0, Integer.MAX_VALUE, 2),Conversion.byteArrayToInt(new byte[]{1}, 0, 0, 0, Integer.MAX_VALUE), orConversion.intToHex(0, 0, "", 0, Integer.MAX_VALUE).Cause: each method documents
@throws IllegalArgumentExceptionwhen the requested count and position exceed the destination width (nBools - 1 + dstPos >= 32,(nBytes - 1) * 8 + dstPos >= 32, and so on) and evaluates that guard first, but inint. For a large count (nBools/nBytes/nHex/nInts/nShorts) or a largesrcPos/dstPosthe expression overflows and wraps negative, so the guard is skipped.Fix: widen the leading operand of each guard to
long((long) nBools - 1 + dstPos,((long) nBytes - 1) * 8 + dstPos, ...) so the documented condition holds. Small valid inputs are unchanged.Without the fix the reading methods throw an undocumented
ArrayIndexOutOfBoundsException/StringIndexOutOfBoundsExceptionfrom the loop, and the hex writers (intToHex/longToHex/shortToHex/byteToHex) skip the guard entirely and build a several-hundred-million-charString. Applied to all 28 guard sites in the class;ConversionTest.testLargeCountThrowsIllegalArgumentExceptionexercises each and fails before the change.mvn; that'smvnon the command line by itself.