Skip to content

Commit 97b9646

Browse files
Chong Gaoclaude
andcommitted
Validate output buffer capacity in RapidsInputFile.readVectored default
Throw IllegalArgumentException when any CopyRange's outputOffset + length exceeds output.getLength(), before opening the stream. This lets callers rely on the default implementation instead of duplicating the check, and gives a clearer error message than the lower-level failure surfaced by HostMemoryBuffer.copyFromStream. The CopyRange constructor already rejects negative inputOffset/outputOffset and non-positive length, so those guards are not duplicated here. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> Signed-off-by: Chong Gao <res_life@163.com>
1 parent 315db02 commit 97b9646

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/main/java/com/nvidia/spark/rapids/jni/fileio/RapidsInputFile.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ default void readVectored(HostMemoryBuffer output, List<CopyRange> copyRanges)
7272
if (copyRanges.isEmpty()) {
7373
return;
7474
}
75+
long outputLength = output.getLength();
7576
for (CopyRange copyRange : copyRanges) {
7677
Objects.requireNonNull(copyRange, "copyRange can't be null");
78+
long end = copyRange.getOutputOffset() + copyRange.getLength();
79+
if (end > outputLength) {
80+
throw new IllegalArgumentException(
81+
"Output buffer length " + outputLength + " is smaller than requested end " + end);
82+
}
7783
}
7884

7985
try (SeekableInputStream input = open()) {

src/test/java/com/nvidia/spark/rapids/jni/fileio/RapidsInputFileTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ public void readVectoredRejectsNullRangesBeforeOpeningStream() throws IOExceptio
6666
assertEquals(0, inputFile.getOpenCount());
6767
}
6868

69+
@Test
70+
public void readVectoredRejectsRangeExceedingOutputBeforeOpeningStream() throws IOException {
71+
TestRapidsInputFile inputFile = new TestRapidsInputFile(FILE_DATA);
72+
try (HostMemoryBuffer output = HostMemoryBuffer.allocate(3)) {
73+
assertThrows(IllegalArgumentException.class,
74+
() -> inputFile.readVectored(output,
75+
Collections.singletonList(new RapidsInputFile.CopyRange(0, 4, 0))));
76+
assertThrows(IllegalArgumentException.class,
77+
() -> inputFile.readVectored(output,
78+
Collections.singletonList(new RapidsInputFile.CopyRange(0, 2, 2))));
79+
}
80+
assertEquals(0, inputFile.getOpenCount());
81+
}
82+
6983
@Test
7084
public void readTailUsesSeekableStreamFallback() throws IOException {
7185
RapidsInputFile inputFile = new TestRapidsInputFile(FILE_DATA);

0 commit comments

Comments
 (0)