PR29117: fix fd leak in debuginfod client for cache-miss files
authorFrank Ch. Eigler <fche@redhat.com>
Wed, 4 May 2022 14:26:42 +0000 (10:26 -0400)
committerFrank Ch. Eigler <fche@redhat.com>
Wed, 4 May 2022 14:27:11 +0000 (10:27 -0400)
Correct a nasty fd leak and a few less nasty leaks in the debuginfod
client code.  The nasty one impacts long-lived apps such as debuginfod
servers.

Signed-off-by: Mark Wielaard <mark@klomp.org>
Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
debuginfod/ChangeLog
debuginfod/debuginfod-client.c
debuginfod/debuginfod-find.c

index 93c8ae1..619ebd8 100644 (file)
@@ -1,3 +1,12 @@
+2022-05-04  Frank Ch. Eigler <fche@redhat.com>
+           Mark Wielaard  <mark@klomp.org>
+
+       * debuginfod-client.c (debuginfod_query_server): Correct fd leak
+       for cache negative-hit unlink case.
+       (debuginfod_config_cache, debuginfod_init_cache): Correct
+       minor fd leaks.
+       * debuginfod-find.c  (main): Ditto.
+
 2022-04-22  Mark Wielaard  <mark@klomp.org>
 
        * Makefile.am (libdebuginfod): Add -lpthread.
index ea6e461..521972e 100644 (file)
@@ -243,7 +243,13 @@ debuginfod_config_cache(char *config_path,
         return -errno;
 
       if (dprintf(fd, "%ld", cache_config_default_s) < 0)
-        return -errno;
+       {
+         int ret = -errno;
+         close (fd);
+         return ret;
+       }
+
+      close (fd);
     }
 
   long cache_config;
@@ -284,7 +290,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
     return -errno;
 
   if (dprintf(fd, "%ld", cache_clean_default_interval_s) < 0)
-    return -errno;
+    {
+      int ret = -errno;
+      close (fd);
+      return ret;
+    }
+
+  close (fd);
 
   /* init max age config file.  */
   if (stat(maxage_path, &st) != 0
@@ -292,8 +304,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
     return -errno;
 
   if (dprintf(fd, "%ld", cache_default_max_unused_age_s) < 0)
-    return -errno;
+    {
+      int ret = -errno;
+      close (fd);
+      return ret;
+    }
 
+  close (fd);
   return 0;
 }
 
@@ -812,18 +829,17 @@ debuginfod_query_server (debuginfod_client *c,
              has passed since the last attempt. */
           time_t cache_miss;
           time_t target_mtime = st.st_mtime;
+
+          close(fd); /* no need to hold onto the negative-hit file descriptor */
+          
           rc = debuginfod_config_cache(cache_miss_path,
                                        cache_miss_default_s, &st);
           if (rc < 0)
-            {
-              close(fd);
-              goto out;
-            }
+            goto out;
 
           cache_miss = (time_t)rc;
           if (time(NULL) - target_mtime <= cache_miss)
             {
-              close(fd);
               rc = -ENOENT;
               goto out;
             }
index 3e8ab20..f60b546 100644 (file)
@@ -231,6 +231,8 @@ main(int argc, char** argv)
       fprintf(stderr, "Server query failed: %s\n", strerror(-rc));
       return 1;
     }
+  else
+    close (rc);
 
   printf("%s\n", cache_name);
   free (cache_name);