]> sourceware.org Git - glibc.git/commitdiff
elf/cache.c: Fix resource leaks identified by static analyzers
authorSiddhesh Poyarekar <siddhesh@sourceware.org>
Tue, 18 May 2021 03:38:41 +0000 (09:08 +0530)
committerSiddhesh Poyarekar <siddhesh@sourceware.org>
Tue, 18 May 2021 03:38:41 +0000 (09:08 +0530)
A coverity run identified a number of resource leaks in cache.c.
There are a couple of simple memory leaks where a local allocation is
not freed before function return.  Then there is a mmap leak and a
file descriptor leak where a map is not unmapped in the error case and
a file descriptor remains open respectively.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
elf/cache.c

index c01d30207218573fa6e47209560e561de1d1998e..8a3404923c625d184a2f25f7871f5dd0689e22b9 100644 (file)
@@ -547,6 +547,7 @@ write_extensions (int fd, uint32_t str_offset,
       || write (fd, generator, strlen (generator)) != strlen (generator))
     error (EXIT_FAILURE, errno, _("Writing of cache extension data failed"));
 
+  free (hwcaps_array);
   free (ext);
 }
 
@@ -778,6 +779,7 @@ save_cache (const char *cache_name)
   free (file_entries_new);
   free (file_entries);
   free (strings_finalized.strings);
+  free (temp_name);
 
   while (entries)
     {
@@ -1034,6 +1036,9 @@ load_aux_cache (const char *aux_cache_name)
                            + aux_cache->nlibs * sizeof (struct aux_cache_file_entry)
                            + aux_cache->len_strings))
     {
+      if (aux_cache != MAP_FAILED)
+       munmap (aux_cache, aux_cache_size);
+
       close (fd);
       init_aux_cache ();
       return;
@@ -1143,10 +1148,13 @@ save_aux_cache (const char *aux_cache_name)
   if (fd < 0)
     goto out_fail;
 
-  if (write (fd, file_entries, file_entries_size + total_strlen)
-      != (ssize_t) (file_entries_size + total_strlen)
-      || fdatasync (fd) != 0
-      || close (fd) != 0)
+  bool fail = ((write (fd, file_entries, file_entries_size + total_strlen)
+               != (ssize_t) (file_entries_size + total_strlen))
+              || fdatasync (fd) != 0);
+
+  fail |= close (fd) != 0;
+
+  if (fail)
     {
       unlink (temp_name);
       goto out_fail;
This page took 0.049438 seconds and 5 git commands to generate.