Index: contrib/netbsd-tests/usr.bin/grep/t_grep.sh =================================================================== --- contrib/netbsd-tests/usr.bin/grep/t_grep.sh +++ contrib/netbsd-tests/usr.bin/grep/t_grep.sh @@ -418,6 +418,26 @@ 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() { @@ -523,6 +543,7 @@ 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 wv_combo_break atf_add_test_case fgrep_sanity atf_add_test_case egrep_sanity Index: usr.bin/grep/util.c =================================================================== --- usr.bin/grep/util.c +++ usr.bin/grep/util.c @@ -63,6 +63,7 @@ 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 @@ 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) { @@ -285,6 +287,14 @@ /* 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; @@ -357,7 +367,7 @@ unsigned int i; int c = 0, r = 0, lastmatches = 0, leflags = eflags; size_t startm = 0, matchidx; - int retry; + unsigned int retry; matchidx = pc->matchidx; @@ -373,6 +383,8 @@ } 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; @@ -429,12 +441,12 @@ * still match a whole word. */ if (r == REG_NOMATCH && - (retry == 0 || pmatch.rm_so + 1 < retry)) + (retry == pc->lnstart || + pmatch.rm_so + 1 < retry)) retry = pmatch.rm_so + 1; if (r == REG_NOMATCH) continue; } - lastmatches++; lastmatch = pmatch; @@ -463,8 +475,11 @@ } /* avoid excessive matching - skip further patterns */ if ((color == NULL && !oflag) || qflag || lflag || - matchidx >= MAX_LINE_MATCHES) + matchidx >= MAX_LINE_MATCHES) { + pc->lnstart = nst; + lastmatches = 0; break; + } } /* @@ -472,7 +487,7 @@ * again just in case we still have a chance to match later in * the string. */ - if (lastmatches == 0 && retry > 0) { + if (lastmatches == 0 && retry > pc->lnstart) { st = retry; continue; } @@ -494,6 +509,7 @@ /* Advance st based on previous matches */ st = nst; + pc->lnstart = st; } /* Reflect the new matchidx in the context */ @@ -613,12 +629,16 @@ /* --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 is printed on a per-line basis, so every + * match gets file metadata. + */ + printline_metadata(&pc->ln, sep); if (!oflag) fwrite(pc->ln.dat + a, match.rm_so - a, 1, stdout);