--- src/usr.bin/checknr/checknr.c 2005/03/02 04:33:12 1.9 +++ src/usr.bin/checknr/checknr.c 2005/03/02 07:20:44 1.10 @@ -43,6 +43,7 @@ * later but for now think of these restrictions as contributions to * structured typesetting. */ +#include #include #include #include @@ -188,19 +189,24 @@ main(int argc, char **argv) char b1[4]; /* Figure out how many known commands there are */ - while (knowncmds[ncmds][0] != '\0') + ncmds = 0; + while (ncmds < MAXCMDS && knowncmds[ncmds][0] != '\0') ncmds++; while (argc > 1 && argv[1][0] == '-') { switch(argv[1][1]) { /* -a: add pairs of macros */ case 'a': - i = strlen(argv[1]) - 2; - if (i % 6 != 0) + if ((strlen(argv[1]) - 2) % 6 != 0) usage(); /* look for empty macro slots */ - for (i = 0; br[i].opbr[0] != '\0'; i++) - ; + i = 0; + while (i < MAXBR && br[i].opbr[0] != '\0') + i++; + if (i >= MAXBR) { + errx(1, "Only %d known macro-pairs allowed", + MAXBR); + } for (cp = argv[1] + 3; cp[-1]; cp += 6) { strncpy(br[i].opbr, cp, 2); strncpy(br[i].clbr, cp + 3, 2); @@ -529,10 +535,6 @@ addcmd(char *myline) mac[2] = 0; if (isspace(mac[1]) || mac[1] == '\\') mac[1] = 0; - if (ncmds >= MAXCMDS) { - printf("Only %d known commands allowed\n", MAXCMDS); - exit(1); - } addmac(mac); } @@ -548,6 +550,10 @@ addmac(const char *mac) { int i, slot; + if (ncmds >= MAXCMDS) { + errx(1, "Only %d known commands allowed", MAXCMDS); + } + if (binsrch(mac, &slot) >= 0) { /* it's OK to redefine something */ #ifdef DEBUG printf("binsrch(%s) -> already in table\n", mac);