From 826357d12dbf9e41525d20a600a8e3fb11c6585d Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 7 Oct 2016 17:41:01 +0100 Subject: [PATCH 1/8] Be more const-correct As a general design principle, strings that we aren't going to modify should usually be const. When compiling with -Wwrite-strings, quoted string constants are of type "const char *", causing compiler warnings when they are assigned to char * variables. Unfortunately, we need to add casts in a few places: * _dbus_list_append(), _dbus_test_oom_handling() and similar generic "user-data" APIs take a void *, not a const void *, so we have to cast * For historical reasons the execve() family of functions take a (char * const *), i.e. a constant pointer to an array of mutable strings, so again we have to cast * _dbus_spawn_async_with_babysitter similarly takes a char **, although we can make it a little more const-correct by making it take (char * const *) like execve() does This also incorporates a subsequent patch by Thomas Zimmermann to put various string constants in static storage, which is a little more efficient. Signed-off-by: Simon McVittie Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357 --- bus/config-parser.c | 2 +- bus/desktop-file.c | 4 ++-- bus/driver.c | 5 +++-- bus/test-launch-helper.c | 2 +- bus/test-main.c | 2 +- bus/test-system.c | 2 +- dbus/dbus-connection.c | 6 +++--- dbus/dbus-spawn-test.c | 9 ++++++--- dbus/dbus-spawn-win.c | 12 ++++++------ dbus/dbus-spawn.c | 6 +++--- dbus/dbus-spawn.h | 2 +- dbus/dbus-sysdeps-unix.c | 25 +++++++++++++++---------- dbus/dbus-sysdeps-win.c | 2 +- test/name-test/test-pending-call-dispatch.c | 2 +- test/name-test/test-pending-call-timeout.c | 2 +- test/name-test/test-threads-init.c | 2 +- tools/dbus-launch-win.c | 6 +++--- tools/dbus-launch-x11.c | 2 +- tools/dbus-uuidgen.c | 3 ++- 19 files changed, 53 insertions(+), 43 deletions(-) diff --git a/bus/config-parser.c b/bus/config-parser.c index 5af1024..b776a2d 100644 --- a/bus/config-parser.c +++ b/bus/config-parser.c @@ -2810,7 +2810,7 @@ static dbus_bool_t do_check_own_rules (BusPolicy *policy) { const struct { - char *name; + const char *name; dbus_bool_t allowed; } checks[] = { {"org.freedesktop", FALSE}, diff --git a/bus/desktop-file.c b/bus/desktop-file.c index bfeb72e..4459858 100644 --- a/bus/desktop-file.c +++ b/bus/desktop-file.c @@ -88,7 +88,7 @@ static unsigned char valid[256] = { }; static void report_error (BusDesktopFileParser *parser, - char *message, + const char *message, const char *error_name, DBusError *error); @@ -579,7 +579,7 @@ parse_key_value (BusDesktopFileParser *parser, DBusError *error) static void report_error (BusDesktopFileParser *parser, - char *message, + const char *message, const char *error_name, DBusError *error) { diff --git a/bus/driver.c b/bus/driver.c index 41ca445..83e8ead 100644 --- a/bus/driver.c +++ b/bus/driver.c @@ -1528,6 +1528,8 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection, DBusMessage *message, DBusError *error) { + static const char dbus_service_name[] = DBUS_SERVICE_DBUS; + const char *text; DBusList *base_names; DBusList *link; @@ -1536,7 +1538,6 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection, BusService *service; DBusMessage *reply; DBusMessageIter iter, array_iter; - char *dbus_service_name = DBUS_SERVICE_DBUS; _DBUS_ASSERT_ERROR_IS_CLEAR (error); @@ -1557,7 +1558,7 @@ bus_driver_handle_list_queued_owners (DBusConnection *connection, _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS)) { /* ORG_FREEDESKTOP_DBUS owns itself */ - if (! _dbus_list_append (&base_names, dbus_service_name)) + if (! _dbus_list_append (&base_names, (char *) dbus_service_name)) goto oom; } else if (service == NULL) diff --git a/bus/test-launch-helper.c b/bus/test-launch-helper.c index e9ec7b9..5d025f5 100644 --- a/bus/test-launch-helper.c +++ b/bus/test-launch-helper.c @@ -130,7 +130,7 @@ main (int argc, char **argv) if (!_dbus_test_oom_handling ("dbus-daemon-launch-helper", bus_activation_helper_oom_test, - "org.freedesktop.DBus.TestSuiteEchoService")) + (char *) "org.freedesktop.DBus.TestSuiteEchoService")) die ("OOM failed"); test_post_hook (argv[0]); diff --git a/bus/test-main.c b/bus/test-main.c index e4ba0ad..9496dd2 100644 --- a/bus/test-main.c +++ b/bus/test-main.c @@ -74,7 +74,7 @@ test_pre_hook (void) initial_fds = _dbus_check_fdleaks_enter (); } -static char *progname = ""; +static const char *progname = ""; static void test_post_hook (void) diff --git a/bus/test-system.c b/bus/test-system.c index e03e677..7d5c3df 100644 --- a/bus/test-system.c +++ b/bus/test-system.c @@ -59,7 +59,7 @@ test_pre_hook (void) { } -static char *progname = ""; +static const char *progname = ""; static void test_post_hook (void) { diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index 365fd45..319987e 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -2183,9 +2183,9 @@ _dbus_memory_pause_based_on_timeout (int timeout_milliseconds) } static DBusMessage * -generate_local_error_message (dbus_uint32_t serial, - char *error_name, - char *error_msg) +generate_local_error_message (dbus_uint32_t serial, + const char *error_name, + const char *error_msg) { DBusMessage *message; message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR); diff --git a/dbus/dbus-spawn-test.c b/dbus/dbus-spawn-test.c index 5cb2cf4..bffb33e 100644 --- a/dbus/dbus-spawn-test.c +++ b/dbus/dbus-spawn-test.c @@ -56,14 +56,17 @@ get_test_exec (const char *exe, static dbus_bool_t check_spawn_nonexistent (void *data) { - char *argv[4] = { NULL, NULL, NULL, NULL }; + static const char arg_does_not_exist[] = "/this/does/not/exist/32542sdgafgafdg"; + + const char *argv[4] = { NULL, NULL, NULL, NULL }; DBusBabysitter *sitter = NULL; DBusError error = DBUS_ERROR_INIT; /*** Test launching nonexistent binary */ - argv[0] = "/this/does/not/exist/32542sdgafgafdg"; - if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_nonexistent", argv, + argv[0] = arg_does_not_exist; + if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_nonexistent", + (char * const *) argv, NULL, DBUS_SPAWN_NONE, NULL, NULL, &error)) { diff --git a/dbus/dbus-spawn-win.c b/dbus/dbus-spawn-win.c index 564f61d..cc5bfc3 100644 --- a/dbus/dbus-spawn-win.c +++ b/dbus/dbus-spawn-win.c @@ -412,8 +412,8 @@ handle_watch (DBusWatch *watch, /* protect_argv lifted from GLib, relicensed by author, Tor Lillqvist */ static int -protect_argv (char **argv, - char ***new_argv) +protect_argv (char * const *argv, + char ***new_argv) { int i; int argc = 0; @@ -440,7 +440,7 @@ protect_argv (char **argv, */ for (i = 0; i < argc; i++) { - char *p = argv[i]; + const char *p = argv[i]; char *q; int len = 0; int need_dblquotes = FALSE; @@ -452,7 +452,7 @@ protect_argv (char **argv, len++; else if (*p == '\\') { - char *pp = p; + const char *pp = p; while (*pp && *pp == '\\') pp++; if (*pp == '"') @@ -479,7 +479,7 @@ protect_argv (char **argv, *q++ = '\\'; else if (*p == '\\') { - char *pp = p; + const char *pp = p; while (*pp && *pp == '\\') pp++; if (*pp == '"') @@ -643,7 +643,7 @@ babysitter (void *parameter) dbus_bool_t _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, const char *log_name, - char **argv, + char * const *argv, char **envp, DBusSpawnFlags flags _DBUS_GNUC_UNUSED, DBusSpawnChildSetupFunc child_setup _DBUS_GNUC_UNUSED, diff --git a/dbus/dbus-spawn.c b/dbus/dbus-spawn.c index 04245b4..df6eafd 100644 --- a/dbus/dbus-spawn.c +++ b/dbus/dbus-spawn.c @@ -1004,8 +1004,8 @@ write_status_and_exit (int fd, int status) static void do_exec (int child_err_report_fd, - char **argv, - char **envp, + char * const *argv, + char * const *envp, DBusSpawnChildSetupFunc child_setup, void *user_data) { @@ -1214,7 +1214,7 @@ babysit (pid_t grandchild_pid, dbus_bool_t _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, const char *log_name, - char **argv, + char * const *argv, char **env, DBusSpawnFlags flags, DBusSpawnChildSetupFunc child_setup, diff --git a/dbus/dbus-spawn.h b/dbus/dbus-spawn.h index dc04dc7..b34324d 100644 --- a/dbus/dbus-spawn.h +++ b/dbus/dbus-spawn.h @@ -45,7 +45,7 @@ typedef enum { dbus_bool_t _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, const char *log_name, - char **argv, + char * const *argv, char **env, DBusSpawnFlags flags, DBusSpawnChildSetupFunc child_setup, diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 5d9062f..fda5d8e 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -1069,7 +1069,7 @@ _dbus_connect_exec (const char *path, _dbus_close_all (); - execvp (path, argv); + execvp (path, (char * const *) argv); fprintf (stderr, "Failed to execute process %s: %s\n", path, _dbus_strerror (errno)); @@ -3545,7 +3545,7 @@ _dbus_get_tmpdir(void) static dbus_bool_t _read_subprocess_line_argv (const char *progpath, dbus_bool_t path_fallback, - char * const *argv, + const char * const *argv, DBusString *result, DBusError *error) { @@ -3648,7 +3648,7 @@ _read_subprocess_line_argv (const char *progpath, /* If it looks fully-qualified, try execv first */ if (progpath[0] == '/') { - execv (progpath, argv); + execv (progpath, (char * const *) argv); /* Ok, that failed. Now if path_fallback is given, let's * try unqualified. This is mostly a hack to work * around systems which ship dbus-launch in /usr/bin @@ -3657,10 +3657,10 @@ _read_subprocess_line_argv (const char *progpath, */ if (path_fallback) /* We must have a slash, because we checked above */ - execvp (strrchr (progpath, '/')+1, argv); + execvp (strrchr (progpath, '/')+1, (char * const *) argv); } else - execvp (progpath, argv); + execvp (progpath, (char * const *) argv); /* still nothing, we failed */ _exit (1); @@ -3758,12 +3758,17 @@ _dbus_get_autolaunch_address (const char *scope, DBusError *error) { #ifdef DBUS_ENABLE_X11_AUTOLAUNCH + static const char arg_dbus_launch[] = "dbus-launch"; + static const char arg_autolaunch[] = "--autolaunch"; + static const char arg_binary_syntax[] = "--binary-syntax"; + static const char arg_close_stderr[] = "--close-stderr"; + /* Perform X11-based autolaunch. (We also support launchd-based autolaunch, * but that's done elsewhere, and if it worked, this function wouldn't * be called.) */ const char *display; const char *progpath; - char *argv[6]; + const char *argv[6]; int i; DBusString uuid; dbus_bool_t retval; @@ -3818,15 +3823,15 @@ _dbus_get_autolaunch_address (const char *scope, * see fd.o#69716 */ i = 0; - argv[i] = "dbus-launch"; + argv[i] = arg_dbus_launch; ++i; - argv[i] = "--autolaunch"; + argv[i] = arg_autolaunch; ++i; argv[i] = _dbus_string_get_data (&uuid); ++i; - argv[i] = "--binary-syntax"; + argv[i] = arg_binary_syntax; ++i; - argv[i] = "--close-stderr"; + argv[i] = arg_close_stderr; ++i; argv[i] = NULL; ++i; diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 62e7023..c68c6ca 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -3678,7 +3678,7 @@ _dbus_logv (DBusSystemLogSeverity severity, const char *msg, va_list args) { - char *s = ""; + const char *s = ""; va_list tmp; switch(severity) diff --git a/test/name-test/test-pending-call-dispatch.c b/test/name-test/test-pending-call-dispatch.c index 7785564..c8462d0 100644 --- a/test/name-test/test-pending-call-dispatch.c +++ b/test/name-test/test-pending-call-dispatch.c @@ -20,7 +20,7 @@ _run_iteration (DBusConnection *conn) DBusPendingCall *dbus_pending; DBusMessage *method; DBusMessage *reply; - char *echo = "echo"; + const char *echo = "echo"; /* send the first message */ method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService", diff --git a/test/name-test/test-pending-call-timeout.c b/test/name-test/test-pending-call-timeout.c index be6b849..6035df8 100644 --- a/test/name-test/test-pending-call-timeout.c +++ b/test/name-test/test-pending-call-timeout.c @@ -17,7 +17,7 @@ _method_call (DBusConnection *conn, DBusPendingCall *pending; DBusMessage *method; DBusMessage *reply; - char *echo = "echo"; + const char *echo = "echo"; /* send the message */ method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService", diff --git a/test/name-test/test-threads-init.c b/test/name-test/test-threads-init.c index 6285715..eb0ff96 100644 --- a/test/name-test/test-threads-init.c +++ b/test/name-test/test-threads-init.c @@ -18,7 +18,7 @@ _run_iteration (DBusConnection *conn) DBusPendingCall *dbus_pending; DBusMessage *method; DBusMessage *reply; - char *echo = "echo"; + const char *echo = "echo"; /* send the first message */ method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService", diff --git a/tools/dbus-launch-win.c b/tools/dbus-launch-win.c index 1dfd095..34837c1 100644 --- a/tools/dbus-launch-win.c +++ b/tools/dbus-launch-win.c @@ -38,7 +38,7 @@ #define wcscpy_s my_wcscpy_s static errno_t -wcscat_s (wchar_t *dest, size_t size, wchar_t *src) +wcscat_s (wchar_t *dest, size_t size, const wchar_t *src) { assert (sizeof (wchar_t) * (wcslen (dest) + wcslen (src) + 1) <= size); wcscat (dest, src); @@ -47,7 +47,7 @@ wcscat_s (wchar_t *dest, size_t size, wchar_t *src) static errno_t -wcscpy_s (wchar_t *dest, size_t size, wchar_t *src) +wcscpy_s (wchar_t *dest, size_t size, const wchar_t *src) { assert (sizeof (wchar_t) * (wcslen (src) + 1) <= size); wcscpy (dest, src); @@ -87,7 +87,7 @@ main (int argc, char **argv) wchar_t dbusDaemonPath[MAX_PATH * 2 + 1]; wchar_t command[MAX_PATH * 2 + 1]; wchar_t *p; - wchar_t *daemon_name; + const wchar_t *daemon_name; int result; #ifdef DBUS_WINCE diff --git a/tools/dbus-launch-x11.c b/tools/dbus-launch-x11.c index a09444b..d22ef5d 100644 --- a/tools/dbus-launch-x11.c +++ b/tools/dbus-launch-x11.c @@ -52,7 +52,7 @@ x_io_error_handler (Display *xdisplay) static void remove_prefix (char *s, - char *prefix) + const char *prefix) { int plen; diff --git a/tools/dbus-uuidgen.c b/tools/dbus-uuidgen.c index 03ce553..744bbeb 100644 --- a/tools/dbus-uuidgen.c +++ b/tools/dbus-uuidgen.c @@ -28,7 +28,8 @@ #include static void -usage (char *name, int ecode) +usage (const char *name, + int ecode) { if (name == NULL) name = "dbus-uuidgen"; -- 2.9.3