ctf: bug 471966: Fix file handle leak in CTF
authorMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Mon, 6 Jul 2015 22:26:54 +0000 (18:26 -0400)
committerMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tue, 7 Jul 2015 15:26:48 +0000 (11:26 -0400)
If something goes wrong in the CTFStreamInputReader constructor or one
of its calls, the file handles in this class are not closed. This is
because the object is not fully constructed yet therefore the caller of
the constructor cannot close it.

Change-Id: Ie03164888711dd49d7b30ce5d774b4e4cdef9228
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/51426
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/trace/CTFStreamInputReader.java

index 9c62495f5442617cf02b6260639dc95e2fbdd440..c4d36a9d1df6587f5552a60b1d19eb47b8a7e45c 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2015 Ericsson, Ecole Polytechnique de Montreal and others
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
@@ -95,15 +95,24 @@ public class CTFStreamInputReader implements AutoCloseable {
         } catch (IOException e) {
             throw new CTFIOException(e);
         }
-        fPacketReader = new CTFStreamInputPacketReader(this);
-        /*
-         * Get the iterator on the packet index.
-         */
-        fPacketIndex = 0;
-        /*
-         * Make first packet the current one.
-         */
-        goToNextPacket();
+        try {
+            fPacketReader = new CTFStreamInputPacketReader(this);
+            /*
+             * Get the iterator on the packet index.
+             */
+            fPacketIndex = 0;
+            /*
+             * Make first packet the current one.
+             */
+            goToNextPacket();
+        } catch (Exception e) {
+            try {
+                close();
+            } catch (IOException e1) {
+                // Ignore
+            }
+            throw e;
+        }
     }
 
     /**
@@ -115,8 +124,12 @@ public class CTFStreamInputReader implements AutoCloseable {
      */
     @Override
     public void close() throws IOException {
-        fFileChannel.close();
-        fPacketReader.close();
+        if (fFileChannel != null) {
+            fFileChannel.close();
+        }
+        if (fPacketReader != null) {
+            fPacketReader.close();
+        }
     }
 
     // ------------------------------------------------------------------------
This page took 0.027066 seconds and 5 git commands to generate.