Repository navigation
MINOR: Bounds check source buffer in BaseFixedWidthVector.copyFrom #1376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,6 @@ | |
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.memory.util.ArrowBufPointer; | ||
| import org.apache.arrow.memory.util.ByteFunctionHelpers; | ||
| import org.apache.arrow.memory.util.MemoryUtil; | ||
| import org.apache.arrow.memory.util.hash.ArrowBufHasher; | ||
| import org.apache.arrow.util.Preconditions; | ||
| import org.apache.arrow.vector.compare.VectorVisitor; | ||
|
|
@@ -833,10 +832,12 @@ public void copyFrom(int fromIndex, int thisIndex, ValueVector from) { | |
| BitVectorHelper.unsetBit(this.getValidityBuffer(), thisIndex); | ||
| } else { | ||
| BitVectorHelper.setBit(this.getValidityBuffer(), thisIndex); | ||
| MemoryUtil.copyMemory( | ||
| from.getDataBuffer().memoryAddress() + (long) fromIndex * typeWidth, | ||
| this.getDataBuffer().memoryAddress() + (long) thisIndex * typeWidth, | ||
| typeWidth); | ||
| from.getDataBuffer() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, also, Do you have a before/after number, for instance the existing JMH benchmarks for Also, this only protects when bounds checking is on. With |
||
| .getBytes( | ||
| (long) fromIndex * typeWidth, | ||
| this.getDataBuffer(), | ||
| (long) thisIndex * typeWidth, | ||
| typeWidth); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,15 +20,19 @@ | |
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Duration; | ||
| import java.time.Period; | ||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
| import org.apache.arrow.memory.ArrowBuf; | ||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.ipc.message.ArrowFieldNode; | ||
| import org.apache.arrow.vector.types.Types.MinorType; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
|
|
@@ -1098,4 +1102,25 @@ public void testCopySafeArrow7837() { | |
| assertEquals("1234567890", Objects.requireNonNull(vc2.getObject(5)).toString()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyFromFixedWidthSourceBufferTooShort() { | ||
| try (IntVector source = new IntVector("source", allocator); | ||
| IntVector target = new IntVector("target", allocator); | ||
| ArrowBuf body = allocator.buffer(64)) { | ||
| body.setZero(0, body.capacity()); | ||
| body.setInt(0, 1); | ||
| body.setInt(12, 0xBAD); | ||
| // Field node claims 4 values, but the data buffer only holds one, as a malformed IPC | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The source is loaded with a zero-length validity buffer ( Is that guaranteed, or does it work only because of how Giving the source a real validity buffer (all bits set) would make the test say what it means. |
||
| // record batch can. | ||
| ArrowBuf validity = body.slice(0, 0); | ||
| ArrowBuf data = body.slice(0, IntVector.TYPE_WIDTH); | ||
| source.loadFieldBuffers(new ArrowFieldNode(4, 0), Arrays.asList(validity, data)); | ||
|
|
||
| target.allocateNew(4); | ||
| target.copyFromSafe(0, 0, source); | ||
| assertEquals(1, target.get(0)); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> target.copyFromSafe(3, 1, source)); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hardens only
BaseFixedWidthVector.copyFrom. The same unchecked pattern,MemoryUtil.copyMemoryon rawmemoryAddress()plus an offset, remains in other places invector/src/main. These includeDecimalVector,Decimal256Vector, BitVectorHelper, andVectorAppender`.If the goal is to make malformed IPC input fail with
IndexOutOfBoundsExceptioninstead of an out-of-bounds read, can we say which of these are in scope?If they are reachable from
loadFieldBuffersstyle input, I would fix them here.