From fe1e11724c444dfb03d4257bd9142b07f9652b6d Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 8 Mar 2018 13:26:15 +0000 Subject: [PATCH 1/7] _dbus_append_address_from_socket: Make control flow clearer In the current implementation, we can reach the "err" label either because we ran out of memory, or because we got a socket error from getsockname() or inet_ntop(), and we blindly assume that running out of memory will set ENOMEM. However, that isn't actually true: when we are simulating OOM in dbus_malloc(), the fake OOM doesn't set ENOMEM. Handle the OOM condition explicitly instead. In the AF_UNIX case, the break statement is no longer reached at all, so leave a comment there. In the AF_INET and AF_INET6 cases, if inet_ntop() fails, explicitly goto err instead of using break (this has the same practical effect) to make it clearer that we are going to the "socket error" code path. Signed-off-by: Simon McVittie --- dbus/dbus-sysdeps-unix.c | 55 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index d2a331eb..675e95ad 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -4674,31 +4674,64 @@ _dbus_append_address_from_socket (DBusSocket fd, _dbus_string_init_const (&path_str, &(socket.un.sun_path[1])); if (_dbus_string_append (address, "unix:abstract=") && _dbus_address_append_escaped (address, &path_str)) - return TRUE; + { + return TRUE; + } + else + { + _DBUS_SET_OOM (error); + return FALSE; + } } else { _dbus_string_init_const (&path_str, socket.un.sun_path); if (_dbus_string_append (address, "unix:path=") && _dbus_address_append_escaped (address, &path_str)) - return TRUE; + { + return TRUE; + } + else + { + _DBUS_SET_OOM (error); + return FALSE; + } } + /* not reached */ break; + case AF_INET: if (inet_ntop (AF_INET, &socket.ipv4.sin_addr, hostip, sizeof (hostip))) - if (_dbus_string_append_printf (address, "tcp:family=ipv4,host=%s,port=%u", - hostip, ntohs (socket.ipv4.sin_port))) - return TRUE; - break; + { + if (_dbus_string_append_printf (address, "tcp:family=ipv4,host=%s,port=%u", + hostip, ntohs (socket.ipv4.sin_port))) + { + return TRUE; + } + else + { + _DBUS_SET_OOM (error); + return FALSE; + } + } + goto err; + #ifdef AF_INET6 case AF_INET6: _dbus_string_init_const (&path_str, hostip); if (inet_ntop (AF_INET6, &socket.ipv6.sin6_addr, hostip, sizeof (hostip))) - if (_dbus_string_append_printf (address, "tcp:family=ipv6,port=%u,host=", - ntohs (socket.ipv6.sin6_port)) && - _dbus_address_append_escaped (address, &path_str)) - return TRUE; - break; + { + if (_dbus_string_append_printf (address, "tcp:family=ipv6,port=%u,host=", + ntohs (socket.ipv6.sin6_port)) && + _dbus_address_append_escaped (address, &path_str)) + else + { + _DBUS_SET_OOM (error); + return FALSE; + } + } + goto err; + #endif default: dbus_set_error (error, -- 2.16.2