summaryrefslogtreecommitdiff
path: root/coreutils/env.c
diff options
context:
space:
mode:
authorDenys Vlasenko2009-11-12 14:52:47 +0100
committerDenys Vlasenko2009-11-12 14:52:47 +0100
commitf4fee418ae9f5308b4d32bc8d4e618f779f3203f (patch)
treea8c10ec6f1513f928d04d3e5c8983754804fe330 /coreutils/env.c
parentab19ede65595f6c0daba1e9b6c7c0a2ede341fec (diff)
downloadbusybox-f4fee418ae9f5308b4d32bc8d4e618f779f3203f.zip
busybox-f4fee418ae9f5308b4d32bc8d4e618f779f3203f.tar.gz
env: don't SEGV on bare "env -"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/env.c')
-rw-r--r--coreutils/env.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/coreutils/env.c b/coreutils/env.c
index f50a03e..9635d2b 100644
--- a/coreutils/env.c
+++ b/coreutils/env.c
@@ -43,21 +43,20 @@ static const char env_longopts[] ALIGN1 =
int env_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int env_main(int argc UNUSED_PARAM, char **argv)
{
- char **ep;
- unsigned opt;
+ unsigned opts;
llist_t *unset_env = NULL;
opt_complementary = "u::";
#if ENABLE_FEATURE_ENV_LONG_OPTIONS
applet_long_options = env_longopts;
#endif
- opt = getopt32(argv, "+iu:", &unset_env);
+ opts = getopt32(argv, "+iu:", &unset_env);
argv += optind;
- if (*argv && LONE_DASH(argv[0])) {
- opt |= 1;
+ if (argv[0] && LONE_DASH(argv[0])) {
+ opts |= 1;
++argv;
}
- if (opt & 1) {
+ if (opts & 1) {
clearenv();
}
while (unset_env) {
@@ -84,8 +83,11 @@ int env_main(int argc UNUSED_PARAM, char **argv)
bb_simple_perror_msg_and_die(*argv);
}
- for (ep = environ; *ep; ep++) {
- puts(*ep);
+ if (environ) { /* clearenv() may set environ == NULL! */
+ char **ep;
+ for (ep = environ; *ep; ep++) {
+ puts(*ep);
+ }
}
fflush_stdout_and_exit(EXIT_SUCCESS);