diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2936,7 +2936,7 @@ def grep(ui, repo, pattern, *pats, **opt
     def matchlines(body):
         begin = 0
         linenum = 0
-        while True:
+        while True and begin < len(body):
             match = regexp.search(body, begin)
             if not match:
                 break
diff --git a/tests/test-grep.t b/tests/test-grep.t
--- a/tests/test-grep.t
+++ b/tests/test-grep.t
@@ -23,6 +23,10 @@ pattern error
 
 simple
 
+  $ hg grep '.*'
+  port:4:export
+  port:4:vaportight
+  port:4:import/export
   $ hg grep port port
   port:4:export
   port:4:vaportight