avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [avro] 02/02: Remove some java6 code, add a configurable limit for maximum string lengths to read
Date Thu, 08 Nov 2018 19:08:47 GMT
This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git

commit 4fad66df0132c815a2a74c01704168d87f47009f
Author: Daniel Kulp <dkulp@apache.org>
AuthorDate: Thu Nov 8 13:28:50 2018 -0500

    Remove some java6 code, add a configurable limit for maximum string lengths to read
---
 .../src/main/java/org/apache/avro/util/Utf8.java    | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java b/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
index dd359dd..9a62664 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
@@ -19,15 +19,33 @@ package org.apache.avro.util;
 
 import java.nio.charset.Charset;
 
+import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.io.BinaryData;
+import org.slf4j.LoggerFactory;
 
 /** A Utf8 string.  Unlike {@link String}, instances are mutable.  This is more
  * efficient than {@link String} when reading or writing a sequence of values,
  * as a single instance may be reused. */
 public class Utf8 implements Comparable<Utf8>, CharSequence {
+  private static final String MAX_LENGTH_PROPERTY = "org.apache.avro.limits.string.maxLength";
+  private static final int MAX_LENGTH;
   private static final byte[] EMPTY = new byte[0];
   private static final Charset UTF8 = Charset.forName("UTF-8");
 
+  static {
+    String o = System.getProperty(MAX_LENGTH_PROPERTY);
+    int i = Integer.MAX_VALUE;
+    if (o != null) {
+      try {
+        i = Integer.parseUnsignedInt(o);
+      } catch (NumberFormatException nfe) {
+        LoggerFactory.getLogger(Utf8.class)
+          .warn("Could not parse property " + MAX_LENGTH_PROPERTY + ": " + o, nfe);
+      }
+    }
+    MAX_LENGTH = i;
+  }
+
   private byte[] bytes = EMPTY;
   private int length;
   private String string;
@@ -73,6 +91,9 @@ public class Utf8 implements Comparable<Utf8>, CharSequence {
   /** Set length in bytes.  Should called whenever byte content changes, even
    * if the length does not change, as this also clears the cached String. */
   public Utf8 setByteLength(int newLength) {
+    if (newLength > MAX_LENGTH) {
+      throw new AvroRuntimeException("String length " + newLength + " exceeds maximum allowed");
+    }
     if (this.bytes.length < newLength) {
       byte[] newBytes = new byte[newLength];
       System.arraycopy(bytes, 0, newBytes, 0, this.length);


Mime
View raw message