Skip to content

Conversation

@binary-signal
Copy link

@binary-signal binary-signal commented Dec 12, 2025

Linked issue: close #2157

Purpose

The to*Array() methods in BinaryArray were passing *_ARRAY_OFFSET constants (e.g., DOUBLE_ARRAY_OFFSET) as the targetOffset parameter to BinarySegmentUtils.copyToUnsafe(). These constants are Unsafe memory offsets (typically 16 bytes on 64-bit JVMs), but copyToUnsafe() uses targetOffset as a regular Java array index.

For example, with a 20-element double array:

  • targetOffset = DOUBLE_ARRAY_OFFSET ≈ 16
  • The code tried to access target[16 + 0] through target[16 + 19 = 35]
  • This caused ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20

Brief change log

Changed all to*Array() methods to pass 0 as the targetOffset since copyToUnsafe() uses regular Java array indexing, not Unsafe memory operations.

Tests

  • Added 7 new tests (testTo*ArrayWithLargeArray) that verify all primitive array conversion methods work correctly with 20-element arrays

Here are all the test methods added to BinaryArrayTest.java:

testToDoubleArrayWithLargeArray()
testToFloatArrayWithLargeArray()
testToLongArrayWithLargeArray()
testToIntArrayWithLargeArray()
testToShortArrayWithLargeArray()
testToByteArrayWithLargeArray()
testToBooleanArrayWithLargeArray()
testToDoubleArrayWithLargeArray

@binary-signal
Copy link
Author

@wuchong could you please have a look when you have time

fmt
Signed-off-by: binary-signal <binary-signal@github.noreply.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ArrayIndexOutOfBoundsException when calling toDoubleArray() with arrays larger than a few elements

1 participant