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

[PATCH v2 4/9] grep/icase: avoid kwsset when -F is specified

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jul 8, 2015, 10:38 UTC
Message-ID
<1436351919-2520-5-git-send-email-pclouds@gmail.com>
In-Reply-To
<1436351919-2520-1-git-send-email-pclouds@gmail.com>

Similar to the previous commit, we can't use kws on icase search outside ascii range. But we can't simply pass the pattern to regcomp/pcre like the previous commit because it may contain regex special characters, so we need to quote the regex first.

To avoid misquote traps that could lead to undefined behavior, we always stick to basic regex engine in this case. We don't need fancy features for grepping a literal string anyway.

basic_regex_quote_buf() assumes that if the pattern is in a multibyte encoding, ascii chars must be unambiguously encoded as single bytes. This is true at least for UTF-8. For others, let's wait until people yell up. Chances are nobody uses multibyte, non utf-8 charsets any more..

Helped-by: René Scharfe <l.s.r@web.de>
Noticed-by: Plamen Totev <plamen.totev@abv.bg>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 grep.c                          | 25 ++++++++++++++++++++++++-
 quote.c                         | 37 +++++++++++++++++++++++++++++++++++++
 quote.h                         |  1 +
 t/t7812-grep-icase-non-ascii.sh | 26 ++++++++++++++++++++++++++
 4 files changed, 88 insertions(+), 1 deletion(-)
diff --git a/grep.c b/grep.c
index d795b0e..8fce54f 100644
--- a/grep.c
+++ b/grep.c
@@ -5,6 +5,7 @@
 #include "diff.h"
 #include "diffcore.h"
 #include "commit.h"
+#include "quote.h"
 
 static int grep_source_load(struct grep_source *gs);
 static int grep_source_is_binary(struct grep_source *gs);
@@ -397,6 +398,24 @@ static int is_fixed(const char *s, size_t len)
 	return 1;
 }
 
+static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
+{
+	struct strbuf sb = STRBUF_INIT;
+	int err;
+
+	basic_regex_quote_buf(&sb, p->pattern);
+	err = regcomp(&p->regexp, sb.buf, opt->regflags & ~REG_EXTENDED);
+	if (opt->debug)
+		fprintf(stderr, "fixed%s\n", sb.buf);
+	strbuf_release(&sb);
+	if (err) {
+		char errbuf[1024];
+		regerror(err, &p->regexp, errbuf, 1024);
+		regfree(&p->regexp);
+		compile_regexp_failed(p, errbuf);
+	}
+}
+
 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 {
 	int icase_non_ascii;
@@ -411,7 +430,11 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 	if (!icase_non_ascii && is_fixed(p->pattern, p->patternlen))
 		p->fixed = 1;
 	else if (opt->fixed) {
-		p->fixed = 1;
+		p->fixed = !icase_non_ascii;
+		if (!p->fixed) {
+			compile_fixed_regexp(p, opt);
+			return;
+		}
 	} else
 		p->fixed = 0;
 
diff --git a/quote.c b/quote.c
index 7920e18..43a8057 100644
--- a/quote.c
+++ b/quote.c
@@ -439,3 +439,40 @@ void tcl_quote_buf(struct strbuf *sb, const char *src)
 	}
 	strbuf_addch(sb, '"');
 }
