Fix: some exeptional resource leaks.
authorFelix Dörre <felix@dogcraft.de>
Sun, 22 Feb 2015 00:28:18 +0000 (01:28 +0100)
committerFelix Dörre <felix@dogcraft.de>
Sun, 22 Feb 2015 00:28:18 +0000 (01:28 +0100)
tests/org/cacert/gigi/TestSSL.java
tests/org/cacert/gigi/ping/TestSSL.java
util/org/cacert/gigi/util/DatabaseManager.java

index 83bc7f69b51214e5ab63675a3668c8288ac2a68e..74806eef8bd74170e5bbdbce74230e2fe03fe3d1 100644 (file)
@@ -35,24 +35,25 @@ public class TestSSL extends ManagedTest {
         SSLContext sc = SSLContext.getDefault();
         SSLEngine se = sc.createSSLEngine();
         String[] serverParts = getServerName().split(":", 2);
-        SocketChannel s = SocketChannel.open(new InetSocketAddress(serverParts[0], Integer.parseInt(serverParts[1])));
-
-        in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
-        inC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
-        inC.limit(0);
-        out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
-        outC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
-        outC.limit(0);
-        se.setUseClientMode(true);
-        se.beginHandshake();
-
-        work(se, s);
-        se.beginHandshake();
-        try {
+        try (SocketChannel s = SocketChannel.open(new InetSocketAddress(serverParts[0], Integer.parseInt(serverParts[1])))) {
+
+            in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
+            inC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
+            inC.limit(0);
+            out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
+            outC = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
+            outC.limit(0);
+            se.setUseClientMode(true);
+            se.beginHandshake();
+
             work(se, s);
-            throw new Error("Client re-negotiation succeded (possible DoS vulnerability");
-        } catch (EOFException e) {
-            // Cool, server closed connection
+            se.beginHandshake();
+            try {
+                work(se, s);
+                throw new Error("Client re-negotiation succeded (possible DoS vulnerability");
+            } catch (EOFException e) {
+                // Cool, server closed connection
+            }
         }
 
     }
index fccd97ebab28031bcd1d6d58da56cce40c8c0fba..e877fa6f208cd321c13d787d0c6295d1eba26ff9 100644 (file)
@@ -143,8 +143,7 @@ public class TestSSL extends PingTest {
     }
 
     private boolean acceptSSLServer(SSLServerSocket sss) throws IOException {
-        try {
-            Socket s = sss.accept();
+        try (Socket s = sss.accept()) {
             s.getOutputStream().write('b');
             s.getOutputStream().close();
             return true;
index 84c012c3586a55d564e43d747dd7a61528ce6c26..da164a56b04f3207d36bf2186d3476b76a5b1f91 100644 (file)
@@ -44,23 +44,27 @@ public class DatabaseManager {
 
     public static void run(String[] args, ImportType truncate) throws ClassNotFoundException, SQLException, IOException {
         Class.forName(args[0]);
-        Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
-        conn.setAutoCommit(false);
-        Statement stmt = conn.createStatement();
-        try (InputStream structure = DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
-            SQLFileManager.addFile(stmt, structure, truncate);
-        }
-        File localData = new File("doc/sampleData.sql");
-        if (localData.exists()) {
-            try (FileInputStream f = new FileInputStream(localData)) {
-                SQLFileManager.addFile(stmt, f, ImportType.PRODUCTION);
+        final Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
+        try {
+            conn.setAutoCommit(false);
+            Statement stmt = conn.createStatement();
+            try {
+                try (InputStream structure = DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
+                    SQLFileManager.addFile(stmt, structure, truncate);
+                }
+                File localData = new File("doc/sampleData.sql");
+                if (localData.exists()) {
+                    try (FileInputStream f = new FileInputStream(localData)) {
+                        SQLFileManager.addFile(stmt, f, ImportType.PRODUCTION);
+                    }
+                }
+                stmt.executeBatch();
+                conn.commit();
+            } finally {
+                stmt.close();
             }
+        } finally {
+            conn.close();
         }
-        stmt.executeBatch();
-        conn.commit();
-        stmt.close();
-
-        conn.close();
     }
-
 }