[PATCH] grep: fix an infinite loop in grep -w

Bartosz Golaszewski bartekgola at gmail.com
Mon Jan 6 19:06:37 UTC 2014


This fixes a bug introduced by commit 414db791d0dc79905e5afe20e503941b8f9778cc
where busybox would hang on grep -w ^ file.txt. Busybox will now produce
output compatible with upstream grep in this case.

Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
---
 findutils/grep.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/findutils/grep.c b/findutils/grep.c
index b8ad1bf..acf38bb 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -416,8 +416,14 @@ static int grep_file(FILE *file)
 							if (!c || (!isalnum(c) && c != '_')) {
 								found = 1;
 							} else {
-								match_at += gl->matched_range.rm_eo;
-								goto opt_w_again;
+								/* Avoid an infinite loop if the partially
+								 * matched string is 0 bytes long - e.g. "^" regex
+								 * passed as search pattern.
+								 */
+								if (gl->matched_range.rm_eo) {
+									match_at += gl->matched_range.rm_eo;
+									goto opt_w_again;
+								}
 							}
 						}
 					}
-- 
1.7.10.4



More information about the busybox mailing list