elf.c (elf_add): Close descriptor if we use a debugfile.
authorIan Lance Taylor <iant@golang.org>
Wed, 31 Jan 2018 23:23:21 +0000 (23:23 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Wed, 31 Jan 2018 23:23:21 +0000 (23:23 +0000)
* elf.c (elf_add): Close descriptor if we use a debugfile.
* btest.c (check_open_files): New static function.
(main): Call check_open_files.

From-SVN: r257275

libbacktrace/ChangeLog
libbacktrace/btest.c
libbacktrace/elf.c

index bb449e4d8ac4eb5a8a87256eff567967f2e96803..9ebce200e37fa76917d17bd454da684bc79134dd 100644 (file)
@@ -1,3 +1,9 @@
+2018-01-31  Ian Lance Taylor  <iant@golang.org>
+
+       * elf.c (elf_add): Close descriptor if we use a debugfile.
+       * btest.c (check_open_files): New static function.
+       (main): Call check_open_files.
+
 2018-01-25  Ian Lance Taylor  <iant@golang.org>
 
        * elf.c (elf_open_debugfile_by_debuglink): Don't check CRC if the
index 5520699aac7e8e86982211caff857d2315d85d06..1348d54da509d4daed4697d93b43239ea64639b5 100644 (file)
@@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "filenames.h"
 
@@ -456,6 +457,25 @@ test5 (void)
   return failures;
 }
 
+/* Check that are no files left open.  */
+
+static void
+check_open_files (void)
+{
+  int i;
+
+  for (i = 3; i < 10; i++)
+    {
+      if (close (i) == 0)
+       {
+         fprintf (stderr,
+                  "ERROR: descriptor %d still open after tests complete\n",
+                  i);
+         ++failures;
+       }
+    }
+}
+
 /* Run all the tests.  */
 
 int
@@ -474,5 +494,7 @@ main (int argc ATTRIBUTE_UNUSED, char **argv)
 #endif
 #endif
 
+  check_open_files ();
+
   exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
 }
index 6403c08fa893d94ad7042e70a832d7a9143f87ab..3ee1dbeaf790cea1efdfc71edff77f9828b0edc8 100644 (file)
@@ -2929,12 +2929,19 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
                                         error_callback, data);
       if (d >= 0)
        {
+         int ret;
+
          backtrace_release_view (state, &buildid_view, error_callback, data);
          if (debuglink_view_valid)
            backtrace_release_view (state, &debuglink_view, error_callback,
                                    data);
-         return elf_add (state, NULL, d, base_address, error_callback, data,
-                         fileline_fn, found_sym, found_dwarf, 0, 1);
+         ret = elf_add (state, NULL, d, base_address, error_callback, data,
+                        fileline_fn, found_sym, found_dwarf, 0, 1);
+         if (ret < 0)
+           backtrace_close (d, error_callback, data);
+         else
+           backtrace_close (descriptor, error_callback, data);
+         return ret;
        }
     }
 
@@ -2953,10 +2960,17 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
                                           data);
       if (d >= 0)
        {
+         int ret;
+
          backtrace_release_view (state, &debuglink_view, error_callback,
                                  data);
-         return elf_add (state, NULL, d, base_address, error_callback, data,
-                         fileline_fn, found_sym, found_dwarf, 0, 1);
+         ret = elf_add (state, NULL, d, base_address, error_callback, data,
+                        fileline_fn, found_sym, found_dwarf, 0, 1);
+         if (ret < 0)
+           backtrace_close (d, error_callback, data);
+         else
+           backtrace_close(descriptor, error_callback, data);
+         return ret;
        }
     }