From e17f8b6119eb64d126422eda8a18d577cbdc9976 Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Wed, 9 Jun 2004 18:33:36 +0000 Subject: * sysdeps/generic/setenv.c (setenv): Return -1/EINVAL if name is NULL, "" or contains '=' character in it. Reported by Michael T Kerrisk . * stdlib/tst-environ.c: Include errno.h. (main): Add tests for these arguments to setenv/unsetenv. --- ChangeLog | 8 ++++++++ stdlib/tst-environ.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- sysdeps/generic/setenv.c | 6 ++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index f3715f17e3..46fcd8de87 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2004-06-09 Jakub Jelinek + + * sysdeps/generic/setenv.c (setenv): Return -1/EINVAL if name is + NULL, "" or contains '=' character in it. Reported by + Michael T Kerrisk . + * stdlib/tst-environ.c: Include errno.h. + (main): Add tests for these arguments to setenv/unsetenv. + 2004-06-07 Roland McGrath * NEWS: Update bug reporting instructions. Fix some typos. diff --git a/stdlib/tst-environ.c b/stdlib/tst-environ.c index 52c26e8391..6dd9a40527 100644 --- a/stdlib/tst-environ.c +++ b/stdlib/tst-environ.c @@ -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 #include #include #include @@ -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; } diff --git a/sysdeps/generic/setenv.c b/sysdeps/generic/setenv.c index a19771391d..48aaecffe0 100644 --- a/sysdeps/generic/setenv.c +++ b/sysdeps/generic/setenv.c @@ -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); } -- cgit v1.2.3