Error on setenv(..., NULL, ...)
diff mbox

Message ID CALoOobNG+bKAGwYtOW5piO2+DSS4mhwON=3ud1ud4Xt9X6oUbw@mail.gmail.com
State New
Headers show

Commit Message

Paul Pluzhnikov March 11, 2015, 4:28 p.m. UTC
On Wed, Mar 11, 2015 at 9:11 AM, Paul Pluzhnikov <ppluzhnikov@google.com> wrote:

> Attached trivial patch makes setenv(..., NULL, ...) fail instead of
> producing "bad" environment. Tested on Linux/x86_64, no new failures.

Now with added test :-)


2015-03-11  Paul Pluzhnikov  <ppluzhnikov@google.com>

        * stdlib/setenv.c (setenv): Reject NULL value in setenv.
        * stdlib/tst-environ.c (do_test): Add test case.

Patch
diff mbox

diff --git a/stdlib/setenv.c b/stdlib/setenv.c
index b60c4f0..63a95cf 100644
--- a/stdlib/setenv.c
+++ b/stdlib/setenv.c
@@ -240,7 +240,8 @@  setenv (name, value, replace)
      const char *value;
      int replace;
 {
-  if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
+  if (name == NULL || *name == '\0' || strchr (name, '=') != NULL
+      || value == NULL)
     {
       __set_errno (EINVAL);
       return -1;
diff --git a/stdlib/tst-environ.c b/stdlib/tst-environ.c
index 9871bd7..d3137cb 100644
--- a/stdlib/tst-environ.c
+++ b/stdlib/tst-environ.c
@@ -197,6 +197,13 @@  do_test (void)
     }
 
   errno = 0;
+  if (setenv (VAR, NULL, 1) >= 0 || errno != EINVAL)
+    {
+      puts ("setenv #7 failed");
+      result = 1;
+    }
+
+  errno = 0;
   if (unsetenv (NULL) >= 0 || errno != EINVAL)
     {
       puts ("unsetenv #1 failed");