From: Simon McVittie Date: Wed, 25 Sep 2024 13:36:13 +0000 (+0100) Subject: sysdeps: Avoid passing possibly null port-number string to printf %s X-Git-Tag: dbus-1.15.10~2^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d8eaa77f43093ce3ef0d1f0130ef2f9f1880ef21;p=thirdparty%2Fdbus.git sysdeps: Avoid passing possibly null port-number string to printf %s 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 --- diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index aae9c4045..a00676f4e 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -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; } diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index ce9b7a75a..842f2d3cb 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -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)); }