]> git.decadent.org.uk Git - odhcp6c.git/commitdiff
Fix script environment variable setting for empty lists
authorBen Hutchings <ben@decadent.org.uk>
Fri, 22 Jan 2016 20:13:35 +0000 (20:13 +0000)
committerBen Hutchings <ben@decadent.org.uk>
Thu, 28 Jan 2016 15:34:45 +0000 (15:34 +0000)
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 <ben@decadent.org.uk>
src/script.c

index 357933105d35d56f5718fbad56b30595dbe80557..1533510dbab56c8d8394298ecf48c844b9255380 100644 (file)
@@ -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);
 }