You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/01 19:43:10 UTC

[commons-io] branch master updated: Throw ArithmeticException if the position overflows an int

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-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 39b501a2 Throw ArithmeticException if the position overflows an int
39b501a2 is described below

commit 39b501a2c297df0344ff0a6963a3180a051488f5
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 1 15:43:06 2023 -0400

    Throw ArithmeticException if the position overflows an int
---
 .../java/org/apache/commons/io/build/AbstractOrigin.java     | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
index 6e2a9e83..c830c89f 100644
--- a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
+++ b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
@@ -414,19 +414,19 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends
      * Gets this origin as a byte array, if possible.
      *
      * @param position the initial index of the range to be copied, inclusive.
-     * @param length How many bytes to copy.
+     * @param length   How many bytes to copy.
      * @return this origin as a byte array, if possible.
-     * @throws IOException                   if an I/O error occurs.
      * @throws UnsupportedOperationException if the origin cannot be converted to a Path.
+     * @throws ArithmeticException           if the {@code position} overflows an int
+     * @throws IOException                   if an I/O error occurs.
      * @since 2.13.0
      */
     public byte[] getByteArray(final long position, final int length) throws IOException {
         final byte[] bytes = getByteArray();
-        final int start = (int) position;
-        // We include a separate check for int overflow.
+        // Checks for int overflow.
+        final int start = Math.toIntExact(position);
         if (start < 0 || length < 0 || start + length < 0 || start + length > bytes.length) {
-            throw new IllegalArgumentException("Couldn't read array (start: " + start + ", length: " + length
-                    + ", data length: " + bytes.length + ").");
+            throw new IllegalArgumentException("Couldn't read array (start: " + start + ", length: " + length + ", data length: " + bytes.length + ").");
         }
         return Arrays.copyOfRange(bytes, start, start + length);
     }