From 84d524b82581d010f4736f7b3663aeb9accc25d1 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 16 Sep 2013 13:40:22 +0100 Subject: [PATCH] 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 --- tools/dbus-launch.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tools/dbus-launch.c b/tools/dbus-launch.c index 14fa226..73e0438 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, -- 1.8.4.rc3