← 输入框|github.com|刷新
Skip to content

MINOR: Check source range in ArrowBuf.setBytes for direct ByteBuffers - #1380

Merged
jbonofre merged 1 commit into
apache:mainfrom
Arawoof06:arrowbuf-setbytes-direct-src-check
Nov 6, 2026
Merged

jbonofre merged 1 commit into
apache:mainfrom
Arawoof06:arrowbuf-setbytes-direct-src-check

Conversation

@Arawoof06

Copy link
Copy Markdown
Contributor

What's Changed

ArrowBuf.setBytes(long index, ByteBuffer src, int srcIndex, int length) bounds-checks only the destination. When src is a direct buffer it copies length bytes from getByteBufferAddress(src) + srcIndex with MemoryUtil.copyMemory, so a source range past src.capacity() (or a negative srcIndex) silently copies adjacent native memory into the ArrowBuf, even with bounds checking enabled. For example, buf.setBytes(0, ByteBuffer.allocateDirect(8), 0, 64) succeeds. The heap path already rejects this through ByteBuffer.position/limit, and the byte[] and ArrowBuf overloads (and NettyArrowBuf.setBytes(ByteBuf)) check the source with isOutOfBounds. This adds the same source check before either path, so the set/setSafe(int, ByteBuffer, int, int) methods on the variable-width vectors fail with IndexOutOfBoundsException instead of reading out of bounds. Valid ranges behave as before. Added a regression test to TestArrowBuf.

@jbonofre jbonofre added the enhancement PRs that add or improve features. label Oct 21, 2026
@apache apache deleted a comment from github-actions Bot Oct 21, 2026
@jbonofre jbonofre added bug-fix PRs that fix a big. and removed enhancement PRs that add or improve features. labels Oct 21, 2026

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the fix @Arawoof06!

The direct-buffer path of setBytes(long, ByteBuffer, int, int) only checked the destination, so an out-of-range srcIndex/length silently copied adjacent native memory.
Adding the source check up front brings it in line with the byte[] and ArrowBuf overloads, and valid ranges behave as before.
The regression test covers oversize length, oversize range and negative srcIndex.

A couple of non-blocking suggestions:

  • Add a test for the heap ByteBuffer path and a valid boundary copy (e.g. srcIndex=4, length=4 on a capacity-8 buffer), so an over-strict check would be caught.
  • Consider naming the source in the exception message (e.g. "source ByteBuffer index:..., length:... (expected: range(0, capacity))"). As written it looks identical to a destination-bounds failure from checkIndex.

@github-actions github-actions Bot added this to the 20.0.0 milestone Oct 21, 2026
@jbonofre
jbonofre force-pushed the arrowbuf-setbytes-direct-src-check branch from 4d14315 to 6553487 Compare November 5, 2026 19:57
@jbonofre
jbonofre merged commit 22f9750 into apache:main Nov 6, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix PRs that fix a big.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants