You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by dm...@apache.org on 2024/10/08 23:33:34 UTC
(avro) branch main updated: AVRO-4070: Optimize Check Max Collection Length for New Collections (#3195)
This is an automated email from the ASF dual-hosted git repository.
dmollitor pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/main by this push:
new 9304cb793 AVRO-4070: Optimize Check Max Collection Length for New Collections (#3195)
9304cb793 is described below
commit 9304cb793aee4d5b1da56b1dced3b7e65329779f
Author: belugabehr <12...@users.noreply.github.com>
AuthorDate: Tue Oct 8 19:33:29 2024 -0400
AVRO-4070: Optimize Check Max Collection Length for New Collections (#3195)
---
.../java/org/apache/avro/SystemLimitException.java | 29 ++++++++++++++++++++++
.../java/org/apache/avro/io/BinaryDecoder.java | 4 +--
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/lang/java/avro/src/main/java/org/apache/avro/SystemLimitException.java b/lang/java/avro/src/main/java/org/apache/avro/SystemLimitException.java
index a96f812d8..b4e468345 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/SystemLimitException.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/SystemLimitException.java
@@ -153,6 +153,35 @@ public class SystemLimitException extends AvroRuntimeException {
return (int) length;
}
+ /**
+ * Check to ensure that reading the specified number of items remains within the
+ * specified limits.
+ *
+ * @param items The next number of items to read. In normal usage, this is
+ * always a positive, permitted value. Negative and zero values
+ * have a special meaning in Avro decoding.
+ * @return The total number of items in the collection if and only if it is
+ * within the limit and non-negative.
+ * @throws UnsupportedOperationException if reading the items would allocate a
+ * collection that the Java VM would be
+ * unable to handle
+ * @throws SystemLimitException if the decoding should fail because it
+ * would otherwise result in an allocation
+ * exceeding the set limit
+ * @throws AvroRuntimeException if the length is negative
+ */
+ public static int checkMaxCollectionLength(long items) {
+ if (items > MAX_ARRAY_VM_LIMIT) {
+ throw new UnsupportedOperationException(
+ "Cannot read collections larger than " + MAX_ARRAY_VM_LIMIT + " items in Java library");
+ }
+ if (items > maxCollectionLength) {
+ throw new SystemLimitException(
+ "Collection length " + items + " exceeds the maximum allowed of " + maxCollectionLength);
+ }
+ return (int) items;
+ }
+
/**
* Check to ensure that reading the string size is within the specified limits.
*
diff --git a/lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java b/lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java
index 7217be3ad..865bc9c06 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java
@@ -428,7 +428,7 @@ public class BinaryDecoder extends Decoder {
@Override
public long readArrayStart() throws IOException {
- collectionCount = SystemLimitException.checkMaxCollectionLength(0L, doReadItemCount());
+ collectionCount = SystemLimitException.checkMaxCollectionLength(doReadItemCount());
return collectionCount;
}
@@ -446,7 +446,7 @@ public class BinaryDecoder extends Decoder {
@Override
public long readMapStart() throws IOException {
- collectionCount = SystemLimitException.checkMaxCollectionLength(0L, doReadItemCount());
+ collectionCount = SystemLimitException.checkMaxCollectionLength(doReadItemCount());
return collectionCount;
}