From 6326ab374ede4fff1e8019a50d4b9854b8db7625 Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Fri, 22 Jan 2016 20:13:35 +0000 Subject: Fix script environment variable setting for empty lists When setting an environment variable as a space-separated list, and the list is empty, we must not delete the '=' before the value. In practice putenv() is likely to discard the invalid string, leaving the variable unset, but this is not guaranteed. Signed-off-by: Ben Hutchings --- src/script.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/script.c b/src/script.c index 3579331..1533510 100644 --- a/src/script.c +++ b/src/script.c @@ -105,7 +105,9 @@ static void ipv6_to_env(const char *name, buf_len += strlen(&buf[buf_len]); buf[buf_len++] = ' '; } - buf[buf_len - 1] = '\0'; + if (buf[buf_len - 1] == ' ') + buf_len--; + buf[buf_len] = '\0'; putenv(buf); } @@ -126,7 +128,9 @@ static void fqdn_to_env(const char *name, const uint8_t *fqdn, size_t len) buf_len += strlen(&buf[buf_len]); buf[buf_len++] = ' '; } - buf[buf_len - 1] = '\0'; + if (buf[buf_len - 1] == ' ') + buf_len--; + buf[buf_len] = '\0'; putenv(buf); } @@ -201,7 +205,9 @@ static void entry_to_env(const char *name, const void *data, size_t len, enum en buf[buf_len++] = ' '; } - buf[buf_len - 1] = '\0'; + if (buf[buf_len - 1] == ' ') + buf_len--; + buf[buf_len] = '\0'; putenv(buf); } @@ -220,7 +226,9 @@ static void search_to_env(const char *name, const uint8_t *start, size_t len) *c++ = ' '; } - c[-1] = '\0'; + if (c[-1] == ' ') + c--; + *c = '\0'; putenv(buf); } -- cgit v1.2.1 From d277ddb78abd034eb0e1c088ac969cdeef10d9e2 Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Fri, 22 Jan 2016 19:34:41 +0000 Subject: Fix iteration after expiring a list entry After removing an entry, the next entry will be at the same offset as the entry we just removed. Also the total length will have changed. Update the length when we remove an entry, and advance the offset only when we don't. Signed-off-by: Ben Hutchings --- src/odhcp6c.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/odhcp6c.c b/src/odhcp6c.c index 1714d62..59f6390 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -619,7 +619,7 @@ static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed) uint8_t *start = odhcp6c_get_state(state, &len); for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start; (uint8_t*)c < &start[len] && &c->auxtarget[c->auxlen] <= &start[len]; - c = (struct odhcp6c_entry*)(&c->auxtarget[c->auxlen])) { + ) { if (c->t1 < elapsed) c->t1 = 0; else if (c->t1 != UINT32_MAX) @@ -640,8 +640,12 @@ static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed) else if (c->valid != UINT32_MAX) c->valid -= elapsed; - if (!c->valid) + if (!c->valid) { odhcp6c_remove_state(state, ((uint8_t*)c) - start, sizeof(*c) + c->auxlen); + start = odhcp6c_get_state(state, &len); + } else { + c = (struct odhcp6c_entry*)(&c->auxtarget[c->auxlen]); + } } } -- cgit v1.2.1