diff --git a/rskj-core/src/main/java/co/rsk/core/types/bytes/BoundaryUtils.java b/rskj-core/src/main/java/co/rsk/core/types/bytes/BoundaryUtils.java index d2ffe8a7691..d56e610b44f 100644 --- a/rskj-core/src/main/java/co/rsk/core/types/bytes/BoundaryUtils.java +++ b/rskj-core/src/main/java/co/rsk/core/types/bytes/BoundaryUtils.java @@ -26,10 +26,10 @@ public static void checkArraycopyParams(int srcLength, int srcPos, byte[] dest, if (length < 0) { throw new IndexOutOfBoundsException("invalid 'length': " + length); } - if (srcPos < 0 || srcPos + length > srcLength) { + if (srcPos < 0 || Long.sum(srcPos, length) > srcLength) { throw new IndexOutOfBoundsException("invalid 'srcPos' and/or 'length': [" + srcPos + ";" + length + ")"); } - if (destPos < 0 || destPos + length > dest.length) { + if (destPos < 0 || Long.sum(destPos, length) > dest.length) { throw new IndexOutOfBoundsException("invalid 'destPos' and/or 'length': [" + destPos + ";" + length + ")"); } } diff --git a/rskj-core/src/main/java/co/rsk/core/types/bytes/BytesSlice.java b/rskj-core/src/main/java/co/rsk/core/types/bytes/BytesSlice.java index 2e140f65410..82d89122753 100644 --- a/rskj-core/src/main/java/co/rsk/core/types/bytes/BytesSlice.java +++ b/rskj-core/src/main/java/co/rsk/core/types/bytes/BytesSlice.java @@ -104,10 +104,10 @@ default byte[] copyArrayOfRange(int from, int to) { if (from < 0 || from > length()) { throw new IndexOutOfBoundsException("invalid 'from': " + from); } - int newLength = to - from; - if (newLength < 0) { + if (to < from) { throw new IllegalArgumentException(from + " > " + to); } + int newLength = to - from; byte[] copy = new byte[newLength]; arraycopy(from, copy, 0, Math.min(length() - from, newLength)); return copy; diff --git a/rskj-core/src/main/java/co/rsk/core/types/bytes/HexPrintableBytes.java b/rskj-core/src/main/java/co/rsk/core/types/bytes/HexPrintableBytes.java index aa7e179ce6e..972fbeab784 100644 --- a/rskj-core/src/main/java/co/rsk/core/types/bytes/HexPrintableBytes.java +++ b/rskj-core/src/main/java/co/rsk/core/types/bytes/HexPrintableBytes.java @@ -86,7 +86,7 @@ default String toHexString() { * @return the hexadecimal representation of the bytes in the range of {@code offset} and {@code length}. */ default String toHexStringV2(int offset, int length) { - int endIndex = offset + length; + long endIndex = Long.sum(offset, length); if (offset < 0 || length < 0 || endIndex > length()) { throw new IndexOutOfBoundsException("invalid 'offset' and/or 'length': " + offset + "; " + length); } @@ -106,7 +106,7 @@ class PrintableBytesHexFormatter implements PrintableBytes.Formatter bytesLen) { + if (off < 0 || length < 0 || Long.sum(off, length) > bytesLen) { throw new IndexOutOfBoundsException("invalid 'off' and/or 'length': " + off + "; " + length); }