(parse): Support 127 arguments instead of 100, to match

player->argp[].  Assign complete argument vector.  This should make
buffer overruns blatantly obvious.

(parse): Don't bother checking SPACE argument, just crash.  It is
extremely unlikely that such a programming error wouldn't be
discovered by even them most superficial testing.
This commit is contained in:
Markus Armbruster 2005-06-13 09:33:54 +00:00
parent 9ee7a02be8
commit ac671d25e4
2 changed files with 25 additions and 24 deletions

View file

@ -386,8 +386,7 @@ extern int min(int, int);
#endif #endif
extern s_char *effadv(int); extern s_char *effadv(int);
extern int onearg(s_char *, s_char *); extern int onearg(s_char *, s_char *);
extern int parse(register s_char *, s_char **, s_char **, s_char *, extern int parse(char *, char **, char **, char *, char **);
s_char **);
extern int ldround(double, int); extern int ldround(double, int);
extern int roundintby(int, int); extern int roundintby(int, int);
extern int scthash(register int, register int, int); extern int scthash(register int, register int, int);

View file

@ -31,36 +31,39 @@
* *
*/ */
/*
* parse empire command line, chop into argp
* If values argpp and spacep passed, parse will use them.
* otherwise, parse will use static space and global argp.
* parse assumes that argpp is a char *buf[16], and that spacep
* points to a buf of at least 256 bytes.
*/
#include <ctype.h> #include <ctype.h>
#include "misc.h" #include "misc.h"
#include "gen.h" #include "gen.h"
/*
* Parse user command in BUF.
* BUF is user text.
* Set ARG[0] to point to the command name.
* Set ARG[1..N] to point to arguments, where N is the number of
* arguments. Set ARG[N+1..127] to NULL.
* If *CONDP is not null, recognize conditional argument syntax, and
* set *CONDP to the conditional argument if present, else NULL.
* Command name and arguments are copied into SPACE[], whose size must
* be at least strlen(BUF) + 1.
* If *REDIR is not null, recognize the redirection syntax, and set
* *REDIR to redirection string if present, else NULL. The
* redirection string is user text.
* Return number of slots used in ARG[], or -1 on error.
*/
int int
parse(register s_char *buf, s_char **argpp, s_char **condp, s_char *space, parse(char *buf, char **arg, char **condp, char *space, char **redir)
s_char **redir)
{ {
register s_char *bp2; char *bp2;
register s_char *bp1 = space; char *bp1 = space;
register s_char **arg = argpp;
int fs; int fs;
int quoted; int quoted;
int argnum; int argnum;
if (space == 0)
return -1;
if (redir) if (redir)
*redir = 0; *redir = 0;
if (condp != 0) if (condp != NULL)
*condp = 0; *condp = NULL;
for (argnum = 0; *buf && argnum < 100;) { for (argnum = 0; *buf && argnum < 127;) {
while (isspace(*buf)) while (isspace(*buf))
buf++; buf++;
if (!*buf) if (!*buf)
@ -86,15 +89,14 @@ parse(register s_char *buf, s_char **argpp, s_char **condp, s_char *space,
} }
} }
*bp1++ = 0; *bp1++ = 0;
if (*bp2 == '?' && condp != 0) { if (*bp2 == '?' && condp != NULL) {
*condp = bp2 + 1; *condp = bp2 + 1;
} else { } else {
arg[argnum] = bp2; arg[argnum] = bp2;
argnum++; argnum++;
} }
} }
arg[argnum] = 0; for (fs = argnum; fs < 128; fs++)
for (fs = argnum + 1; fs < 16; fs++) arg[fs] = NULL;
arg[fs] = 0;
return argnum; return argnum;
} }