This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-imaging.git
commit a8390a1def194b0d2dd5a8c83de62223c3d75f9c Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue May 16 14:11:47 2023 -0400 Better exception handling --- .../imaging/common/AllocationRequestException.java | 22 ++++++++++++++++------ .../apache/commons/imaging/common/Allocator.java | 10 +++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/commons/imaging/common/AllocationRequestException.java b/src/main/java/org/apache/commons/imaging/common/AllocationRequestException.java index f05193aa..a01b99af 100644 --- a/src/main/java/org/apache/commons/imaging/common/AllocationRequestException.java +++ b/src/main/java/org/apache/commons/imaging/common/AllocationRequestException.java @@ -17,6 +17,8 @@ package org.apache.commons.imaging.common; +import java.math.BigInteger; + /** * Thrown when an allocation request is too large. */ @@ -25,7 +27,7 @@ public class AllocationRequestException extends ImagingRuntimeException { private static final long serialVersionUID = 1L; private final int limit; - private final long request; + private final BigInteger request; /** * Constructs a new instance. @@ -33,12 +35,22 @@ public class AllocationRequestException extends ImagingRuntimeException { * @param limit The allocation limit. * @param request The allocation request. */ - public AllocationRequestException(final int limit, final int request) { + public AllocationRequestException(final int limit, final BigInteger request) { super(String.format("Allocation limit %,d exceeded: %,d", limit, request)); this.limit = limit; this.request = request; } + /** + * Constructs a new instance. + * + * @param limit The allocation limit. + * @param request The allocation request. + */ + public AllocationRequestException(final int limit, final int request) { + this(limit, BigInteger.valueOf(request)); + } + /** * Constructs a new instance. * @@ -46,9 +58,7 @@ public class AllocationRequestException extends ImagingRuntimeException { * @param request The allocation request. */ public AllocationRequestException(final int limit, final long request) { - super(String.format("Allocation limit %,d exceeded: %,d", limit, request)); - this.limit = limit; - this.request = request; + this(limit, BigInteger.valueOf(request)); } /** @@ -65,7 +75,7 @@ public class AllocationRequestException extends ImagingRuntimeException { * * @return the allocation request. */ - public long getRequest() { + public BigInteger getRequest() { return request; } } diff --git a/src/main/java/org/apache/commons/imaging/common/Allocator.java b/src/main/java/org/apache/commons/imaging/common/Allocator.java index 3316975d..cf2d4567 100644 --- a/src/main/java/org/apache/commons/imaging/common/Allocator.java +++ b/src/main/java/org/apache/commons/imaging/common/Allocator.java @@ -17,6 +17,7 @@ package org.apache.commons.imaging.common; +import java.math.BigInteger; import java.util.ArrayList; import java.util.function.IntFunction; @@ -144,10 +145,14 @@ public class Allocator { * @param elementSize The element size. * @return the request. * @throws AllocationRequestException Thrown when the request exceeds the limit. - * @throws ArithmeticException if the result overflows an int. */ public static int check(final int request, final int elementSize) { - final int multiplyExact = Math.multiplyExact(request, elementSize); + int multiplyExact; + try { + multiplyExact = Math.multiplyExact(request, elementSize); + } catch (ArithmeticException e) { + throw new AllocationRequestException(LIMIT, BigInteger.valueOf(request).multiply(BigInteger.valueOf(elementSize))); + } if (multiplyExact > LIMIT) { throw new AllocationRequestException(LIMIT, request); } @@ -165,7 +170,6 @@ public class Allocator { * @param elementSize The element size. * @return the request. * @throws AllocationRequestException Thrown when the request exceeds the limit. - * @throws ArithmeticException if the result overflows an int. */ public static int check(final long request, final int elementSize) { if (request > Integer.MAX_VALUE) {