From e772bf748f68ab198170eeff0f6fc7b06d326388 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 17 Jan 2017 20:41:17 +0000 Subject: [PATCH 05/16] test, tools: assert impossible values of local enums are not reached Based on part of a patch from Thomas Zimmermann. Signed-off-by: Simon McVittie --- dbus/dbus-marshal-recursive-util.c | 3 +++ dbus/dbus-sha.c | 7 ++++++- test/test-utils-glib.c | 2 ++ tools/dbus-cleanup-sockets.c | 1 + tools/dbus-launch.c | 6 ++++++ tools/dbus-monitor.c | 10 ++++++++-- tools/dbus-run-session.c | 4 ++++ 7 files changed, 30 insertions(+), 3 deletions(-) diff --git a/dbus/dbus-marshal-recursive-util.c b/dbus/dbus-marshal-recursive-util.c index d43972fd..b30f5883 100644 --- a/dbus/dbus-marshal-recursive-util.c +++ b/dbus/dbus-marshal-recursive-util.c @@ -2390,6 +2390,9 @@ string_from_seed (char *buf, */ switch (seed % 3) { + default: + /* don't alter it */ + break; case 1: len += 2; break; diff --git a/dbus/dbus-sha.c b/dbus/dbus-sha.c index 343b5d49..52c74f54 100644 --- a/dbus/dbus-sha.c +++ b/dbus/dbus-sha.c @@ -693,7 +693,9 @@ get_next_expected_result (DBusString *results, i = 0; while (i < _dbus_string_get_length (result)) { - switch (_dbus_string_get_byte (result, i)) + unsigned char c = _dbus_string_get_byte (result, i); + + switch (c) { case 'A': _dbus_string_set_byte (result, i, 'a'); @@ -718,6 +720,9 @@ get_next_expected_result (DBusString *results, _dbus_string_delete (result, i, 1); --i; /* to offset ++i below */ break; + default: + if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) + _dbus_assert_not_reached ("invalid SHA-1 test script"); } ++i; diff --git a/test/test-utils-glib.c b/test/test-utils-glib.c index e13d20d3..004fafd2 100644 --- a/test/test-utils-glib.c +++ b/test/test-utils-glib.c @@ -151,6 +151,8 @@ spawn_dbus_daemon (const gchar *binary, break; + case TEST_USER_ME: + /* cannot get here, fall through */ default: g_assert_not_reached (); } diff --git a/tools/dbus-cleanup-sockets.c b/tools/dbus-cleanup-sockets.c index 1b6709af..994e57ed 100644 --- a/tools/dbus-cleanup-sockets.c +++ b/tools/dbus-cleanup-sockets.c @@ -328,6 +328,7 @@ handle_sockets (SocketEntry **entries, break; case SOCKET_UNLINKED: + default: fprintf (stderr, "Bad status from open_socket(), should not happen\n"); exit (1); break; diff --git a/tools/dbus-launch.c b/tools/dbus-launch.c index 65287af6..75f39cf0 100644 --- a/tools/dbus-launch.c +++ b/tools/dbus-launch.c @@ -709,6 +709,8 @@ babysit (int exit_with_session, strerror (errno)); exit (1); break; + default: + _dbus_assert_not_reached ("Invalid read result"); } verbose ("Got PID %ld from daemon\n", @@ -1328,6 +1330,8 @@ main (int argc, char **argv) strerror (errno)); exit (1); break; + default: + _dbus_assert_not_reached ("Invalid read result"); } close (bus_address_to_launcher_pipe[READ_END]); @@ -1347,6 +1351,8 @@ main (int argc, char **argv) strerror (errno)); exit (1); break; + default: + _dbus_assert_not_reached ("Invalid read result"); } end = NULL; diff --git a/tools/dbus-monitor.c b/tools/dbus-monitor.c index aa28447f..4edee29e 100644 --- a/tools/dbus-monitor.c +++ b/tools/dbus-monitor.c @@ -21,7 +21,7 @@ #include -#include "dbus/dbus-internals.h" /* just for the macros */ +#include "dbus/dbus-internals.h" #include #include @@ -240,6 +240,10 @@ binary_filter_func (DBusConnection *connection, } break; + case BINARY_MODE_NOT: + _dbus_assert_not_reached ("wrong filter function"); + break; + case BINARY_MODE_RAW: default: /* nothing special, just the raw message stream */ @@ -474,8 +478,10 @@ main (int argc, char *argv[]) case DBUS_BUS_SESSION: where = "session bus"; break; + case DBUS_BUS_STARTER: default: - where = ""; + /* We don't set type to anything else */ + _dbus_assert_not_reached ("impossible bus type"); } } fprintf (stderr, "Failed to open connection to %s: %s\n", diff --git a/tools/dbus-run-session.c b/tools/dbus-run-session.c index 135f5d21..0adb5ad1 100644 --- a/tools/dbus-run-session.c +++ b/tools/dbus-run-session.c @@ -37,6 +37,7 @@ #include #include "dbus/dbus.h" +#include "dbus/dbus-internals.h" #define MAX_ADDR_LEN 512 #define PIPE_READ_END 0 @@ -386,6 +387,9 @@ main (int argc, char **argv) me, strerror (errno)); return 127; break; + + default: + _dbus_assert_not_reached ("invalid read result"); } close (bus_address_pipe[PIPE_READ_END]); -- 2.11.0