fortune, strfile: Improve validation of command-line arguments.

- Avoid potential overflow when parsing a percentage.
- Avoid truncation when copying file paths.

PR:		246050
Submitted by:	Akos Somfai <akos.somfai@gmail.com> (original)
MFC after:	1 week
This commit is contained in:
Mark Johnston 2020-08-10 17:01:59 +00:00
parent a08d04f4e4
commit 777c9f5a38
2 changed files with 19 additions and 8 deletions

View File

@ -400,11 +400,12 @@ form_file_list(char **files, int file_cnt)
sp = files[i]; sp = files[i];
else { else {
percent = 0; percent = 0;
for (sp = files[i]; isdigit((unsigned char)*sp); sp++) for (sp = files[i]; isdigit((unsigned char)*sp); sp++) {
percent = percent * 10 + *sp - '0'; percent = percent * 10 + *sp - '0';
if (percent > 100) { if (percent > 100) {
fprintf(stderr, "percentages must be <= 100\n"); fprintf(stderr, "percentages must be <= 100\n");
return (FALSE); return (FALSE);
}
} }
if (*sp == '.') { if (*sp == '.') {
fprintf(stderr, "percentages must be integers\n"); fprintf(stderr, "percentages must be integers\n");

View File

@ -295,16 +295,26 @@ getargs(int argc, char **argv)
if (*argv) { if (*argv) {
Infile = *argv; Infile = *argv;
if (*++argv) if (*++argv) {
strcpy(Outfile, *argv); if (strlcpy(Outfile, *argv, sizeof(Outfile)) >=
sizeof(Outfile)) {
fprintf(stderr,
"output_file path is too long\n");
exit(1);
}
}
} }
if (!Infile) { if (!Infile) {
puts("No input file name"); puts("No input file name");
usage(); usage();
} }
if (*Outfile == '\0') { if (*Outfile == '\0') {
strlcpy(Outfile, Infile, sizeof(Outfile)); if ((size_t)snprintf(Outfile, sizeof(Outfile), "%s.dat",
strlcat(Outfile, ".dat", sizeof(Outfile)); Infile) >= sizeof(Outfile)) {
fprintf(stderr,
"generated output_file path is too long\n");
exit(1);
}
} }
} }