fix signedness of UINT32_MAX and UINT64_MAX at the preprocessor level
[musl] / src / env / setenv.c
index 03e165c..76e8ee1 100644 (file)
@@ -9,7 +9,7 @@ int setenv(const char *var, const char *value, int overwrite)
        char *s;
        int l1, l2;
 
-       if (strchr(var, '=')) {
+       if (!var || !*var || strchr(var, '=')) {
                errno = EINVAL;
                return -1;
        }
@@ -18,14 +18,13 @@ int setenv(const char *var, const char *value, int overwrite)
        l1 = strlen(var);
        l2 = strlen(value);
        s = malloc(l1+l2+2);
-       memcpy(s, var, l1);
-       s[l1] = '=';
-       memcpy(s+l1+1, value, l2);
-       s[l1+l2+1] = 0;
-       if (__putenv(s, 1)) {
-               free(s);
-               errno = ENOMEM;
-               return -1;
+       if (s) {
+               memcpy(s, var, l1);
+               s[l1] = '=';
+               memcpy(s+l1+1, value, l2);
+               s[l1+l2+1] = 0;
+               if (!__putenv(s, 1)) return 0;
        }
-       return 0;
+       free(s);
+       return -1;
 }