svn commit: r322607 - in stable/11: contrib/netbsd-tests/usr.bin/grep usr.bin/grep
Kyle Evans
kevans at FreeBSD.org
Thu Aug 17 04:04:43 UTC 2017
Author: kevans
Date: Thu Aug 17 04:04:42 2017
New Revision: 322607
URL: https://svnweb.freebsd.org/changeset/base/322607
Log:
bsdgrep: Don't allow negative context flags, add more tests
MFC r318302: bsdgrep: don't allow negative -A / -B / -C
Previously, when given a negative -A/-B/-C argument bsdgrep would
overflow the respective context flag(s) and exhibited surprising
behavior.
Fix this by removing unsignedness of Aflag/Bflag and erroring out if
we're given a value < 0. Also adjust the type used to track 'tail'
context in procfile() so that it accurately reflects the Aflag value
rather than overflowing and losing trailing context.
This also fixes an inconsistency previously existing between -n and
-C "n" behavior. They are now both limited to LLONG_MAX, to be
consistent.
Add some test cases to make sure grep errors out properly for both
negative context values as well as non-numeric context values rather
than giving bogus matches.
MFC r318317: bsdgrep: add more tests for different binary flags
The existing 'binary' test in netbsd-tests/ does a basic check of the
default treatment for binary behavior, but not much more than that.
Given some opportunity for breakage recently that did not trigger any
failures, add some tests to cover the three different binary file
behaviors (a, -I, -U) and their --binary-files= equivalent values.
Approved by: emaste (mentor, blanket MFC)
Modified:
stable/11/contrib/netbsd-tests/usr.bin/grep/t_grep.sh
stable/11/usr.bin/grep/grep.c
stable/11/usr.bin/grep/grep.h
stable/11/usr.bin/grep/queue.c
stable/11/usr.bin/grep/util.c
Directory Properties:
stable/11/ (props changed)
Modified: stable/11/contrib/netbsd-tests/usr.bin/grep/t_grep.sh
==============================================================================
--- stable/11/contrib/netbsd-tests/usr.bin/grep/t_grep.sh Thu Aug 17 03:56:42 2017 (r322606)
+++ stable/11/contrib/netbsd-tests/usr.bin/grep/t_grep.sh Thu Aug 17 04:04:42 2017 (r322607)
@@ -517,6 +517,63 @@ grep_nomatch_flags_body()
atf_check -o empty grep -q -A 1 -e "B" test1
atf_check -o empty grep -q -C 1 -e "B" test1
}
+
+atf_test_case badcontext
+badcontext_head()
+{
+ atf_set "descr" "Check for handling of invalid context arguments"
+}
+badcontext_body()
+{
+ printf "A\nB\nC\n" > test1
+
+ atf_check -s not-exit:0 -e ignore grep -A "-1" "B" test1
+
+ atf_check -s not-exit:0 -e ignore grep -B "-1" "B" test1
+
+ atf_check -s not-exit:0 -e ignore grep -C "-1" "B" test1
+
+ atf_check -s not-exit:0 -e ignore grep -A "B" "B" test1
+
+ atf_check -s not-exit:0 -e ignore grep -B "B" "B" test1
+
+ atf_check -s not-exit:0 -e ignore grep -C "B" "B" test1
+}
+
+atf_test_case binary_flags
+binary_flags_head()
+{
+ atf_set "descr" "Check output for binary flags (-a, -I, -U, --binary-files)"
+}
+binary_flags_body()
+{
+ printf "A\000B\000C" > test1
+ printf "A\n\000B\n\000C" > test2
+ binmatchtext="Binary file test1 matches\n"
+
+ # Binaries not treated as text (default, -U)
+ atf_check -o inline:"${binmatchtext}" grep 'B' test1
+ atf_check -o inline:"${binmatchtext}" grep 'B' -C 1 test1
+
+ atf_check -o inline:"${binmatchtext}" grep -U 'B' test1
+ atf_check -o inline:"${binmatchtext}" grep -U 'B' -C 1 test1
+
+ # Binary, -a, no newlines
+ atf_check -o inline:"A\000B\000C\n" grep -a 'B' test1
+ atf_check -o inline:"A\000B\000C\n" grep -a 'B' -C 1 test1
+
+ # Binary, -a, newlines
+ atf_check -o inline:"\000B\n" grep -a 'B' test2
+ atf_check -o inline:"A\n\000B\n\000C\n" grep -a 'B' -C 1 test2
+
+ # Binary files ignored
+ atf_check -s exit:1 grep -I 'B' test2
+
+ # --binary-files equivalence
+ atf_check -o inline:"${binmatchtext}" grep --binary-files=binary 'B' test1
+ atf_check -o inline:"A\000B\000C\n" grep --binary-files=text 'B' test1
+ atf_check -s exit:1 grep --binary-files=without-match 'B' test2
+}
# End FreeBSD
atf_init_test_cases()
@@ -551,5 +608,7 @@ atf_init_test_cases()
atf_add_test_case egrep_sanity
atf_add_test_case grep_sanity
atf_add_test_case grep_nomatch_flags
+ atf_add_test_case binary_flags
+ atf_add_test_case badcontext
# End FreeBSD
}
Modified: stable/11/usr.bin/grep/grep.c
==============================================================================
--- stable/11/usr.bin/grep/grep.c Thu Aug 17 03:56:42 2017 (r322606)
+++ stable/11/usr.bin/grep/grep.c Thu Aug 17 04:04:42 2017 (r322607)
@@ -109,8 +109,8 @@ struct epat *dpattern, *fpattern;
char re_error[RE_ERROR_BUF + 1];
/* Command-line flags */
-unsigned long long Aflag; /* -A x: print x lines trailing each match */
-unsigned long long Bflag; /* -B x: print x lines leading each match */
+long long Aflag; /* -A x: print x lines trailing each match */
+long long Bflag; /* -B x: print x lines leading each match */
bool Hflag; /* -H: always print file name */
bool Lflag; /* -L: only show names of files with no matches */
bool bflag; /* -b: show block numbers for each match */
@@ -352,7 +352,7 @@ main(int argc, char *argv[])
char **aargv, **eargv, *eopts;
char *ep;
const char *pn;
- unsigned long long l;
+ long long l;
unsigned int aargc, eargc, i;
int c, lastc, needpattern, newarg, prevoptind;
@@ -439,10 +439,11 @@ main(int argc, char *argv[])
case '5': case '6': case '7': case '8': case '9':
if (newarg || !isdigit(lastc))
Aflag = 0;
- else if (Aflag > LLONG_MAX / 10) {
+ else if (Aflag > LLONG_MAX / 10 - 1) {
errno = ERANGE;
err(2, NULL);
}
+
Aflag = Bflag = (Aflag * 10) + (c - '0');
break;
case 'C':
@@ -455,14 +456,17 @@ main(int argc, char *argv[])
/* FALLTHROUGH */
case 'B':
errno = 0;
- l = strtoull(optarg, &ep, 10);
- if (((errno == ERANGE) && (l == ULLONG_MAX)) ||
- ((errno == EINVAL) && (l == 0)))
+ l = strtoll(optarg, &ep, 10);
+ if (errno == ERANGE || errno == EINVAL)
err(2, NULL);
else if (ep[0] != '\0') {
errno = EINVAL;
err(2, NULL);
+ } else if (l < 0) {
+ errno = EINVAL;
+ err(2, "context argument must be non-negative");
}
+
if (c == 'A')
Aflag = l;
else if (c == 'B')
Modified: stable/11/usr.bin/grep/grep.h
==============================================================================
--- stable/11/usr.bin/grep/grep.h Thu Aug 17 03:56:42 2017 (r322606)
+++ stable/11/usr.bin/grep/grep.h Thu Aug 17 04:04:42 2017 (r322607)
@@ -115,7 +115,7 @@ extern bool Eflag, Fflag, Gflag, Hflag, Lflag,
bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
qflag, sflag, vflag, wflag, xflag;
extern bool dexclude, dinclude, fexclude, finclude, lbflag, nullflag;
-extern unsigned long long Aflag, Bflag;
+extern long long Aflag, Bflag;
extern long long mcount;
extern long long mlimit;
extern char fileeol;
Modified: stable/11/usr.bin/grep/queue.c
==============================================================================
--- stable/11/usr.bin/grep/queue.c Thu Aug 17 03:56:42 2017 (r322606)
+++ stable/11/usr.bin/grep/queue.c Thu Aug 17 04:04:42 2017 (r322607)
@@ -49,7 +49,7 @@ struct qentry {
};
static STAILQ_HEAD(, qentry) queue = STAILQ_HEAD_INITIALIZER(queue);
-static unsigned long long count;
+static long long count;
static struct qentry *dequeue(void);
Modified: stable/11/usr.bin/grep/util.c
==============================================================================
--- stable/11/usr.bin/grep/util.c Thu Aug 17 03:56:42 2017 (r322606)
+++ stable/11/usr.bin/grep/util.c Thu Aug 17 04:04:42 2017 (r322607)
@@ -196,11 +196,12 @@ int
procfile(const char *fn)
{
struct parsec pc;
+ long long tail;
struct file *f;
struct stat sb;
struct str *ln;
mode_t s;
- int c, last_outed, t, tail;
+ int c, last_outed, t;
bool doctx, printmatch, same_file;
if (strcmp(fn, "-") == 0) {
More information about the svn-src-stable
mailing list