Skip to content
Open
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 @@ -290,7 +290,7 @@ private static String readAsciiLen(LittleEndianByteArrayInputStream leis) throws

private static String readUtf16(LittleEndianByteArrayInputStream leis) throws IOException {
int size = leis.readInt();
byte[] buf = IOUtils.toByteArray(leis, size * 2, MAX_STRING_LENGTH);
byte[] buf = IOUtils.toByteArray(leis, size * 2L, MAX_STRING_LENGTH);
return StringUtil.getFromUnicodeLE(buf, 0, size);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.RecordFormatException;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -107,4 +108,23 @@ void testOleNativeOOM() throws IOException {
}
}

@Test
void testOle10NativeUtf16SizeOverflow() {
// command2 declares 0x40000001 UTF-16 chars; the byte count (size * 2) overflows
// a signed int to a negative value that slips past the MAX_STRING_LENGTH cap.
byte[] data = new byte[34];
LittleEndian.putShort(data, 4, (short) 2); // flags1 -> parsed encoding
data[6] = 'A'; // label (AsciiZ)
data[8] = 'B'; // fileName (AsciiZ)
// flags2, unknown1, ascii command length and data length stay zero
LittleEndian.putInt(data, 22, 0x40000001); // command2 char count
LittleEndian.putInt(data, 0, data.length - 4); // totalSize

RecordFormatException ex = assertThrows(
RecordFormatException.class,
() -> new Ole10Native(data, 0)
);
assertTrue(ex.getMessage().contains("Tried to allocate"));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use POITestCase assertContains

}

}