ash: fix segfault in "command -v"

This commit is contained in:
Denis Vlasenko 2008-07-11 23:09:34 +00:00
parent ee1b3b1042
commit e7067e38ea

View File

@ -1575,14 +1575,14 @@ static char *optionarg; /* set by nextopt (like getopt) */
static char *optptr; /* used by nextopt */ static char *optptr; /* used by nextopt */
/* /*
* XXX - should get rid of. have all builtins use getopt(3). the * XXX - should get rid of. Have all builtins use getopt(3).
* library getopt must have the BSD extension static variable "optreset" * The library getopt must have the BSD extension static variable
* otherwise it can't be used within the shell safely. * "optreset", otherwise it can't be used within the shell safely.
* *
* Standard option processing (a la getopt) for builtin routines. The * Standard option processing (a la getopt) for builtin routines.
* only argument that is passed to nextopt is the option string; the * The only argument that is passed to nextopt is the option string;
* other arguments are unnecessary. It return the character, or '\0' on * the other arguments are unnecessary. It returns the character,
* end of input. * or '\0' on end of input.
*/ */
static int static int
nextopt(const char *optstring) nextopt(const char *optstring)
@ -1593,13 +1593,20 @@ nextopt(const char *optstring)
p = optptr; p = optptr;
if (p == NULL || *p == '\0') { if (p == NULL || *p == '\0') {
/* We ate entire "-param", take next one */
p = *argptr; p = *argptr;
if (p == NULL || *p != '-' || *++p == '\0') if (p == NULL)
return '\0';
if (*p != '-')
return '\0';
if (*++p == '\0') /* just "-" ? */
return '\0'; return '\0';
argptr++; argptr++;
if (LONE_DASH(p)) /* check for "--" */ if (LONE_DASH(p)) /* "--" ? */
return '\0'; return '\0';
/* p => next "-param" */
} }
/* p => some option char in the middle of a "-param" */
c = *p++; c = *p++;
for (q = optstring; *q != c;) { for (q = optstring; *q != c;) {
if (*q == '\0') if (*q == '\0')
@ -1608,8 +1615,11 @@ nextopt(const char *optstring)
q++; q++;
} }
if (*++q == ':') { if (*++q == ':') {
if (*p == '\0' && (p = *argptr++) == NULL) if (*p == '\0') {
ash_msg_and_raise_error("no arg for -%c option", c); p = *argptr++;
if (p == NULL)
ash_msg_and_raise_error("no arg for -%c option", c);
}
optionarg = p; optionarg = p;
p = NULL; p = NULL;
} }
@ -7421,8 +7431,10 @@ commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
else if (c != 'p') else if (c != 'p')
abort(); abort();
#endif #endif
if (verify) /* Mimic bash: just "command -v" doesn't complain, it's a nop */
if (verify && (*argptr != NULL)) {
return describe_command(*argptr, verify - VERIFY_BRIEF); return describe_command(*argptr, verify - VERIFY_BRIEF);
}
return 0; return 0;
} }