SVN.pm::parse_svn_date: allow timestamps with a single-digit hour
[gitweb.git] / compat / setenv.c
index 94acd2da9e2e95f9f9a72061d9575de6079dcaec..fc1439a6439393ff5224e98ab126607ceaa4994d 100644 (file)
@@ -1,5 +1,4 @@
-#include <stdlib.h>
-#include <string.h>
+#include "../git-compat-util.h"
 
 int gitsetenv(const char *name, const char *value, int replace)
 {
@@ -7,7 +6,10 @@ int gitsetenv(const char *name, const char *value, int replace)
        size_t namelen, valuelen;
        char *envstr;
 
-       if (!name || !value) return -1;
+       if (!name || strchr(name, '=') || !value) {
+               errno = EINVAL;
+               return -1;
+       }
        if (!replace) {
                char *oldval = NULL;
                oldval = getenv(name);
@@ -16,8 +18,11 @@ int gitsetenv(const char *name, const char *value, int replace)
 
        namelen = strlen(name);
        valuelen = strlen(value);
-       envstr = malloc((namelen + valuelen + 2) * sizeof(char));
-       if (!envstr) return -1;
+       envstr = malloc((namelen + valuelen + 2));
+       if (!envstr) {
+               errno = ENOMEM;
+               return -1;
+       }
 
        memcpy(envstr, name, namelen);
        envstr[namelen] = '=';
@@ -25,7 +30,11 @@ int gitsetenv(const char *name, const char *value, int replace)
        envstr[namelen + valuelen + 1] = 0;
 
        out = putenv(envstr);
+       /* putenv(3) makes the argument string part of the environment,
+        * and changing that string modifies the environment --- which
+        * means we do not own that storage anymore.  Do not free
+        * envstr.
+        */
 
-       free(envstr);
        return out;
 }