]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
sysdeps: Avoid passing possibly null port-number string to printf %s
authorSimon McVittie <smcv@collabora.com>
Wed, 25 Sep 2024 13:36:13 +0000 (14:36 +0100)
committerSimon McVittie <smcv@collabora.com>
Wed, 25 Sep 2024 13:42:15 +0000 (14:42 +0100)
gcc 14 detects that port can be NULL here, which is technically invalid
to pass to printf (although many implementations print it as "(null)").
Becuase we're using getaddrinfo(), we treat a NULL service (port number
string) as a request to listen on "port 0", meaning we ask the kernel to
assign an arbitrary nonzero port for us; when printing addresses in
error messages, treating that as port 0 seems reasonable.

Signed-off-by: Simon McVittie <smcv@collabora.com>
dbus/dbus-sysdeps-unix.c
dbus/dbus-sysdeps.c

index aae9c4045995ab2cb8c3542896ea1a21e4f969d4..a00676f4e354591e39140a981b4adbcb8b1a3870 100644 (file)
@@ -1652,7 +1652,7 @@ _dbus_listen_tcp_socket (const char     *host,
       dbus_set_error (error,
                       _dbus_error_from_gai (res, errno),
                       "Failed to lookup host/port: \"%s:%s\": %s (%d)",
-                      host ? host : "*", port, gai_strerror(res), res);
+                      host ? host : "*", port ? port : "0", gai_strerror(res), res);
       goto failed;
     }
 
@@ -1673,7 +1673,7 @@ _dbus_listen_tcp_socket (const char     *host,
       if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr))==-1)
         {
           _dbus_warn ("Failed to set socket option \"%s:%s\": %s",
-                      host ? host : "*", port, _dbus_strerror (errno));
+                      host ? host : "*", port ? port : "0", _dbus_strerror (errno));
         }
 
       /* Nagle's algorithm imposes a huge delay on the initial messages
@@ -1682,7 +1682,7 @@ _dbus_listen_tcp_socket (const char     *host,
       if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &tcp_nodelay_on, sizeof (tcp_nodelay_on)) == -1)
         {
           _dbus_warn ("Failed to set TCP_NODELAY socket option \"%s:%s\": %s",
-                      host ? host : "*", port, _dbus_strerror (errno));
+                      host ? host : "*", port ? port : "0", _dbus_strerror (errno));
         }
 
       if (bind (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0)
@@ -1783,7 +1783,7 @@ _dbus_listen_tcp_socket (const char     *host,
                   saved_errno = errno;
                   dbus_set_error (error, _dbus_error_from_errno (saved_errno),
                                   "Failed to retrieve socket name for \"%s:%s\": %s",
-                                  host ? host : "*", port, _dbus_strerror (saved_errno));
+                                  host ? host : "*", port ? port : "0", _dbus_strerror (saved_errno));
                   goto failed;
                 }
 
@@ -1794,7 +1794,7 @@ _dbus_listen_tcp_socket (const char     *host,
                   saved_errno = errno;
                   dbus_set_error (error, _dbus_error_from_gai (res, saved_errno),
                                   "Failed to resolve port \"%s:%s\": %s (%d)",
-                                  host ? host : "*", port, gai_strerror(res), res);
+                                  host ? host : "*", port ? port : "0", gai_strerror(res), res);
                   goto failed;
                 }
 
index ce9b7a75acecee1d05a57ade04c4986d29d6aca3..842f2d3cbbd866cc429cd1d11b17522974463de4 100644 (file)
@@ -933,7 +933,7 @@ _dbus_combine_tcp_errors (DBusList **sources,
         name = DBUS_ERROR_FAILED;
 
       dbus_set_error (dest, name, "%s to \"%s\":%s (%s)",
-                      summary, host ? host : "*", port,
+                      summary, host ? host : "*", port ? port : "0",
                       _dbus_string_get_const_data (&message));
     }