Skip to content

Commit f90274e

Browse files
committed
[bug-69628] make IOUtils more tolerant of len < 0
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1924601 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8278417 commit f90274e

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

poi/src/main/java/org/apache/poi/util/IOUtils.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,12 @@ public static byte[] toByteArrayWithMaxLength(InputStream stream, final int maxL
248248

249249
private static byte[] toByteArray(InputStream stream, final int length, final int maxLength,
250250
final boolean checkEOFException, final boolean isLengthKnown) throws IOException {
251-
if (length < 0 || maxLength < 0) {
252-
throw new RecordFormatException("Can't allocate an array of length < 0");
253-
}
254251
final int derivedMaxLength = Math.max(maxLength, BYTE_ARRAY_MAX_OVERRIDE);
255252
if ((length != Integer.MAX_VALUE) || (derivedMaxLength != Integer.MAX_VALUE)) {
256253
checkLength(length, derivedMaxLength);
257254
}
258255

259-
final int derivedLen = isLengthKnown ? Math.min(length, derivedMaxLength) : derivedMaxLength;
256+
final int derivedLen = isLengthKnown && length >= 0 ? Math.min(length, derivedMaxLength) : derivedMaxLength;
260257
final int byteArrayInitLen = calculateByteArrayInitLength(isLengthKnown, length, derivedMaxLength);
261258
final int internalBufferLen = DEFAULT_BUFFER_SIZE;
262259
try (UnsynchronizedByteArrayOutputStream baos = UnsynchronizedByteArrayOutputStream.builder().setBufferSize(byteArrayInitLen).get()) {
@@ -275,7 +272,7 @@ private static byte[] toByteArray(InputStream stream, final int length, final in
275272
throwRecordTruncationException(derivedMaxLength);
276273
}
277274

278-
if (checkEOFException && derivedLen != Integer.MAX_VALUE && totalBytes < derivedLen) {
275+
if (checkEOFException && length >= 0 && derivedLen != Integer.MAX_VALUE && totalBytes < derivedLen) {
279276
throw new EOFException("unexpected EOF - expected len: " + derivedLen + " - actual len: " + totalBytes);
280277
}
281278

poi/src/test/java/org/apache/poi/util/TestIOUtils.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,25 @@ void testToByteArrayMaxLengthTooSmall() {
110110
}
111111

112112
@Test
113-
void testToByteArrayNegativeLength() {
114-
assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(data123(), -1));
113+
void testToByteArrayNegativeLength() throws IOException {
114+
final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
115+
IOUtils.setByteArrayMaxOverride(30 * 1024 * 1024);
116+
try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
117+
assertArrayEquals(array, IOUtils.toByteArray(is, -1, 100));
118+
} finally {
119+
IOUtils.setByteArrayMaxOverride(-1);
120+
}
121+
}
122+
123+
@Test
124+
void testToByteArrayNegativeLength2() throws IOException {
125+
final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
126+
IOUtils.setByteArrayMaxOverride(30 * 1024 * 1024);
127+
try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
128+
assertArrayEquals(array, IOUtils.toByteArray(is, -1));
129+
} finally {
130+
IOUtils.setByteArrayMaxOverride(-1);
131+
}
115132
}
116133

117134
@Test

0 commit comments

Comments
 (0)