diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2013-09-16 13:40:22 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2013-10-08 11:06:18 +0100 |
commit | 580ed1c8fb3cbfb4a8071a7db8a802f14df3890b (patch) | |
tree | fdf1b679ba55f77c6e01eb8370d80afab0dbe107 /tools/dbus-launch.c | |
parent | fd5271f83996956ee326441f450ffd5e5e8e91d0 (diff) | |
download | dbus-580ed1c8fb3cbfb4a8071a7db8a802f14df3890b.tar.gz |
dbus-launch: avoid asprintf(), and die gracefully on out-of-memory
asprintf() is a GNU extension (non-portable).
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=37849
Reviewed-by: Chengwei Yang <chengwei.yang@intel.com>
Diffstat (limited to 'tools/dbus-launch.c')
-rw-r--r-- | tools/dbus-launch.c | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/tools/dbus-launch.c b/tools/dbus-launch.c index b071fcc5..3899d6d6 100644 --- a/tools/dbus-launch.c +++ b/tools/dbus-launch.c @@ -222,6 +222,26 @@ xstrdup (const char *str) return copy; } +static char * +concat2 (const char *a, + const char *b) +{ + size_t la, lb; + char *ret; + + la = strlen (a); + lb = strlen (b); + + ret = malloc (la + lb + 1); + + if (ret == NULL) + return NULL; + + memcpy (ret, a, la); + memcpy (ret + la, b, lb + 1); + return ret; +} + typedef enum { READ_STATUS_OK, /**< Read succeeded */ @@ -1114,11 +1134,15 @@ main (int argc, char **argv) { if (config_file == NULL && getenv ("DBUS_TEST_DATA") != NULL) { - ret = asprintf (&config_file, "%s/valid-config-files/session.conf", - getenv ("DBUS_TEST_DATA")); + config_file = concat2 (getenv ("DBUS_TEST_DATA"), + "/valid-config-files/session.conf"); + + if (config_file == NULL) + { + fprintf (stderr, "Out of memory\n"); + exit (1); + } } - if (ret == -1 && config_file != NULL) - free (config_file); execl (TEST_BUS_BINARY, TEST_BUS_BINARY, |