Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ public UnsafeRow() {}
public void pointTo(Object baseObject, long baseOffset, int sizeInBytes) {
assert numFields >= 0 : "numFields (" + numFields + ") should >= 0";
assert sizeInBytes % 8 == 0 : "sizeInBytes (" + sizeInBytes + ") should be a multiple of 8";
if (baseObject instanceof byte[] bytes) {
int offsetInByteArray = (int) (baseOffset - Platform.BYTE_ARRAY_OFFSET);
if (offsetInByteArray < 0 || sizeInBytes < 0 ||
bytes.length < offsetInByteArray + sizeInBytes) {
throw new ArrayIndexOutOfBoundsException(
"byte array length: " + bytes.length +
", offset: " + offsetInByteArray + ", size: " + sizeInBytes
);
}
}
this.baseObject = baseObject;
this.baseOffset = baseOffset;
this.sizeInBytes = sizeInBytes;
Expand Down
27 changes: 27 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/UnsafeRowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,31 @@ class UnsafeRowSuite extends SparkFunSuite {
unsafeRow.setDecimal(0, d2, 38)
assert(unsafeRow.getDecimal(0, 38, 18) === null)
}

test("SPARK-48713: throw ArrayIndexOutOfBoundsException for illegal UnsafeRow.pointTo") {
val emptyRow = UnsafeRow.createFromByteArray(64, 2)
val byteArray = new Array[Byte](64)

// Out of bounds
var errorMsg = intercept[ArrayIndexOutOfBoundsException] {
emptyRow.pointTo(byteArray, Platform.BYTE_ARRAY_OFFSET + 50, 32)
}.getMessage
assert(errorMsg.contains("byte array length: 64, offset: 50, size: 32"))

// Negative size
errorMsg = intercept[ArrayIndexOutOfBoundsException] {
emptyRow.pointTo(byteArray, Platform.BYTE_ARRAY_OFFSET + 50, -32)
}.getMessage
assert(errorMsg.contains("byte array length: 64, offset: 50, size: -32"))

// Negative offset
errorMsg = intercept[ArrayIndexOutOfBoundsException] {
emptyRow.pointTo(byteArray, -5, 32)
}.getMessage
assert(
errorMsg.contains(
s"byte array length: 64, offset: ${-5 - Platform.BYTE_ARRAY_OFFSET}, size: 32"
)
)
}
}