[FLINK-39754][core] Fix int overflow in DataOutputSerializer.resize#28252
Open
sauliusvl wants to merge 1 commit into
Open
[FLINK-39754][core] Fix int overflow in DataOutputSerializer.resize#28252sauliusvl wants to merge 1 commit into
sauliusvl wants to merge 1 commit into
Conversation
buffer.length * 2 uses int arithmetic and overflows to a negative value once the buffer crosses Integer.MAX_VALUE / 2 (~1.07 GB). Math.max then picks buffer.length + minCapacityAdd, so every subsequent resize grows the buffer by a handful of bytes instead of doubling, doing a full System.arraycopy of the ~1+ GB buffer each call. On large heaps this manifests as a silent O(n^2) hang until buffer.length + minCapacityAdd itself overflows and the existing NegativeArraySizeException catch translates it to an IOException. Extract the size computation into a package-private static helper that uses long arithmetic, caps at Integer.MAX_VALUE - 8 (matching java.util.ArrayList), and jumps to the cap once doubling would overflow so serializations that just barely fit under 2 GB still complete.
Collaborator
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 is the purpose of the change
Fixes FLINK-39754.
DataOutputSerializer.resize()usesintarithmetic forbuffer.length * 2. Oncebuffer.lengthcrossesInteger.MAX_VALUE / 2(~1.07 GB), doubling overflows to a negativeint,Math.maxthen picksbuffer.length + minCapacityAdd, and every subsequent resize grows the buffer by a handful of bytes instead of doubling — doing a fullSystem.arraycopyof the ~1+ GB buffer each call. On large heaps this manifests as a silent O(n²) hang untilbuffer.length + minCapacityAdditself overflows and the existingcatch (NegativeArraySizeException)translates it to anIOException.Brief change log
resize(int)into a@VisibleForTestingpackage-private static helpercomputeNewBufferLength(int, int).longarithmetic, validates against a newMAX_ARRAY_SIZE = Integer.MAX_VALUE - 8cap (matchingjava.util.ArrayList), and jumps to the cap when doubling would overflow — so serializations that just barely fit under 2 GB still complete instead of grinding through a linear-step resize loop.catch (NegativeArraySizeException)block fromresize. The existingOutOfMemoryErrorretry path is preserved (it addresses an independent concern — doubled size exceeding available heap).Verifying this change
This change added tests and can be verified as follows:
computeNewBufferLengthinDataInputOutputSerializerTestcovering: normal doubling,minCapacityAdd-dominated growth, jump-to-cap whencurrentLength * 2would overflow, exact-cap boundary, andIOExceptionwhen the required size exceeds the cap. No multi-GB allocations required.DataInputOutputSerializerTesttests continue to pass, confirming the normal write/read paths throughresize()are unchanged.Does this pull request potentially affect one of the following parts:
@Public(Evolving): no (DataOutputSerializeris unannotated / internal)IOExceptioninstead of an opaqueNegativeArraySizeException-derived message.Documentation
Was generative AI tooling used to co-author this PR?
Generated-by: Claude (Anthropic, Opus 4.7) via Zed editor