From 86cf0364bd58e07646a23a1128e4a9ea79189579 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 8 Mar 2011 12:44:02 +0100 Subject: printenv: fix environ == NULL segfault Signed-off-by: Denys Vlasenko --- coreutils/printenv.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'coreutils/printenv.c') diff --git a/coreutils/printenv.c b/coreutils/printenv.c index 33be5c096..d0fb71636 100644 --- a/coreutils/printenv.c +++ b/coreutils/printenv.c @@ -19,9 +19,14 @@ int printenv_main(int argc UNUSED_PARAM, char **argv) /* no variables specified, show whole env */ if (!argv[1]) { - int e = 0; - while (environ[e]) - puts(environ[e++]); + char **e = environ; + + /* environ can be NULL! (for example, after clearenv()) + * Check for that: + */ + if (e) + while (*e) + puts(*e++); } else { /* search for specified variables and print them out if found */ char *arg, *env; -- cgit v1.2.3