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