From 265e4b58a5d4f86e357acd9aceb492e592d9a748 Mon Sep 17 00:00:00 2001
From: str4d <str4d@mail.i2p>
Date: Thu, 23 Jul 2015 01:15:11 +0000
Subject: [PATCH] Throw DataFormatException if not enough bytes

---
 core/java/src/net/i2p/data/Certificate.java | 28 +++++++++++++--------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/core/java/src/net/i2p/data/Certificate.java b/core/java/src/net/i2p/data/Certificate.java
index fdc343a63e..7b40b50992 100644
--- a/core/java/src/net/i2p/data/Certificate.java
+++ b/core/java/src/net/i2p/data/Certificate.java
@@ -48,19 +48,25 @@ public class Certificate extends DataStructureImpl {
 
     /**
      * If null cert, return immutable static instance, else create new
-     * @throws AIOOBE if not enough bytes, FIXME should throw DataFormatException
+     * @throws DataFormatException if not enough bytes
      * @since 0.8.3
      */
-    public static Certificate create(byte[] data, int off) {
-        int type = data[off] & 0xff;
-        int length = (int) DataHelper.fromLong(data, off + 1, 2);
-        if (type == 0 && length == 0)
-            return NULL_CERT;
-        // from here down roughly the same as readBytes() below
-        if (length == 0)
-            return new Certificate(type, null);
-        byte[] payload = new byte[length];
-        System.arraycopy(data, off + 3, payload, 0, length);
+    public static Certificate create(byte[] data, int off) throws DataFormatException {
+    	int type;
+    	byte[] payload;
+    	try {
+            type = data[off] & 0xff;
+            int length = (int) DataHelper.fromLong(data, off + 1, 2);
+            if (type == 0 && length == 0)
+                return NULL_CERT;
+            // from here down roughly the same as readBytes() below
+            if (length == 0)
+                return new Certificate(type, null);
+            payload = new byte[length];
+            System.arraycopy(data, off + 3, payload, 0, length);
+    	} catch (ArrayIndexOutOfBoundsException aioobe) {
+    		throw new DataFormatException("not enough bytes", aioobe);
+    	}
         if (type == CERTIFICATE_TYPE_KEY) {
             try {
                 return new KeyCertificate(payload);
-- 
GitLab