jbonofre commented on code in PR #1247:
URL: https://github.com/apache/arrow-java/pull/1247#discussion_r4206317898
##########
vector/src/main/java/org/apache/arrow/vector/DecimalVector.java:
##########
@@ -207,6 +207,14 @@ public void setBigEndian(int index, byte[] value) {
BitVectorHelper.setBit(validityBuffer, index);
final int length = value.length;
+ // Reject an oversized value before any bytes are written. The
little-endian path below
+ // copies all `length` bytes into the fixed TYPE_WIDTH slot with unchecked
native writes,
+ // so validating after the copy would leave adjacent memory already
corrupted.
+ if (length > TYPE_WIDTH) {
Review Comment:
`BitVectorHelper.setBit(validityBuffer, index)` a few lines above still runs
before this check. If an oversized value is rejected here, the slot is already
marked valid. A caller that catches the exception then sees `isNull(index) ==
false` and reads stale or zero bytes instead of null.
Could you move this check above the `setBit` call, so a rejected call leaves
the vector unchanged?
##########
vector/src/main/java/org/apache/arrow/vector/Decimal256Vector.java:
##########
@@ -208,6 +208,14 @@ public void setBigEndian(int index, byte[] value) {
BitVectorHelper.setBit(validityBuffer, index);
final int length = value.length;
+ // Reject an oversized value before any bytes are written. The
little-endian path below
+ // copies all `length` bytes into the fixed TYPE_WIDTH slot with unchecked
native writes,
+ // so validating after the copy would leave adjacent memory already
corrupted.
+ if (length > TYPE_WIDTH) {
Review Comment:
Same issue here as in `DecimalVector.setBigEndian`. `setBit` runs before
this check, so a value longer than 32 bytes leaves the slot marked valid after
the exception.
Please validate the length first.
##########
vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java:
##########
@@ -279,6 +280,25 @@ public void decimalBE2NE() {
}
}
+ @Test
+ public void setBigEndianOversizedDoesNotCorruptNeighbor() {
+ try (DecimalVector decimalVector =
+ TestUtils.newVector(
+ DecimalVector.class, "decimal", new ArrowType.Decimal(38, 0, 128),
allocator)) {
+ decimalVector.allocateNew(2);
+
+ // A value whose low bytes are all non-zero, stored in the slot right
after the target.
+ final BigInteger neighbor = new BigInteger("305419896"); // 0x12345678
+ decimalVector.setBigEndian(1, neighbor.toByteArray());
+
+ // A value longer than the 16-byte decimal must be rejected before
anything is written.
+ assertThrows(
+ IllegalArgumentException.class, () -> decimalVector.setBigEndian(0,
new byte[24]));
+
+ assertEquals(neighbor, decimalVector.getObject(1).unscaledValue());
Review Comment:
This covers the original bug, since the neighboring slot stays intact. It
doesn't check the target slot after the exception.
Once the check is moved, please also assert
`assertTrue(decimalVector.isNull(0))` after the failed call. That would catch
the validity-bit ordering issue.
A matching test for `setBigEndianSafe` would help too. It should assert
`isNull` and that the value capacity is unchanged after the rejected call.
##########
vector/src/main/java/org/apache/arrow/vector/DecimalVector.java:
##########
@@ -306,6 +312,11 @@ public void setBigEndianSafe(int index, long start,
ArrowBuf buffer, int length)
handleSafe(index);
BitVectorHelper.setBit(validityBuffer, index);
+ if (length > TYPE_WIDTH) {
Review Comment:
`handleSoft(index)` and `setBit` both run before this check. A rejected
oversize call can still grow the vector and mark the slot valid.
Please validate `length` at the top of the method so a failed call has no
side effects.
##########
vector/src/main/java/org/apache/arrow/vector/Decimal256Vector.java:
##########
@@ -308,6 +314,11 @@ public void setBigEndianSafe(int index, long start,
ArrowBuf buffer, int length)
handleSafe(index);
BitVectorHelper.setBit(validityBuffer, index);
+ if (length > TYPE_WIDTH) {
Review Comment:
Same here as in `DecimalVector.setBigEndianSafe`.
Please validate `length` before `handleSafe` and `setBit`.
##########
vector/src/test/java/org/apache/arrow/vector/TestDecimal256Vector.java:
##########
@@ -283,6 +283,25 @@ public void decimalBE2NE() {
}
}
+ @Test
+ public void setBigEndianOversizedDoesNotCorruptNeighbor() {
+ try (Decimal256Vector decimalVector =
+ TestUtils.newVector(
+ Decimal256Vector.class, "decimal", new ArrowType.Decimal(60, 0,
256), allocator)) {
+ decimalVector.allocateNew(2);
+
+ // A value whose low bytes are all non-zero, stored in the slot right
after the target.
+ final BigInteger neighbor = new BigInteger("305419896"); // 0x12345678
+ decimalVector.setBigEndian(1, neighbor.toByteArray());
+
+ // A value longer than the 32-byte decimal must be rejected before
anything is written.
+ assertThrows(
+ IllegalArgumentException.class, () -> decimalVector.setBigEndian(0,
new byte[40]));
+
+ assertEquals(neighbor, decimalVector.getObject(1).unscaledValue());
Review Comment:
Same as the `TestDecimalVector` comment. Please assert `isNull(0)` after the
rejected call, and add a `setBigEndianSafe` case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]