diff options
| author | njoly <njoly@NetBSD.org> | 2010-10-16 11:23:41 +0000 |
|---|---|---|
| committer | njoly <njoly@NetBSD.org> | 2010-10-16 11:23:41 +0000 |
| commit | 4c968434a3726d4d3d7d95daae90dc2e46884b3a (patch) | |
| tree | db259302534e1f0bb9a1513a3d6a8835007696ee /lib/libc/stdlib | |
| parent | 53f8ab79a99dec15137c6a319c0111deb0a05a44 (diff) | |
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 (limited to 'lib/libc/stdlib')
| -rw-r--r-- | lib/libc/stdlib/getenv.3 | 27 | ||||
| -rw-r--r-- | lib/libc/stdlib/setenv.c | 21 |
2 files changed, 29 insertions, 19 deletions
diff --git a/lib/libc/stdlib/getenv.3 b/lib/libc/stdlib/getenv.3 index 0e063c38ce7..2a933523330 100644 --- a/lib/libc/stdlib/getenv.3 +++ b/lib/libc/stdlib/getenv.3 @@ -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 @@ These functions set, unset and fetch environment variables from the 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 @@ is successful, the string returned should be considered read-only. 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 --git a/lib/libc/stdlib/setenv.c b/lib/libc/stdlib/setenv.c index 625c38574ea..2385e0fea88 100644 --- a/lib/libc/stdlib/setenv.c +++ b/lib/libc/stdlib/setenv.c @@ -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 @@ int 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 @@ setenv(const char *name, const char *value, int rewrite) if (__allocenv(offset) == -1) goto bad; - if (*value == '=') /* no `=' in value */ - ++value; l_value = strlen(value); if (c != NULL) { @@ -93,9 +101,6 @@ setenv(const char *name, const char *value, int rewrite) 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; |
