Avoid buffer overrun when identifies or filenames are extremely long.

OpenBSD revisions: C.c 1.4-1.5, ctags.c 1.5, fortran.c 1.3, lisp.c 1.3,
tree.c 1.2

Obtained from:	OpenBSD
This commit is contained in:
Tim J. Robbins 2002-05-30 10:54:53 +00:00
parent fe71224650
commit e58bac2e7f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=97574
5 changed files with 37 additions and 14 deletions

View File

@ -239,7 +239,11 @@ c_entries()
sp = tok; sp = tok;
} }
else if (sp != tok || begtoken(c)) { else if (sp != tok || begtoken(c)) {
*sp++ = c; if (sp == tok + sizeof tok - 1)
/* Too long -- truncate it */
*sp = EOS;
else
*sp++ = c;
token = YES; token = YES;
} }
continue; continue;
@ -337,7 +341,11 @@ hash_entry()
return; return;
if (iswhite(c)) if (iswhite(c))
break; break;
*sp++ = c; if (sp == tok + sizeof tok - 1)
/* Too long -- truncate it */
*sp = EOS;
else
*sp++ = c;
} }
*sp = EOS; *sp = EOS;
if (memcmp(tok, "define", 6)) /* only interested in #define's */ if (memcmp(tok, "define", 6)) /* only interested in #define's */
@ -349,7 +357,11 @@ hash_entry()
break; break;
} }
for (sp = tok;;) { /* get next token */ for (sp = tok;;) { /* get next token */
*sp++ = c; if (sp == tok + sizeof tok - 1)
/* Too long -- truncate it */
*sp = EOS;
else
*sp++ = c;
if (GETC(==, EOF)) if (GETC(==, EOF))
return; return;
/* /*
@ -391,7 +403,11 @@ str_entry(c)
if (c == '{') /* it was "struct {" */ if (c == '{') /* it was "struct {" */
return (YES); return (YES);
for (sp = tok;;) { /* get next token */ for (sp = tok;;) { /* get next token */
*sp++ = c; if (sp == tok + sizeof tok - 1)
/* Too long -- truncate it */
*sp = EOS;
else
*sp++ = c;
if (GETC(==, EOF)) if (GETC(==, EOF))
return (NO); return (NO);
if (!intoken(c)) if (!intoken(c))

View File

@ -95,7 +95,7 @@ main(argc, argv)
int exit_val; /* exit value */ int exit_val; /* exit value */
int step; /* step through args */ int step; /* step through args */
int ch; /* getopts char */ int ch; /* getopts char */
char cmd[100]; /* too ugly to explain */ char *cmd;
aflag = uflag = NO; aflag = uflag = NO;
while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1) while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
@ -157,11 +157,14 @@ main(argc, argv)
else { else {
if (uflag) { if (uflag) {
for (step = 0; step < argc; step++) { for (step = 0; step < argc; step++) {
(void)sprintf(cmd, (void)asprintf(&cmd,
"mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS", "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
outfile, argv[step], outfile, argv[step], outfile);
outfile); if (cmd == NULL)
err(1, "out of space");
system(cmd); system(cmd);
free(cmd);
cmd = NULL;
} }
++aflag; ++aflag;
} }
@ -170,9 +173,13 @@ main(argc, argv)
put_entries(head); put_entries(head);
(void)fclose(outf); (void)fclose(outf);
if (uflag) { if (uflag) {
(void)sprintf(cmd, "sort -o %s %s", (void)asprintf(&cmd, "sort -o %s %s",
outfile, outfile); outfile, outfile);
if (cmd == NULL)
err(1, "out of space");
system(cmd); system(cmd);
free(cmd);
cmd = NULL;
} }
} }
} }

View File

@ -127,7 +127,7 @@ PF_funcs()
if ((cp = lbp + 1)) if ((cp = lbp + 1))
continue; continue;
*cp = EOS; *cp = EOS;
(void)strcpy(tok, lbp); (void)strlcpy(tok, lbp, sizeof(tok)); /* possible trunc */
getline(); /* process line for ex(1) */ getline(); /* process line for ex(1) */
pfnote(tok, lineno); pfnote(tok, lineno);
pfcnt = YES; pfcnt = YES;

View File

@ -101,7 +101,7 @@ l_entries()
continue; continue;
savedc = *cp; savedc = *cp;
*cp = EOS; *cp = EOS;
(void)strcpy(tok, lbp); (void)strlcpy(tok, lbp, sizeof(tok)); /* possible trunc */
*cp = savedc; *cp = savedc;
getline(); getline();
pfnote(tok, lineno); pfnote(tok, lineno);

View File

@ -78,7 +78,7 @@ pfnote(name, ln)
fp = curfile; fp = curfile;
else else
++fp; ++fp;
(void)sprintf(nbuf, "M%s", fp); (void)snprintf(nbuf, sizeof(nbuf), "M%s", fp);
fp = strrchr(nbuf, '.'); fp = strrchr(nbuf, '.');
if (fp && !fp[2]) if (fp && !fp[2])
*fp = EOS; *fp = EOS;