From 24b673dc1468af5adf8bfefe0130e65078eb4586 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Tue, 14 Apr 2015 23:17:40 +0200 Subject: [PATCH] Always assert that BUS_CONNECTION_DATA() returns non-NULL Every DBusConnection in the dbus-daemon should have been through bus_connections_setup_connection(), so we can assert that the BusConnectionData has been attached to it. Having this assertion is enough to hint to Coverity that it does not need to worry about whether this pointer might be NULL. In regression tests, we do work with a few fake client-side DBusConnection instances in the same process; but it would be a serious bug if we mixed those up with the ones processed by dbus-daemon's real code, so the assertion is still valid. This patch has been inspired by (and fixes) the following coverity scan issues: CID 54846: Dereference null return value (NULL_RETURNS). CID 54854: Dereference null return value (NULL_RETURNS). Bug: https://bugs.freedesktop.org/show_bug.cgi?id=90021 [smcv: fixed -Wdeclaration-after-statement; more informative commit message] --- bus/connection.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/bus/connection.c b/bus/connection.c index 690f3a5..95e20a6 100644 --- a/bus/connection.c +++ b/bus/connection.c @@ -35,6 +35,7 @@ #include #include #include +#include /* Trim executed commands to this length; we want to keep logs readable */ #define MAX_LOG_COMMAND_LEN 50 @@ -134,6 +135,7 @@ connection_get_loop (DBusConnection *connection) BusConnectionData *d; d = BUS_CONNECTION_DATA (connection); + _dbus_assert(d != NULL); return bus_context_get_loop (d->connections->context); } @@ -638,9 +640,12 @@ static void check_pending_fds_cb (DBusConnection *connection) { BusConnectionData *d = BUS_CONNECTION_DATA (connection); - int n_pending_unix_fds_old = d->n_pending_unix_fds; + int n_pending_unix_fds_old; int n_pending_unix_fds_new; + _dbus_assert(d != NULL); + + n_pending_unix_fds_old = d->n_pending_unix_fds; n_pending_unix_fds_new = _dbus_connection_get_pending_fds_count (connection); _dbus_verbose ("Pending fds count changed on connection %p: %d -> %d\n", @@ -1012,6 +1017,7 @@ bus_connection_get_loginfo (DBusConnection *connection) BusConnectionData *d; d = BUS_CONNECTION_DATA (connection); + _dbus_assert(d != NULL); if (!bus_connection_is_active (connection)) return "inactive"; @@ -1248,8 +1254,9 @@ bus_connection_is_active (DBusConnection *connection) BusConnectionData *d; d = BUS_CONNECTION_DATA (connection); + _dbus_assert(d != NULL); - return d != NULL && d->name != NULL; + return d->name != NULL; } dbus_bool_t @@ -2649,6 +2656,8 @@ bus_connection_get_peak_match_rules (DBusConnection *connection) BusConnectionData *d; d = BUS_CONNECTION_DATA (connection); + _dbus_assert(d != NULL); + return d->peak_match_rules; } @@ -2658,6 +2667,8 @@ bus_connection_get_peak_bus_names (DBusConnection *connection) BusConnectionData *d; d = BUS_CONNECTION_DATA (connection); + _dbus_assert(d != NULL); + return d->peak_bus_names; } #endif /* DBUS_ENABLE_STATS */ @@ -2668,8 +2679,9 @@ bus_connection_is_monitor (DBusConnection *connection) BusConnectionData *d; d = BUS_CONNECTION_DATA (connection); + _dbus_assert(d != NULL); - return d != NULL && d->link_in_monitors != NULL; + return d->link_in_monitors != NULL; } static dbus_bool_t -- 2.1.4