From: cheloha Date: Sun, 10 Oct 2021 15:57:25 +0000 (+0000) Subject: head(1): validate all line count arguments X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=3d668c255f6827a5d207253952bd0609df072e4d;p=openbsd head(1): validate all line count arguments We need to validate all line count option arguments, not just the last such argument found. While here, switch to the default strtonum(3) error message format: is : Thread: https://marc.info/?l=openbsd-tech&m=163383023212104&w=2 ok millert@ --- diff --git a/usr.bin/head/head.c b/usr.bin/head/head.c index ccf13720dc2..f0c10cd2666 100644 --- a/usr.bin/head/head.c +++ b/usr.bin/head/head.c @@ -1,4 +1,4 @@ -/* $OpenBSD: head.c,v 1.21 2016/03/20 17:14:51 tb Exp $ */ +/* $OpenBSD: head.c,v 1.22 2021/10/10 15:57:25 cheloha Exp $ */ /* * Copyright (c) 1980, 1987 Regents of the University of California. @@ -48,11 +48,11 @@ static void usage(void); int main(int argc, char *argv[]) { + const char *errstr; FILE *fp; long cnt; int ch, firsttime; long linecnt = 10; - char *p = NULL; int status = 0; if (pledge("stdio rpath", NULL) == -1) @@ -61,7 +61,9 @@ main(int argc, char *argv[]) /* handle obsolete -number syntax */ if (argc > 1 && argv[1][0] == '-' && isdigit((unsigned char)argv[1][1])) { - p = argv[1] + 1; + linecnt = strtonum(argv[1] + 1, 1, LONG_MAX, &errstr); + if (errstr != NULL) + errx(1, "count is %s: %s", errstr, argv[1] + 1); argc--; argv++; } @@ -69,7 +71,9 @@ main(int argc, char *argv[]) while ((ch = getopt(argc, argv, "n:")) != -1) { switch (ch) { case 'n': - p = optarg; + linecnt = strtonum(optarg, 1, LONG_MAX, &errstr); + if (errstr != NULL) + errx(1, "count is %s: %s", errstr, optarg); break; default: usage(); @@ -77,14 +81,6 @@ main(int argc, char *argv[]) } argc -= optind, argv += optind; - if (p) { - const char *errstr; - - linecnt = strtonum(p, 1, LONG_MAX, &errstr); - if (errstr) - errx(1, "line count %s: %s", errstr, p); - } - for (firsttime = 1; ; firsttime = 0) { if (!*argv) { if (!firsttime)