Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Make setenv(3) follow the standard, by rejecting invalid str...



details:   https://anonhg.NetBSD.org/src/rev/d49737abe42a
branches:  trunk
changeset: 758066:d49737abe42a
user:      njoly <njoly%NetBSD.org@localhost>
date:      Sat Oct 16 11:23:41 2010 +0000

description:
Make setenv(3) follow the standard, by rejecting invalid strings. It
now fails with EINVAL errno when variable is NULL, empty or contains
an `=' character; or value is NULL.

Adjust the man page accordingly, and exercize them in the existing
environment testcase.

diffstat:

 lib/libc/stdlib/getenv.3              |  27 ++++++++++++++++-----------
 lib/libc/stdlib/setenv.c              |  21 +++++++++++++--------
 tests/lib/libc/stdlib/t_environment.c |  13 +++++++++++--
 3 files changed, 40 insertions(+), 21 deletions(-)

diffs (159 lines):

diff -r 468f35e7e5ef -r d49737abe42a lib/libc/stdlib/getenv.3
--- a/lib/libc/stdlib/getenv.3  Sat Oct 16 11:13:52 2010 +0000
+++ b/lib/libc/stdlib/getenv.3  Sat Oct 16 11:23:41 2010 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: getenv.3,v 1.22 2010/10/01 20:57:50 wiz Exp $
+.\"    $NetBSD: getenv.3,v 1.23 2010/10/16 11:23:41 njoly Exp $
 .\"
 .\" Copyright (c) 1988, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -33,7 +33,7 @@
 .\"
 .\"     from: @(#)getenv.3     8.2 (Berkeley) 12/11/93
 .\"
-.Dd October 1, 2010
+.Dd October 16, 2010
 .Dt GETENV 3
 .Os
 .Sh NAME
@@ -62,16 +62,14 @@
 host
 .Em environment list .
 For compatibility with differing environment conventions,
-the given arguments
+the
+.Fn getenv
+or
+.Fn getenv_r
+given argument
 .Ar name
-and
-.Ar value
-may be appended and prepended,
-respectively,
-with an equal sign
-.Dq Li \&= ,
-except for
-.Fn unsetenv .
+may be appended with an equal sign
+.Dq Li \&= .
 .Pp
 The
 .Fn getenv
@@ -160,11 +158,18 @@
 The
 .Fa name
 argument to
+.Fn setenv
+or
 .Fn unsetenv
 is a null pointer, points to an empty string, or points to a string
 containing an
 .Dq Li \&=
 character.
+The
+.Fa value
+argument to
+.Fn setenv
+is a null pointer.
 .It Bq Er ENOMEM
 The function
 .Fn setenv
diff -r 468f35e7e5ef -r d49737abe42a lib/libc/stdlib/setenv.c
--- a/lib/libc/stdlib/setenv.c  Sat Oct 16 11:13:52 2010 +0000
+++ b/lib/libc/stdlib/setenv.c  Sat Oct 16 11:23:41 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: setenv.c,v 1.40 2010/10/02 16:56:03 tron Exp $ */
+/*     $NetBSD: setenv.c,v 1.41 2010/10/16 11:23:41 njoly Exp $        */
 
 /*
  * Copyright (c) 1987, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)setenv.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: setenv.c,v 1.40 2010/10/02 16:56:03 tron Exp $");
+__RCSID("$NetBSD: setenv.c,v 1.41 2010/10/16 11:23:41 njoly Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -60,13 +60,23 @@
 setenv(const char *name, const char *value, int rewrite)
 {
        char *c;
-       const char *cc;
        size_t l_value, size;
        int offset;
 
        _DIAGASSERT(name != NULL);
        _DIAGASSERT(value != NULL);
 
+       if (name == NULL || value == NULL) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       size = strcspn(name, "=");
+       if (size == 0 || name[size] != '\0') {
+               errno = EINVAL;
+               return -1;
+       }
+
        if (rwlock_wrlock(&__environ_lock) != 0)
                return -1;
 
@@ -76,8 +86,6 @@
        if (__allocenv(offset) == -1)
                goto bad;
 
-       if (*value == '=')                      /* no `=' in value */
-               ++value;
        l_value = strlen(value);
 
        if (c != NULL) {
@@ -93,9 +101,6 @@
                        goto copy;
                }
        }
-       for (cc = name; *cc && *cc != '='; ++cc)        /* no `=' in name */
-               continue;
-       size = cc - name;
        /* name + `=' + value */
        if ((c = malloc(size + l_value + 2)) == NULL)
                goto bad;
diff -r 468f35e7e5ef -r d49737abe42a tests/lib/libc/stdlib/t_environment.c
--- a/tests/lib/libc/stdlib/t_environment.c     Sat Oct 16 11:13:52 2010 +0000
+++ b/tests/lib/libc/stdlib/t_environment.c     Sat Oct 16 11:23:41 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_environment.c,v 1.2 2010/10/01 20:12:20 christos Exp $       */
+/*     $NetBSD: t_environment.c,v 1.3 2010/10/16 11:23:41 njoly Exp $  */
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -32,9 +32,10 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_environment.c,v 1.2 2010/10/01 20:12:20 christos Exp $");
+__RCSID("$NetBSD: t_environment.c,v 1.3 2010/10/16 11:23:41 njoly Exp $");
 
 #include <atf-c.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -71,6 +72,14 @@
                unsetenv(name);
                ATF_CHECK(getenv(name) == NULL);
        }
+
+       ATF_CHECK_ERRNO(EINVAL, setenv(NULL, "val", 1) == -1);
+       ATF_CHECK_ERRNO(EINVAL, setenv("", "val", 1) == -1);
+       ATF_CHECK_ERRNO(EINVAL, setenv("v=r", "val", 1) == -1);
+       ATF_CHECK_ERRNO(EINVAL, setenv("var", NULL, 1) == -1);
+
+       ATF_CHECK(setenv("var", "=val", 1) == 0);
+       ATF_CHECK_STREQ(getenv("var"), "=val");
 }
 
 ATF_TC_BODY(t_putenv, tc)



Home | Main Index | Thread Index | Old Index