summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep
diff options
context:
space:
mode:
authormartijn <martijn@openbsd.org>2020-07-23 20:19:27 +0000
committermartijn <martijn@openbsd.org>2020-07-23 20:19:27 +0000
commit483368fb1fd1e08929f65e98b837b307662a61d5 (patch)
treeeb251d73213f376d05d79dc7f981b26c605acffb /usr.bin/grep
parentgetline(3) does it's own memory allocation. No need to use an intermediate (diff)
downloadwireguard-openbsd-483368fb1fd1e08929f65e98b837b307662a61d5.tar.xz
wireguard-openbsd-483368fb1fd1e08929f65e98b837b307662a61d5.zip
Change line counter from int to unsigned long long to reduce overflow.
In case unsigned long long is miraculously still too small add an additional overflow detection so we stop counting and add a marker to couter output. Input on earlier diff guenther@ OK millert
Diffstat (limited to 'usr.bin/grep')
-rw-r--r--usr.bin/grep/grep.c4
-rw-r--r--usr.bin/grep/util.c16
2 files changed, 12 insertions, 8 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c
index 55563095404..f41b5e20ca6 100644
--- a/usr.bin/grep/grep.c
+++ b/usr.bin/grep/grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.64 2019/12/03 09:14:37 jca Exp $ */
+/* $OpenBSD: grep.c,v 1.65 2020/07/23 20:19:27 martijn Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -517,7 +517,7 @@ main(int argc, char *argv[])
c = grep_tree(argv);
else
for (c = 0; argc--; ++argv)
- c += procfile(*argv);
+ c |= procfile(*argv);
exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
}
diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c
index db605e99cee..e16d08e7d85 100644
--- a/usr.bin/grep/util.c
+++ b/usr.bin/grep/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.62 2019/12/03 09:14:37 jca Exp $ */
+/* $OpenBSD: util.c,v 1.63 2020/07/23 20:19:27 martijn Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -91,7 +91,7 @@ grep_tree(char **argv)
/* skip "./" if implied */
if (argv == dot_argv && p->fts_pathlen >= 2)
path += 2;
- c += procfile(path);
+ c |= procfile(path);
break;
}
}
@@ -106,7 +106,8 @@ procfile(char *fn)
{
str_t ln;
file_t *f;
- int c, t, z, nottext;
+ int t, z, nottext, overflow = 0;
+ unsigned long long c;
mcount = mlimit;
@@ -158,7 +159,10 @@ procfile(char *fn)
enqueue(&ln);
linesqueued++;
}
- c += t;
+ if (ULLONG_MAX - c < (unsigned long long)t)
+ overflow = 1;
+ else
+ c += t;
if (mflag && mcount <= 0)
break;
}
@@ -169,7 +173,7 @@ procfile(char *fn)
if (cflag) {
if (!hflag)
printf("%s:", ln.file);
- printf("%u\n", c);
+ printf("%llu%s\n", c, overflow ? "+" : "");
}
if (lflag && c != 0)
printf("%s\n", fn);
@@ -179,7 +183,7 @@ procfile(char *fn)
binbehave == BIN_FILE_BIN && nottext && !qflag)
printf("Binary file %s matches\n", fn);
- return c;
+ return overflow || c != 0;
}