3ccea2
commit c424e5f3d24f76e01242d15ba361dc6234706fed
3ccea2
Author: Frank Ch. Eigler <fche@redhat.com>
3ccea2
Date:   Thu Nov 3 10:07:31 2022 -0400
3ccea2
3ccea2
    debuginfod.cxx: fix coverity-found use-after-release error
3ccea2
    
3ccea2
    The debuginfod_client object lifetime needs more careful handling,
3ccea2
    made easier with the defer_dtor<> gadget.
3ccea2
    
3ccea2
    Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
3ccea2
3ccea2
diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
3ccea2
index f46da6ef..02a11477 100644
3ccea2
--- a/debuginfod/debuginfod.cxx
3ccea2
+++ b/debuginfod/debuginfod.cxx
3ccea2
@@ -2249,85 +2249,82 @@ handle_buildid (MHD_Connection* conn,
3ccea2
 
3ccea2
   int fd = -1;
3ccea2
   debuginfod_client *client = debuginfod_pool_begin ();
3ccea2
-  if (client != NULL)
3ccea2
-    {
3ccea2
-      debuginfod_set_progressfn (client, & debuginfod_find_progress);
3ccea2
+  if (client == NULL)
3ccea2
+    throw libc_exception(errno, "debuginfod client pool alloc");
3ccea2
+  defer_dtor<debuginfod_client*,void> client_closer (client, debuginfod_pool_end);
3ccea2
+  
3ccea2
+  debuginfod_set_progressfn (client, & debuginfod_find_progress);
3ccea2
 
3ccea2
-      if (conn)
3ccea2
-        {
3ccea2
-          // Transcribe incoming User-Agent:
3ccea2
-          string ua = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
3ccea2
-          string ua_complete = string("User-Agent: ") + ua;
3ccea2
-          debuginfod_add_http_header (client, ua_complete.c_str());
3ccea2
-
3ccea2
-          // Compute larger XFF:, for avoiding info loss during
3ccea2
-          // federation, and for future cyclicity detection.
3ccea2
-          string xff = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
3ccea2
-          if (xff != "")
3ccea2
-            xff += string(", "); // comma separated list
3ccea2
-
3ccea2
-          unsigned int xff_count = 0;
3ccea2
-          for (auto&& i : xff){
3ccea2
-            if (i == ',') xff_count++;
3ccea2
-          }
3ccea2
+  if (conn)
3ccea2
+    {
3ccea2
+      // Transcribe incoming User-Agent:
3ccea2
+      string ua = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
3ccea2
+      string ua_complete = string("User-Agent: ") + ua;
3ccea2
+      debuginfod_add_http_header (client, ua_complete.c_str());
3ccea2
+      
3ccea2
+      // Compute larger XFF:, for avoiding info loss during
3ccea2
+      // federation, and for future cyclicity detection.
3ccea2
+      string xff = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
3ccea2
+      if (xff != "")
3ccea2
+        xff += string(", "); // comma separated list
3ccea2
+      
3ccea2
+      unsigned int xff_count = 0;
3ccea2
+      for (auto&& i : xff){
3ccea2
+        if (i == ',') xff_count++;
3ccea2
+      }
3ccea2
 
3ccea2
-          // if X-Forwarded-For: exceeds N hops,
3ccea2
-          // do not delegate a local lookup miss to upstream debuginfods.
3ccea2
-          if (xff_count >= forwarded_ttl_limit)
3ccea2
-            throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found, --forwared-ttl-limit reached \
3ccea2
+      // if X-Forwarded-For: exceeds N hops,
3ccea2
+      // do not delegate a local lookup miss to upstream debuginfods.
3ccea2
+      if (xff_count >= forwarded_ttl_limit)
3ccea2
+        throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found, --forwared-ttl-limit reached \
3ccea2
 and will not query the upstream servers");
3ccea2
 
3ccea2
-          // Compute the client's numeric IP address only - so can't merge with conninfo()
3ccea2
-          const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
3ccea2
-                                                                       MHD_CONNECTION_INFO_CLIENT_ADDRESS);
3ccea2
-          struct sockaddr *so = u ? u->client_addr : 0;
3ccea2
-          char hostname[256] = ""; // RFC1035
3ccea2
-          if (so && so->sa_family == AF_INET) {
3ccea2
-            (void) getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), NULL, 0,
3ccea2
-                                NI_NUMERICHOST);
3ccea2
-          } else if (so && so->sa_family == AF_INET6) {
3ccea2
-            struct sockaddr_in6* addr6 = (struct sockaddr_in6*) so;
3ccea2
-            if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
3ccea2
-              struct sockaddr_in addr4;
3ccea2
-              memset (&addr4, 0, sizeof(addr4));
3ccea2
-              addr4.sin_family = AF_INET;
3ccea2
-              addr4.sin_port = addr6->sin6_port;
3ccea2
-              memcpy (&addr4.sin_addr.s_addr, addr6->sin6_addr.s6_addr+12, sizeof(addr4.sin_addr.s_addr));
3ccea2
-              (void) getnameinfo ((struct sockaddr*) &addr4, sizeof (addr4),
3ccea2
-                                  hostname, sizeof (hostname), NULL, 0,
3ccea2
-                                  NI_NUMERICHOST);
3ccea2
-            } else {
3ccea2
-              (void) getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname), NULL, 0,
3ccea2
-                                  NI_NUMERICHOST);
3ccea2
-            }
3ccea2
-          }
3ccea2
-          
3ccea2
-          string xff_complete = string("X-Forwarded-For: ")+xff+string(hostname);
3ccea2
-          debuginfod_add_http_header (client, xff_complete.c_str());
3ccea2
+      // Compute the client's numeric IP address only - so can't merge with conninfo()
3ccea2
+      const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
3ccea2
+                                                                   MHD_CONNECTION_INFO_CLIENT_ADDRESS);
3ccea2
+      struct sockaddr *so = u ? u->client_addr : 0;
3ccea2
+      char hostname[256] = ""; // RFC1035
3ccea2
+      if (so && so->sa_family == AF_INET) {
3ccea2
+        (void) getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), NULL, 0,
3ccea2
+                            NI_NUMERICHOST);
3ccea2
+      } else if (so && so->sa_family == AF_INET6) {
3ccea2
+        struct sockaddr_in6* addr6 = (struct sockaddr_in6*) so;
3ccea2
+        if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
3ccea2
+          struct sockaddr_in addr4;
3ccea2
+          memset (&addr4, 0, sizeof(addr4));
3ccea2
+          addr4.sin_family = AF_INET;
3ccea2
+          addr4.sin_port = addr6->sin6_port;
3ccea2
+          memcpy (&addr4.sin_addr.s_addr, addr6->sin6_addr.s6_addr+12, sizeof(addr4.sin_addr.s_addr));
3ccea2
+          (void) getnameinfo ((struct sockaddr*) &addr4, sizeof (addr4),
3ccea2
+                              hostname, sizeof (hostname), NULL, 0,
3ccea2
+                              NI_NUMERICHOST);
3ccea2
+        } else {
3ccea2
+          (void) getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname), NULL, 0,
3ccea2
+                              NI_NUMERICHOST);
3ccea2
         }
