jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1311661 - in /jackrabbit/oak/trunk/oak-mk/src: main/java/org/apache/jackrabbit/mk/util/IOUtils.java test/java/org/apache/jackrabbit/mk/util/IOUtilsTest.java
Date Tue, 10 Apr 2012 10:01:13 GMT
Author: thomasm
Date: Tue Apr 10 10:01:12 2012
New Revision: 1311661

URL: http://svn.apache.org/viewvc?rev=1311661&view=rev
Log:
OAK-54 IOUtils.readVarInt and readVarLong can result in an endless loop on EOF

Modified:
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java
    jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/IOUtilsTest.java

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java?rev=1311661&r1=1311660&r2=1311661&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java (original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java Tue
Apr 10 10:01:12 2012
@@ -108,7 +108,7 @@ public class IOUtils {
     }
 
     /**
-     * Read a byte array. This will first read the length as 4 bytes, and then 
+     * Read a byte array. This will first read the length as 4 bytes, and then
      * the actual bytes.
      *
      * @param in the data input stream
@@ -154,7 +154,11 @@ public class IOUtils {
         }
         x &= 0x7f;
         for (int s = 7;; s += 7) {
-            int b = (byte) in.read();
+            int b = in.read();
+            if (b < 0) {
+                throw new EOFException();
+            }
+            b = (byte) b;
             x |= (b & 0x7f) << s;
             if (b >= 0) {
                 return x;
@@ -237,7 +241,11 @@ public class IOUtils {
         }
         x &= 0x7f;
         for (int s = 7;; s += 7) {
-            long b = (byte) in.read();
+            long b = in.read();
+            if (b < 0) {
+                throw new EOFException();
+            }
+            b = (byte) b;
             x |= (b & 0x7f) << s;
             if (b >= 0) {
                 return x;

Modified: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/IOUtilsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/IOUtilsTest.java?rev=1311661&r1=1311660&r2=1311661&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/IOUtilsTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/IOUtilsTest.java
Tue Apr 10 10:01:12 2012
@@ -197,6 +197,20 @@ public class IOUtilsTest extends TestCas
         assertEquals(-1, in.read());
     }
 
+    public void testVarEOF() throws IOException {
+        try {
+            IOUtils.readVarInt(new ByteArrayInputStream(new byte[0]));
+            fail();
+        } catch (EOFException e) {
+            // expected
+        }
+        try {
+            IOUtils.readVarLong(new ByteArrayInputStream(new byte[0]));
+            fail();
+        } catch (EOFException e) {
+            // expected
+        }
+    }
 
     private void testVarInt(int x, int expectedLen) throws IOException {
         ByteArrayOutputStream out = new ByteArrayOutputStream();



Mime
View raw message