+
+void basic_regex_quote_buf(struct strbuf *sb, const char *src)
+{
+	char c;
+
+	if (*src == '^') {
+		/* only beginning '^' is special and needs quoting */
+		strbuf_addch(sb, '\\');
+		strbuf_addch(sb, *src++);
+	}
+	if (*src == '*')
+		/* beginning '*' is not special, no quoting */
+		strbuf_addch(sb, *src++);
+
+	while ((c = *src++)) {
+		switch (c) {
+		case '[':
+		case '.':
+		case '\\':
+		case '*':
+			strbuf_addch(sb, '\\');
+			strbuf_addch(sb, c);
+			break;
+
+		case '$':
+			/* only the end '$' is special and needs quoting */
+			if (*src == '\0')
+				strbuf_addch(sb, '\\');
+			strbuf_addch(sb, c);
+			break;
+
+		default:
+			strbuf_addch(sb, c);
+			break;
+		}
+	}
+}
diff --git a/quote.h b/quote.h
index 99e04d3..362d315 100644
--- a/quote.h
+++ b/quote.h
@@ -67,5 +67,6 @@ extern char *quote_path_relative(const char *in, const char *prefix,
 extern void perl_quote_buf(struct strbuf *sb, const char *src);
 extern void python_quote_buf(struct strbuf *sb, const char *src);
 extern void tcl_quote_buf(struct strbuf *sb, const char *src);
+extern void basic_regex_quote_buf(struct strbuf *sb, const char *src);
 
 #endif
diff --git a/t/t7812-grep-icase-non-ascii.sh b/t/t7812-grep-icase-non-ascii.sh
index 63a2630..c945589 100755
--- a/t/t7812-grep-icase-non-ascii.sh
+++ b/t/t7812-grep-icase-non-ascii.sh
@@ -16,4 +16,30 @@ test_expect_success GETTEXT_LOCALE 'grep literal string, no -F' '
 	git grep -i "TILRAUN: HALLÓ HEIMUR!"
 '
 
+test_expect_success GETTEXT_LOCALE 'grep literal string, with -F' '
+	git grep --debug -i -F "TILRAUN: Halló Heimur!"  2>&1 >/dev/null |
+		 grep fixed >debug1 &&
+	echo "fixedTILRAUN: Halló Heimur!" >expect1 &&
+	test_cmp expect1 debug1 &&
+
+	git grep --debug -i -F "TILRAUN: HALLÓ HEIMUR!"  2>&1 >/dev/null |
+		 grep fixed >debug2 &&
+	echo "fixedTILRAUN: HALLÓ HEIMUR!" >expect2 &&
+	test_cmp expect2 debug2
+'
+
+test_expect_success GETTEXT_LOCALE 'grep string with regex, with -F' '
+	printf "^*TILR^AUN:.* \\Halló \$He[]imur!\$" >file &&
+
+	git grep --debug -i -F "^*TILR^AUN:.* \\Halló \$He[]imur!\$" 2>&1 >/dev/null |
+		 grep fixed >debug1 &&
+	echo "fixed\\^*TILR^AUN:\\.\\* \\\\Halló \$He\\[]imur!\\\$" >expect1 &&
+	test_cmp expect1 debug1 &&
+
+	git grep --debug -i -F "^*TILR^AUN:.* \\HALLÓ \$HE[]IMUR!\$"  2>&1 >/dev/null |
+		 grep fixed >debug2 &&
+	echo "fixed\\^*TILR^AUN:\\.\\* \\\\HALLÓ \$HE\\[]IMUR!\\\$" >expect2 &&
+	test_cmp expect2 debug2
+'
+
 test_done
-- 
2.3.0.rc1.137.g477eb31
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 17 of 53 in “Git grep does not support multi-byte characters (like UTF-8)”
  1. Plamen TotevJul 6, 2015
  2. Duy NguyenJul 6, 2015
  3. Plamen TotevJul 7, 2015
  4. Duy NguyenJul 7, 2015
  5. Junio C HamanoJul 7, 2015
  6. Plamen TotevJul 7, 2015
  7. Duy NguyenJul 8, 2015
  8. Junio C HamanoJul 8, 2015
  9. grep: use regcomp() for icase search with non-ascii patternsNguyễn Thái Ngọc Duy, Jul 6, 2015
  10. René ScharfeJul 6, 2015
  11. Duy NguyenJul 6, 2015
  12. Plamen TotevJul 7, 2015
  13. 0/9 icase match on non-asciiNguyễn Thái Ngọc Duy, Jul 8, 2015
  14. 1/9 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Jul 8, 2015
  15. 2/9 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Jul 8, 2015
  16. 3/9 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Jul 8, 2015
  17. 4/9 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Jul 8, 2015
  18. 5/9 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Jul 8, 2015
  19. Duy NguyenJul 8, 2015
  20. 6/9 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Jul 8, 2015
  21. 7/9 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Jul 8, 2015
  22. Plamen TotevJul 11, 2015
  23. 8/9 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Jul 8, 2015
  24. 9/9 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Jul 8, 2015
  25. Eric SunshineJul 9, 2015
  26. Torsten BögershausenJul 8, 2015
  27. Duy NguyenJul 8, 2015
  28. Junio C HamanoJul 8, 2015
  29. Duy NguyenJul 8, 2015
  30. 0/9 icase match on non-asciiNguyễn Thái Ngọc Duy, Jul 14, 2015
  31. 1/9 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Jul 14, 2015
  32. 2/9 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Jul 14, 2015
  33. 3/9 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Jul 14, 2015
  34. 4/9 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Jul 14, 2015
  35. 5/9 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Jul 14, 2015
  36. 6/9 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Jul 14, 2015
  37. 7/9 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Jul 14, 2015
  38. 8/9 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Jul 14, 2015
  39. 9/9 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Jul 14, 2015
  40. Torsten BögershausenJul 14, 2015
  41. Duy NguyenJul 15, 2015
  42. Torsten BögershausenJul 15, 2015
  43. 00/10 icase match on non-asciiNguyễn Thái Ngọc Duy, Aug 21, 2015
  44. 01/10 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Aug 21, 2015
  45. 02/10 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Aug 21, 2015
  46. 03/10 test-regex: expose full regcomp() to the command lineNguyễn Thái Ngọc Duy, Aug 21, 2015
  47. 04/10 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Aug 21, 2015
  48. 05/10 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Aug 21, 2015
  49. 06/10 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Aug 21, 2015
  50. 07/10 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Aug 21, 2015
  51. 08/10 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Aug 21, 2015
  52. 09/10 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Aug 21, 2015
  53. 10/10 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Aug 21, 2015

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.