diff options
author | knu <knu@FreeBSD.org> | 2001-11-19 19:57:45 +0000 |
---|---|---|
committer | knu <knu@FreeBSD.org> | 2001-11-19 19:57:45 +0000 |
commit | 0fe2ba53883223d69e19b4717389976dfbd78eb6 (patch) | |
tree | 90c2e9a4454d39c387003fb60adc404a47c36e49 /bin/test/test.c | |
parent | 9a6b4717f37df4cf4f5b0c6351cb5386264ce09a (diff) | |
download | FreeBSD-src-0fe2ba53883223d69e19b4717389976dfbd78eb6.zip FreeBSD-src-0fe2ba53883223d69e19b4717389976dfbd78eb6.tar.gz |
- Do not reference argv[1] if no argument is given.
Reported by: brian
- Call error() instead of errx() if compiled as sh(1) builtin.
Diffstat (limited to 'bin/test/test.c')
-rw-r--r-- | bin/test/test.c | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/bin/test/test.c b/bin/test/test.c index 1cd9a11..5d711a2 100644 --- a/bin/test/test.c +++ b/bin/test/test.c @@ -30,6 +30,30 @@ static const char rcsid[] = #ifdef SHELL #define main testcmd #include "bltin/bltin.h" +#else +static void error(const char *, ...) __attribute__((__noreturn__)); + +static void +#ifdef __STDC__ +error(const char *msg, ...) +#else +error(va_alist) + va_dcl +#endif +{ + va_list ap; +#ifndef __STDC__ + const char *msg; + + va_start(ap); + msg = va_arg(ap, const char *); +#else + va_start(ap, msg); +#endif + verrx(2, msg, ap); + /*NOTREACHED*/ + va_end(ap); +} #endif /* test(1) accepts the following grammar: @@ -180,7 +204,7 @@ main(argc, argv) p++; if (strcmp(p, "[") == 0) { if (strcmp(argv[--argc], "]")) - errx(2, "missing ]"); + error("missing ]"); argv[argc] = NULL; } @@ -188,6 +212,9 @@ main(argc, argv) (void)setgid(getegid()); (void)setuid(geteuid()); + if (--argc <= 0) + return 1; + t_wp = &argv[1]; res = !oexpr(t_lex(*t_wp)); @@ -204,9 +231,9 @@ syntax(op, msg) { if (op && *op) - errx(2, "%s: %s", op, msg); + error("%s: %s", op, msg); else - errx(2, "%s", msg); + error("%s", msg); } static int @@ -441,13 +468,13 @@ getn(s) r = strtol(s, &p, 10); if (errno != 0) - errx(2, "%s: out of range", s); + error("%s: out of range", s); while (isspace((unsigned char)*p)) p++; if (*p) - errx(2, "%s: bad number", s); + error("%s: bad number", s); return (int) r; } @@ -464,13 +491,13 @@ getq(s) r = strtoq(s, &p, 10); if (errno != 0) - errx(2, "%s: out of range", s); + error("%s: out of range", s); while (isspace((unsigned char)*p)) p++; if (*p) - errx(2, "%s: bad number", s); + error("%s: bad number", s); return r; } |