PARQUET-3261: Fix integer overflow in CapacityByteArrayOutputStream#3525
Open
yadavay-amzn wants to merge 1 commit intoapache:masterfrom
Open
PARQUET-3261: Fix integer overflow in CapacityByteArrayOutputStream#3525yadavay-amzn wants to merge 1 commit intoapache:masterfrom
yadavay-amzn wants to merge 1 commit intoapache:masterfrom
Conversation
The overflow check in addSlab() used bytesUsed which is not updated until after addSlab() returns in write(). This caused the overflow guard to miss cases where bytesAllocated + nextSlabSize exceeds Integer.MAX_VALUE. Fix: - Use bytesAllocated instead of bytesUsed for the overflow check, since bytesAllocated is always up to date when addSlab() is called. - Cap nextSlabSize when it would cause bytesAllocated to overflow, instead of letting Math.addExact throw an uncaught ArithmeticException.
2dab1ed to
29bbc79
Compare
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.
What changes were made?
Fix integer overflow in
CapacityByteArrayOutputStream.addSlab()that causesArithmeticExceptionwhen writing largeARRAY<STRING>columns (issue #3261).Root cause (identified by @Kimahriman)
The overflow check in
addSlab()usedbytesUsedto detect overflow, butbytesUsedis not updated until afteraddSlab()returns inwrite(). Additionally,nextSlabSizecan be larger thanminimumSize(due to the doubling strategy), so checking onlybytesUsed + minimumSizewas insufficient.This meant
bytesAllocated = Math.addExact(this.bytesAllocated, nextSlabSize)could overflow without being caught by the guard, throwing an uncaughtArithmeticExceptioninstead of the intendedOutOfMemoryError.Fix
bytesAllocatedinstead ofbytesUsedfor the overflow check —bytesAllocatedis always up to date whenaddSlab()is called.nextSlabSizewhen it would causebytesAllocatedto overflowInteger.MAX_VALUE, preventing the uncaughtArithmeticExceptionon theMath.addExactcall.Tests
Added
TestCapacityByteArrayOutputStreamOverflowwith two tests:Integer.MAX_VALUEsucceeds (previously threwArithmeticException)OutOfMemoryErroras intended