git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/7] grep: use memmem() for fixed string search

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
May 13, 2010, 20:38 UTC
Message-ID
<4BEC6338.9080300@lsrfire.ath.cx>
In-Reply-To
<4BEC6211.2000309@lsrfire.ath.cx>

Allow searching beyond NUL characters by using memmem() instead of strstr().

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 grep.c                 |   16 +++++++++-------
 t/t7008-grep-binary.sh |    4 ++++
 2 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/grep.c b/grep.c
index 4b6c02e..4633b63 100644
--- a/grep.c
+++ b/grep.c
@@ -329,14 +329,15 @@ static void show_name(struct grep_opt *opt, const char *name)
 	opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
 }
 
-
-static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
+static int fixmatch(const char *pattern, char *line, char *eol,
+		    int ignore_case, regmatch_t *match)
 {
 	char *hit;
+
 	if (ignore_case)
 		hit = strcasestr(line, pattern);
 	else
-		hit = strstr(line, pattern);
+		hit = memmem(line, eol - line, pattern, strlen(pattern));
 
 	if (!hit) {
 		match->rm_so = match->rm_eo = -1;
@@ -399,7 +400,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 
  again:
 	if (p->fixed)
-		hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
+		hit = !fixmatch(p->pattern, bol, eol, p->ignore_case, pmatch);
 	else
 		hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
 
@@ -725,9 +726,10 @@ static int look_ahead(struct grep_opt *opt,
 		int hit;
 		regmatch_t m;
 
-		if (p->fixed)
-			hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
-		else {
+		if (p->fixed) {
+			hit = !fixmatch(p->pattern, bol, bol + *left_p,
+					p->ignore_case, &m);
+		} else {
 #ifdef REG_STARTEND
 			m.rm_so = 0;
 			m.rm_eo = *left_p;
diff --git a/t/t7008-grep-binary.sh b/t/t7008-grep-binary.sh
index 5449dd9..ad97720 100755
--- a/t/t7008-grep-binary.sh
+++ b/t/t7008-grep-binary.sh
@@ -33,4 +33,8 @@ test_expect_success 'git grep -c ina a' '
 	test_cmp expect actual
 '
 
+test_expect_success 'git grep -F ile a' '
+	git grep -F ile a
+'
+
 test_done
-- 
1.7.1
Previous: René ScharfeNext: René Scharfe
Message 7 of 10 in “grep: better support for binary files”
  1. 0/7 grep: better support for binary filesRené Scharfe, May 13, 2010
  2. 1/7 grep: add test script for binary file handlingRené Scharfe, May 13, 2010
  3. 2/7 grep: refactor handling of binary mode optionsRené Scharfe, May 13, 2010
  4. 3/7 grep: --count over binaryRené Scharfe, May 13, 2010
  5. Dmitry PotapovMay 14, 2010
  6. René ScharfeMay 16, 2010
  7. 4/7 grep: use memmem() for fixed string searchRené Scharfe, May 13, 2010
  8. 5/7 grep: continue case insensitive fixed string search after NUL charsRené Scharfe, May 13, 2010
  9. 6/7 grep: add regmatch(), a wrapper for REG_STARTEND handlingRené Scharfe, May 13, 2010
  10. 7/7 grep: use regmatch() for line matchingRené Scharfe, May 13, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.