From 3bbbd15c65c3ebded14f558be93e760c38c52e1b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Fri, 27 Aug 2010 22:16:50 -0400 Subject: [PATCH] Marshall NULL strings without crashing --- connection.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/connection.c b/connection.c index 71e39e6..cd9a45d 100644 --- a/connection.c +++ b/connection.c @@ -338,7 +338,7 @@ wl_connection_vmarshal(struct wl_connection *connection, break; case 's': s = va_arg(ap, const char *); - length = strlen(s); + length = s ? strlen(s) : 0; *p++ = length; memcpy(p, s, length); p += DIV_ROUNDUP(length, sizeof(*p)); @@ -456,14 +456,18 @@ wl_connection_demarshal(struct wl_connection *connection, goto out; } - values[i].string = malloc(length + 1); - if (values[i].string == NULL) { - errno = ENOMEM; - ret = -1; - goto out; + if (length == 0) { + values[i].string = NULL; + } else { + values[i].string = malloc(length + 1); + if (values[i].string == NULL) { + errno = ENOMEM; + ret = -1; + goto out; + } + memcpy(values[i].string, p, length); + values[i].string[length] = '\0'; } - memcpy(values[i].string, p, length); - values[i].string[length] = '\0'; p = next; break; case 'o': -- 2.7.4