Buffers.clear(...) only zeros the buffer's own window of the backing array - #869
Draft
kdelay wants to merge 1 commit into
Draft
Buffers.clear(...) only zeros the buffer's own window of the backing array#869kdelay wants to merge 1 commit into
kdelay wants to merge 1 commit into
Conversation
…array The array-backed branch of every Buffers.clear(XBuffer) overload calls Arrays.fill(buffer.array(), 0), which zeros the whole backing array and ignores arrayOffset() and capacity(). A buffer produced by slice() shares its parent's array at a non-zero offset, so clearing the slice also wipes data outside it. Measured on JDK 17 with a 10-byte heap array filled with 7 and a [4, 7) slice: array-backed slice -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] direct slice -> [7, 7, 7, 7, 0, 0, 0, 7, 7, 7] The direct branch of the same method is correct because it writes through the buffer, so the two branches of one method disagree. clearBuffer() already sets position to zero and limit to the capacity, so the region to zero is [arrayOffset(), arrayOffset() + capacity()). Use the ranged Arrays.fill() overload for all seven element types. Adds four regression tests (byte, char, int, short) that fail without the change.
garydgregory
marked this pull request as draft
August 2, 2026 15:27
Member
|
I converted this PR to draft.
This is not acceptable. Do your own homework before dumping it on someone else. |
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
Buffers.clear(XBuffer)promises to clear this buffer. For array-backed buffers it does more than that: every overload callsarray()returns the whole backing array, soarrayOffset()andcapacity()are ignored. A buffer returned byslice()shares its parent's array at a non-zero offset, so clearing a slice also zeros bytes that belong to the parent and are outside the slice.The direct branch of the same method is correct, because it writes zeros through the buffer and therefore respects the window. So the two branches of one method disagree.
Reproduction
JDK 17,
masterat eb60bd5, 10-element heap array filled with7, slice over[4, 7):[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]allocateDirect[7, 7, 7, 7, 0, 0, 0, 7, 7, 7]The direct result is what the Javadoc describes.
Change
clearBuffer()has already set the position to zero and the limit to the capacity, so the region that belongs to this buffer is[arrayOffset(), arrayOffset() + capacity()). This switches the seven array-backed branches to the rangedArrays.fill()overload. Buffers witharrayOffset() == 0are unaffected, which covers everythingallocate()andwrap()produce, and the direct branches are untouched.Buffersis new in 2.23.0 and is not released yet, and the only in-tree callers go throughclearDirect(...), so this does not change behavior anyone can be depending on today.Tests
Four regression tests in
BuffersTest(byte, char, int, short, covering each cast variant of the fill). Verified they are load-bearing:Tests run: 61, Failures: 0, Errors: 0, Skipped: 0Buffers.javareverted:Tests run: 61, Failures: 4Full default goal (
mvn, so rat, checkstyle, japicmp, spotbugs, pmd and javadoc included):BUILD SUCCESS,Tests run: 6414, Failures: 0, Errors: 0, Skipped: 36.@since 2.23.0Javadoc contracts against the implementations, and the probe, the patch, the tests and this description were written by the tool.mvn; that'smvnon the command line by itself.