This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix setenv() with NULL or "" name or '=' in name argument


On Wed, Jun 09, 2004 at 04:39:04PM +0200, Michael T Kerrisk wrote:
> Gidday,
> 
> The SUSv3 specification for setenv() and unsetenv() stipulates that 

Glibc unsetenv matches XPG6 since early 2001.
The BSD setenv is only supposed to fail if not enough memory
and BSD unsetenv even returned void instead of int.
These functions were added in XPG6, e.g. SuSv2 did not have them at all,
and added this new requirement.

2004-06-09  Jakub Jelinek  <jakub@redhat.com>

	* sysdeps/generic/setenv.c (setenv): Return -1/EINVAL if name is
	NULL, "" or contains '=' character in it.  Reported by
	Michael T Kerrisk <mtk-lists@gmx.net>.
	* stdlib/tst-environ.c: Include errno.h.
	(main): Add tests for these arguments to setenv/unsetenv.

--- libc/sysdeps/generic/setenv.c.jj	2004-02-12 11:53:03.000000000 +0100
+++ libc/sysdeps/generic/setenv.c	2004-06-09 17:05:42.620281837 +0200
@@ -265,6 +265,12 @@ setenv (name, value, replace)
      const char *value;
      int replace;
 {
+  if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+
   return __add_to_environ (name, value, NULL, replace);
 }
 
--- libc/stdlib/tst-environ.c.jj	2001-07-06 06:55:41.000000000 +0200
+++ libc/stdlib/tst-environ.c	2004-06-09 17:11:20.115798246 +0200
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1999, 2004 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -16,6 +16,7 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -173,5 +174,49 @@ main (void)
       result = 1;
     }
 
+  /* Both setenv and unsetenv should return -1/EINVAL for NULL or "" name
+     or if name contains '=' character.  */
+  errno = 0;
+  if (setenv (NULL, "val", 1) >= 0 || errno != EINVAL)
+    {
+      puts ("setenv #4 failed");
+      result = 1;
+    }
+
+  errno = 0;
+  if (setenv ("", "val", 0) >= 0 || errno != EINVAL)
+    {
+      puts ("setenv #5 failed");
+      result = 1;
+    }
+
+  errno = 0;
+  if (setenv ("var=val", "val", 1) >= 0 || errno != EINVAL)
+    {
+      puts ("setenv #6 failed");
+      result = 1;
+    }
+
+  errno = 0;
+  if (unsetenv (NULL) >= 0 || errno != EINVAL)
+    {
+      puts ("unsetenv #1 failed");
+      result = 1;
+    }
+
+  errno = 0;
+  if (unsetenv ("") >= 0 || errno != EINVAL)
+    {
+      puts ("unsetenv #2 failed");
+      result = 1;
+    }
+
+  errno = 0;
+  if (unsetenv ("x=y") >= 0 || errno != EINVAL)
+    {
+      puts ("unsetenv #3 failed");
+      result = 1;
+    }
+
   return result;
 }


	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]