From 3c3729f81c3a81cba9c376573aeadc3bff32255f Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 18 Jun 2020 14:14:00 -0400 Subject: [PATCH] TT#28300 fix missing strdup for multi homed interfaces Change-Id: Ifaac7f85a5ab3515d95e72b6ee640c9e865cf219 --- daemon/main.c | 6 +++--- lib/str.h | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/daemon/main.c b/daemon/main.c index 7c7ffbdfe..71646cf8d 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -161,11 +161,11 @@ static int if_addr_parse(GQueue *q, char *s, struct ifaddrs *ifas) { c = strchr(s, '/'); if (c) { *c++ = 0; - str_init_dup(&name, s); + str_init(&name, s); s = c; } else - str_init_dup(&name, "default"); + str_init(&name, "default"); /* advertised address */ c = strchr(s, '!'); @@ -229,7 +229,7 @@ static int if_addr_parse(GQueue *q, char *s, struct ifaddrs *ifas) { while ((addr = g_queue_pop_head(&addrs))) { ifa = g_slice_alloc0(sizeof(*ifa)); - ifa->name = name; + str_init_dup_str(&ifa->name, &name); ifa->local_address.addr = *addr; ifa->local_address.type = socktype_udp; ifa->advertised_address.addr = adv; diff --git a/lib/str.h b/lib/str.h index 7b1e263cf..dd1f47709 100644 --- a/lib/str.h +++ b/lib/str.h @@ -62,6 +62,7 @@ INLINE str *str_init_len_assert_len(str *out, char *s, int buflen, int len); #define str_init_len_assert(out, s, len) str_init_len_assert_len(out, s, sizeof(s), len) /* inits a str object from a regular string and duplicates the contents. returns out */ INLINE str *str_init_dup(str *out, char *s); +INLINE str *str_init_dup_str(str *out, const str *s); INLINE void str_free_dup(str *out); /* returns new str object with uninitialized buffer large enough to hold `len` characters (+1 for null byte) */ INLINE str *str_alloc(int len); @@ -236,6 +237,16 @@ INLINE str *str_init_dup(str *out, char *s) { out->len = s ? strlen(s) : 0; return out; } +INLINE str *str_init_dup_str(str *out, const str *s) { + if (!s || !s->len) { + *out = STR_NULL; + return out; + } + out->s = malloc(s->len + 1); + memcpy(out->s, s->s, s->len); + out->s[s->len] = '\0'; + return out; +} INLINE void str_free_dup(str *out) { if (!out) return ;