diff --git a/src/search.c b/src/search.c
index d9b4462..38b929f 100644
--- a/src/search.c
+++ b/src/search.c
@@ -385,7 +385,7 @@
          /* No good fixed strings; start with DFA. */
          char const *next_beg = dfaexec (&dfa, beg, (char *) buflim,
                          0, NULL, &backref);
-         if (next_beg == NULL)
+         if (next_beg == NULL || next_beg == buflim)
        break;
          /* Narrow down to the line we've found. */
          beg = next_beg;