3ccea2
-
3ccea2
-      if (artifacttype == "debuginfo")
3ccea2
-	fd = debuginfod_find_debuginfo (client,
3ccea2
-					(const unsigned char*) buildid.c_str(),
3ccea2
-					0, NULL);
3ccea2
-      else if (artifacttype == "executable")
3ccea2
-	fd = debuginfod_find_executable (client,
3ccea2
-					 (const unsigned char*) buildid.c_str(),
3ccea2
-					 0, NULL);
3ccea2
-      else if (artifacttype == "source")
3ccea2
-	fd = debuginfod_find_source (client,
3ccea2
-				     (const unsigned char*) buildid.c_str(),
3ccea2
-				     0, suffix.c_str(), NULL);
3ccea2
-      else if (artifacttype == "section")
3ccea2
-	fd = debuginfod_find_section (client,
3ccea2
-				      (const unsigned char*) buildid.c_str(),
3ccea2
-				      0, section.c_str(), NULL);
3ccea2
-
3ccea2
+      }
3ccea2
+          
3ccea2
+      string xff_complete = string("X-Forwarded-For: ")+xff+string(hostname);
3ccea2
+      debuginfod_add_http_header (client, xff_complete.c_str());
3ccea2
     }
3ccea2
-  else
3ccea2
-    fd = -errno; /* Set by debuginfod_begin.  */
3ccea2
-  debuginfod_pool_end (client);
3ccea2
-
3ccea2
+  
3ccea2
+  if (artifacttype == "debuginfo")
3ccea2
+    fd = debuginfod_find_debuginfo (client,
3ccea2
+                                    (const unsigned char*) buildid.c_str(),
3ccea2
+                                    0, NULL);
3ccea2
+  else if (artifacttype == "executable")
3ccea2
+    fd = debuginfod_find_executable (client,
3ccea2
+                                     (const unsigned char*) buildid.c_str(),
3ccea2
+                                     0, NULL);
3ccea2
+  else if (artifacttype == "source")
3ccea2
+    fd = debuginfod_find_source (client,
3ccea2
+                                 (const unsigned char*) buildid.c_str(),
3ccea2
+                                 0, suffix.c_str(), NULL);
3ccea2
+  else if (artifacttype == "section")
3ccea2
+    fd = debuginfod_find_section (client,
3ccea2
+                                  (const unsigned char*) buildid.c_str(),
3ccea2
+                                  0, section.c_str(), NULL);
3ccea2
+  
3ccea2
   if (fd >= 0)
3ccea2
     {
3ccea2
       if (conn != 0)