Skip to content
Merged
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
26 changes: 19 additions & 7 deletions poi/src/main/java/org/apache/poi/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,14 @@ public static long copy(InputStream inp, OutputStream out, long limit) throws IO
long totalCount = 0;
int readBytes = -1;
do {
int todoBytes = (int)((limit < 0) ? DEFAULT_BUFFER_SIZE : Math.min(limit-totalCount, DEFAULT_BUFFER_SIZE));
final int todoBytes;
try {
todoBytes = limit < 0 ?
DEFAULT_BUFFER_SIZE :
Math.toIntExact(Math.min(limit - totalCount, DEFAULT_BUFFER_SIZE));
} catch (ArithmeticException e) {
throw new IOException("Int Overflow calculating todoBytes", e);
}
if (todoBytes > 0) {
readBytes = inp.read(buff, 0, todoBytes);
if (readBytes > 0) {
Expand Down Expand Up @@ -556,7 +563,8 @@ public static long skipFully(final InputStream input, final long toSkip) throws
long remain = toSkip;
while (remain > 0) {
// See https://issues.apache.org/jira/browse/IO-203 for why we use read() rather than delegating to skip()
final long n = input.read(skipBuffer, 0, (int) Math.min(remain, SKIP_BUFFER_SIZE));
final long n = input.read(
skipBuffer, 0, Math.toIntExact(Math.min(remain, SKIP_BUFFER_SIZE)));
if (n < 0) { // EOF
break;
}
Expand All @@ -573,7 +581,11 @@ public static byte[] safelyAllocate(long length, int maxLength) {

checkByteSizeLimit(length);

return new byte[(int)length];
try {
return new byte[Math.toIntExact(length)];
} catch (ArithmeticException e) {
throw new RecordFormatException("Int Overflow with length", e);
}
}

public static void safelyAllocateCheck(long length, int maxLength) {
Expand Down Expand Up @@ -641,10 +653,10 @@ public static File newFile(final File parent, final String name) throws IOExcept

private static void throwRFE(long length, int maxLength) {
throw new RecordFormatException(String.format(Locale.ROOT, "Tried to allocate an array of length %,d" +
", but the maximum length for this record type is %,d.%n" +
"If the file is not corrupt and not large, please open an issue on bugzilla to request %n" +
"increasing the maximum allowable size for this record type.%n" +
"You can set a higher override value with IOUtils.setByteArrayMaxOverride()", length, maxLength));
", but the maximum length for this record type is %,d.%n" +
"If the file is not corrupt and not large, please open an issue on bugzilla to request %n" +
"increasing the maximum allowable size for this record type.%n" +
"You can set a higher override value with IOUtils.setByteArrayMaxOverride()", length, maxLength));
}

private static void throwRecordTruncationException(final int maxLength) {
Expand Down