Ver Fonte

ZOOKEEPER-1189. For an invalid snapshot file(less than 10bytes size) RandomAccessFile stream is leaking. (Rakesh R via mahadev)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1176144 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar há 13 anos atrás
pai
commit
05287a9c14

+ 3 - 0
CHANGES.txt

@@ -367,6 +367,9 @@ BUGFIXES:
   ZOOKEEPER-1136. NEW_LEADER should be queued not sent to match the Zab 1.0 protocol 
   on the twiki (breed via mahadev)
 
+  ZOOKEEPER-1189. For an invalid snapshot file(less than 10bytes size) RandomAccessFile 
+  stream is leaking. (Rakesh R via mahadev)
+
 IMPROVEMENTS:
   ZOOKEEPER-724. Improve junit test integration - log harness information 
   (phunt via mahadev)

+ 5 - 5
src/java/main/org/apache/zookeeper/server/persistence/Util.java

@@ -164,12 +164,12 @@ public class Util {
 
         // Check for a valid snapshot
         RandomAccessFile raf = new RandomAccessFile(f, "r");
-        // including the header and the last / bytes
-        // the snapshot should be atleast 10 bytes
-        if (raf.length() < 10) {
-            return false;
-        }
         try {
+            // including the header and the last / bytes
+            // the snapshot should be atleast 10 bytes
+            if (raf.length() < 10) {
+                return false;
+            }
             raf.seek(raf.length() - 5);
             byte bytes[] = new byte[5];
             int readlen = 0;

+ 21 - 1
src/java/test/org/apache/zookeeper/server/ZooKeeperServerTest.java

@@ -19,14 +19,15 @@
 package org.apache.zookeeper.server;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.List;
 
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.server.persistence.FileTxnLog;
 import org.apache.zookeeper.server.persistence.Util;
+import org.apache.zookeeper.test.ClientBase;
 import org.junit.Assert;
 import org.junit.Test;
-import org.junit.Assert;
 
 public class ZooKeeperServerTest extends ZKTestCase {
     @Test
@@ -114,4 +115,23 @@ public class ZooKeeperServerTest extends ZKTestCase {
         }
     }
 
+    @Test
+    public void testInvalidSnapshot() {
+        File f = null;
+        File tmpFileDir = null;
+        try {
+            tmpFileDir = ClientBase.createTmpDir();
+            f = new File(tmpFileDir, "snapshot.0");
+            if (!f.exists()) {
+                f.createNewFile();
+            }
+            Assert.assertFalse("Snapshot file size is greater than 9 bytes", Util.isValidSnapshot(f));
+            Assert.assertTrue("Can't delete file", f.delete());
+        } catch (IOException e) {
+        } finally {
+            if (null != tmpFileDir) {
+                ClientBase.recursiveDelete(tmpFileDir);
+            }
+        }
+    }
 }