diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-11 23:09:34 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-11 23:09:34 +0000 |
commit | e7067e38ea8c6e0e498270ee6e8cd6aa5e92796e (patch) | |
tree | 894e240578f41db098e5c78fc8d03df97156bd75 | |
parent | ee1b3b1042c8077cda5027bc0ce317662a1d8feb (diff) | |
download | busybox-w32-e7067e38ea8c6e0e498270ee6e8cd6aa5e92796e.tar.gz busybox-w32-e7067e38ea8c6e0e498270ee6e8cd6aa5e92796e.tar.bz2 busybox-w32-e7067e38ea8c6e0e498270ee6e8cd6aa5e92796e.zip |
ash: fix segfault in "command -v"
-rw-r--r-- | shell/ash.c | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/shell/ash.c b/shell/ash.c index 77fe91a24..08bdfc3b3 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -1575,14 +1575,14 @@ static char *optionarg; /* set by nextopt (like getopt) */ | |||
1575 | static char *optptr; /* used by nextopt */ | 1575 | static char *optptr; /* used by nextopt */ |
1576 | 1576 | ||
1577 | /* | 1577 | /* |
1578 | * XXX - should get rid of. have all builtins use getopt(3). the | 1578 | * XXX - should get rid of. Have all builtins use getopt(3). |
1579 | * library getopt must have the BSD extension static variable "optreset" | 1579 | * The library getopt must have the BSD extension static variable |
1580 | * otherwise it can't be used within the shell safely. | 1580 | * "optreset", otherwise it can't be used within the shell safely. |
1581 | * | 1581 | * |
1582 | * Standard option processing (a la getopt) for builtin routines. The | 1582 | * Standard option processing (a la getopt) for builtin routines. |
1583 | * only argument that is passed to nextopt is the option string; the | 1583 | * The only argument that is passed to nextopt is the option string; |
1584 | * other arguments are unnecessary. It return the character, or '\0' on | 1584 | * the other arguments are unnecessary. It returns the character, |
1585 | * end of input. | 1585 | * or '\0' on end of input. |
1586 | */ | 1586 | */ |
1587 | static int | 1587 | static int |
1588 | nextopt(const char *optstring) | 1588 | nextopt(const char *optstring) |
@@ -1593,13 +1593,20 @@ nextopt(const char *optstring) | |||
1593 | 1593 | ||
1594 | p = optptr; | 1594 | p = optptr; |
1595 | if (p == NULL || *p == '\0') { | 1595 | if (p == NULL || *p == '\0') { |
1596 | /* We ate entire "-param", take next one */ | ||
1596 | p = *argptr; | 1597 | p = *argptr; |
1597 | if (p == NULL || *p != '-' || *++p == '\0') | 1598 | if (p == NULL) |
1599 | return '\0'; | ||
1600 | if (*p != '-') | ||
1601 | return '\0'; | ||
1602 | if (*++p == '\0') /* just "-" ? */ | ||
1598 | return '\0'; | 1603 | return '\0'; |
1599 | argptr++; | 1604 | argptr++; |
1600 | if (LONE_DASH(p)) /* check for "--" */ | 1605 | if (LONE_DASH(p)) /* "--" ? */ |
1601 | return '\0'; | 1606 | return '\0'; |
1607 | /* p => next "-param" */ | ||
1602 | } | 1608 | } |
1609 | /* p => some option char in the middle of a "-param" */ | ||
1603 | c = *p++; | 1610 | c = *p++; |
1604 | for (q = optstring; *q != c;) { | 1611 | for (q = optstring; *q != c;) { |
1605 | if (*q == '\0') | 1612 | if (*q == '\0') |
@@ -1608,8 +1615,11 @@ nextopt(const char *optstring) | |||
1608 | q++; | 1615 | q++; |
1609 | } | 1616 | } |
1610 | if (*++q == ':') { | 1617 | if (*++q == ':') { |
1611 | if (*p == '\0' && (p = *argptr++) == NULL) | 1618 | if (*p == '\0') { |
1612 | ash_msg_and_raise_error("no arg for -%c option", c); | 1619 | p = *argptr++; |
1620 | if (p == NULL) | ||
1621 | ash_msg_and_raise_error("no arg for -%c option", c); | ||
1622 | } | ||
1613 | optionarg = p; | 1623 | optionarg = p; |
1614 | p = NULL; | 1624 | p = NULL; |
1615 | } | 1625 | } |
@@ -7421,8 +7431,10 @@ commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
7421 | else if (c != 'p') | 7431 | else if (c != 'p') |
7422 | abort(); | 7432 | abort(); |
7423 | #endif | 7433 | #endif |
7424 | if (verify) | 7434 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
7435 | if (verify && (*argptr != NULL)) { | ||
7425 | return describe_command(*argptr, verify - VERIFY_BRIEF); | 7436 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
7437 | } | ||
7426 | 7438 | ||
7427 | return 0; | 7439 | return 0; |
7428 | } | 7440 | } |