From e1d6fd42e9cc262e090699072be865c8f97a0be9 Mon Sep 17 00:00:00 2001 From: Volodymyr Kravets Date: Wed, 9 Oct 2024 11:57:43 +0300 Subject: [PATCH] perf: add overflow check --- .../main/java/co/rsk/core/types/bytes/BoundaryUtils.java | 4 ++-- .../src/main/java/co/rsk/core/types/bytes/BytesSlice.java | 4 ++-- .../java/co/rsk/core/types/bytes/HexPrintableBytes.java | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) 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..de8dfb1e697 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,11 +86,11 @@ 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; - if (offset < 0 || length < 0 || endIndex > length()) { + if (offset < 0 || length < 0 || Long.sum(offset, length) > length()) { throw new IndexOutOfBoundsException("invalid 'offset' and/or 'length': " + offset + "; " + length); } + int endIndex = offset + length; StringBuilder sb = new StringBuilder(length * 2); for (int i = offset; i < endIndex; i++) { byte b = byteAt(i); @@ -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); }