Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -21,12 +21,14 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.Map;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoSerializable;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import org.apache.spark.SparkIllegalArgumentException;
import org.apache.spark.SparkUnsupportedOperationException;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.catalyst.types.*;
Expand Down Expand Up @@ -155,6 +157,17 @@ 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 SparkIllegalArgumentException(
"INTERNAL_ERROR",
Map.of("message", "Invalid byte array backed UnsafeRow: byte array length=" +
bytes.length + ", offset=" + offsetInByteArray + ", byte size=" + sizeInBytes)
);
}
}
this.baseObject = baseObject;
this.baseOffset = baseOffset;
this.sizeInBytes = sizeInBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.sql

import java.io.ByteArrayOutputStream

import org.apache.spark.{SparkConf, SparkFunSuite}
import org.apache.spark.{SparkConf, SparkFunSuite, SparkIllegalArgumentException}
import org.apache.spark.serializer.{JavaSerializer, KryoSerializer}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{UnsafeProjection, UnsafeRow}
Expand Down Expand Up @@ -188,4 +188,40 @@ class UnsafeRowSuite extends SparkFunSuite {
unsafeRow.setDecimal(0, d2, 38)
assert(unsafeRow.getDecimal(0, 38, 18) === null)
}

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

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

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

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