Commit 8db5ca90 authored by Simon McVittie's avatar Simon McVittie Committed by Simon McVittie

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: default avatarSimon McVittie <smcv@debian.org>
Reviewed-by: default avatarThomas Zimmermann <tdz@users.sourceforge.net>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357
parent cc7df2fb
......@@ -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},
......
......@@ -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)
{
......
......@@ -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)
......
......@@ -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]);
......
......@@ -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)
......
......@@ -59,7 +59,7 @@ test_pre_hook (void)
{
}
static char *progname = "";
static const char *progname = "";
static void
test_post_hook (void)
{
......
......@@ -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);
......
......@@ -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))
{
......
......@@ -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,
......
......@@ -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,
......
......@@ -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,
......
......@@ -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;
......
......@@ -3678,7 +3678,7 @@ _dbus_logv (DBusSystemLogSeverity severity,
const char *msg,
va_list args)
{
char *s = "";
const char *s = "";
va_list tmp;
switch(severity)
......
......@@ -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",
......
......@@ -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",
......
......@@ -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",
......
......@@ -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
......
......@@ -52,7 +52,7 @@ x_io_error_handler (Display *local_xdisplay)
static void
remove_prefix (char *s,
char *prefix)
const char *prefix)
{
int plen;
......
......@@ -28,7 +28,8 @@
#include <dbus/dbus.h>
static void
usage (char *name, int ecode)
usage (const char *name,
int ecode)
{
if (name == NULL)
name = "dbus-uuidgen";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment