diff --git a/contrib/netbsd-tests/usr.bin/grep/t_grep.sh b/contrib/netbsd-tests/usr.bin/grep/t_grep.sh index 4833ef393af..e0aaf2dcb3e 100755 --- a/contrib/netbsd-tests/usr.bin/grep/t_grep.sh +++ b/contrib/netbsd-tests/usr.bin/grep/t_grep.sh @@ -412,6 +412,26 @@ wflag_emptypat_body() atf_check -o file:test4 grep -w -e "" test4 } +atf_test_case excessive_matches +excessive_matches_head() +{ + atf_set "descr" "Check for proper handling of lines with excessive matches (PR 218811)" +} +excessive_matches_body() +{ + grep_type + if [ $type -eq $GREP_TYPE_GNU_FREEBSD ]; then + atf_expect_fail "this test does not pass with GNU grep in base" + fi + + for i in $(jot 4096); do + printf "x" >> test.in + done + + atf_check -s exit:0 -x '[ $(grep -o x test.in | wc -l) -eq 4096 ]' + atf_check -s exit:1 -x 'grep -on x test.in | grep -v "1:x"' +} + atf_test_case fgrep_sanity fgrep_sanity_head() { @@ -500,6 +520,7 @@ atf_init_test_cases() atf_add_test_case egrep_empty_invalid atf_add_test_case zerolen atf_add_test_case wflag_emptypat + atf_add_test_case excessive_matches atf_add_test_case fgrep_sanity atf_add_test_case egrep_sanity atf_add_test_case grep_sanity diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index 014aaa4ed7a..d116e916bf2 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -63,6 +63,7 @@ static bool first_match = true; struct parsec { regmatch_t matches[MAX_LINE_MATCHES]; /* Matches made */ struct str ln; /* Current line */ + size_t lnstart; size_t matchidx; /* Latest used match index */ bool binary; /* Binary file? */ }; @@ -244,8 +245,9 @@ procfile(const char *fn) same_file = false; for (c = 0; c == 0 || !(lflag || qflag); ) { - /* Reset match count for every line processed */ + /* Reset match count and line start for every line processed */ pc.matchidx = 0; + pc.lnstart = 0; pc.ln.off += pc.ln.len + 1; if ((pc.ln.dat = grep_fgetln(f, &pc.ln.len)) == NULL || pc.ln.len == 0) { if (pc.ln.line_no == 0 && matchall) @@ -281,6 +283,14 @@ procfile(const char *fn) /* Print the matching line, but only if not quiet/binary */ if (t == 0 && !qflag && !pc.binary) { printline(&pc, ':'); + while(pc.matchidx >= MAX_LINE_MATCHES) { + /* Reset matchidx and try again */ + pc.matchidx = 0; + if (procline(&pc) == 0) + printline(&pc, ':'); + else + break; + } first_match = false; same_file = true; last_outed = 0; @@ -349,7 +359,7 @@ procline(struct parsec *pc) { regmatch_t pmatch, lastmatch, chkmatch; wchar_t wbegin, wend; - size_t st = 0, nst = 0; + size_t st, nst; unsigned int i; int c = 0, r = 0, lastmatches = 0, leflags = eflags; size_t startm = 0, matchidx; @@ -368,6 +378,8 @@ procline(struct parsec *pc) } else if (matchall) return (0); + st = pc->lnstart; + nst = 0; /* Initialize to avoid a false positive warning from GCC. */ lastmatch.rm_so = lastmatch.rm_eo = 0; @@ -440,6 +452,11 @@ procline(struct parsec *pc) pc->matches[matchidx++] = pmatch; nst = pmatch.rm_eo; } + } else { + /* Make sure we restart matching here */ + pc->lnstart = st; + lastmatches = 0; + break; } /* matches - skip further patterns */ @@ -471,6 +488,7 @@ procline(struct parsec *pc) /* Advance st based on previous matches */ st = nst; + pc->lnstart = st; } /* Reflect the new matchidx in the context */ @@ -591,12 +609,13 @@ printline(struct parsec *pc, int sep) /* --color and -o */ if ((oflag || color) && matchidx > 0) { - printline_metadata(&pc->ln, sep); for (i = 0; i < matchidx; i++) { match = pc->matches[i]; /* Don't output zero length matches */ if (match.rm_so == match.rm_eo) continue; + /* Metadata should be printed on a per-line basis */ + printline_metadata(&pc->ln, sep); if (!oflag) fwrite(pc->ln.dat + a, match.rm_so - a, 1, stdout);