Skip to contentSkip to conversation

grep: refactor next_match() and match_one_pattern() for external use

PATCH In review Hamza Mahfooz 6 participants 47 messages Oct 15, 2021 – Nov 19, 2021 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v13 1/3 grep: refactor next_match() and match_one_pattern() for external use

These changes are made in preparation of, the colorization support for the "git log" subcommands that, rely on regex functionality (i.e. "--author", "--committer" and "--grep"). These changes are necessary primarily because match_one_pattern() expects header lines to be prefixed, however, in pretty, the prefixes are stripped from the lines because the name-email pairs need to go through additional parsing, before they can be printed and because next_match() doesn't handle the case of
"ctx == GREP_CONTEXT_HEAD" at all. So, teach next_match() how to handle the new case and move match_one_pattern()'s core logic to headerless_match_one_pattern() while preserving match_one_pattern()'s uses that depend on the additional processing.

---
v5: separate grep changes from pretty changes.

v6: rescope some variables.

v7: export header_field[] and allow for subsequent matches on header lines
in match_one_pattern().

v8: add headerless_match_one_pattern() and move header_field[] back.

v9: get rid of the new check in headerless_match_one_pattern(), move the
header pattern filtering logic in grep_next_match() and document grep_next_match() in grep.h.

v10: add a "magic" comment in grep_next_match() to signify a fall through
in the switch statement.

---
 grep.c | 79 ++++++++++++++++++++++++++++++++++++----------------------
 grep.h |  9 +++++++
 2 files changed, 58 insertions(+), 30 deletions(-)
grep.c +49 -30
index 14fe8a0fd2..fe847a0111 100644
@@ -944,10 +944,10 @@ static struct {
 	{ "reflog ", 7 },
 };
 
-static int match_one_pattern(struct grep_pat *p,
-			     const char *bol, const char *eol,
-			     enum grep_context ctx,
-			     regmatch_t *pmatch, int eflags)
+static int headerless_match_one_pattern(struct grep_pat *p,
+					const char *bol, const char *eol,
+					enum grep_context ctx,
+					regmatch_t *pmatch, int eflags)
 {
 	int hit = 0;
 	const char *start = bol;
@@ -956,25 +956,6 @@ static int match_one_pattern(struct grep_pat *p,
 	    ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
 		return 0;
 
-	if (p->token == GREP_PATTERN_HEAD) {
-		const char *field;
-		size_t len;
-		assert(p->field < ARRAY_SIZE(header_field));
-		field = header_field[p->field].field;
-		len = header_field[p->field].len;
-		if (strncmp(bol, field, len))
-			return 0;
-		bol += len;
-		switch (p->field) {
-		case GREP_HEADER_AUTHOR:
-		case GREP_HEADER_COMMITTER:
-			strip_timestamp(bol, &eol);
-			break;
-		default:
-			break;
-		}
-	}
-
  again:
 	hit = patmatch(p, bol, eol, pmatch, eflags);
 
@@ -1025,6 +1006,36 @@ static int match_one_pattern(struct grep_pat *p,
 	return hit;
 }
 
+static int match_one_pattern(struct grep_pat *p,
+			     const char *bol, const char *eol,
+			     enum grep_context ctx, regmatch_t *pmatch,
+			     int eflags)
+{
+	const char *field;
+	size_t len;
+
+	if (p->token == GREP_PATTERN_HEAD) {
+		assert(p->field < ARRAY_SIZE(header_field));
+		field = header_field[p->field].field;
+		len = header_field[p->field].len;
+		if (strncmp(bol, field, len))
+			return 0;
+		bol += len;
+
+		switch (p->field) {
+		case GREP_HEADER_AUTHOR:
+		case GREP_HEADER_COMMITTER:
+			strip_timestamp(bol, &eol);
+			break;
+		default:
+			break;
+		}
+	}
+
+	return headerless_match_one_pattern(p, bol, eol, ctx, pmatch, eflags);
+}
+
+
 static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x,
 			   const char *bol, const char *eol,
 			   enum grep_context ctx, ssize_t *col,
@@ -1143,7 +1154,7 @@ static int match_next_pattern(struct grep_pat *p,
 {
 	regmatch_t match;
 
-	if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
+	if (!headerless_match_one_pattern(p, bol, eol, ctx, &match, eflags))
 		return 0;
 	if (match.rm_so < 0 || match.rm_eo < 0)
 		return 0;
@@ -1158,19 +1169,26 @@ static int match_next_pattern(struct grep_pat *p,
 	return 1;
 }
 
-static int next_match(struct grep_opt *opt,
-		      const char *bol, const char *eol,
-		      enum grep_context ctx, regmatch_t *pmatch, int eflags)
+int grep_next_match(struct grep_opt *opt,
+		    const char *bol, const char *eol,
+		    enum grep_context ctx, regmatch_t *pmatch,
+		    enum grep_header_field field, int eflags)
 {
 	struct grep_pat *p;
 	int hit = 0;
 
 	pmatch->rm_so = pmatch->rm_eo = -1;
 	if (bol < eol) {
-		for (p = opt->pattern_list; p; p = p->next) {
+		for (p = ((ctx == GREP_CONTEXT_HEAD)
+			   ? opt->header_list : opt->pattern_list);
+			  p; p = p->next) {
 			switch (p->token) {
-			case GREP_PATTERN: /* atom */
 			case GREP_PATTERN_HEAD:
+				if ((field != GREP_HEADER_FIELD_MAX) &&
+				    (p->field != field))
+					continue;
+				/* fall thru */
+			case GREP_PATTERN: /* atom */
 			case GREP_PATTERN_BODY:
 				hit |= match_next_pattern(p, bol, eol, ctx,
 							  pmatch, eflags);
@@ -1261,7 +1279,8 @@ static void show_line(struct grep_opt *opt,
 			else if (sign == '=')
 				line_color = opt->colors[GREP_COLOR_FUNCTION];
 		}
-		while (next_match(opt, bol, eol, ctx, &match, eflags)) {
+		while (grep_next_match(opt, bol, eol, ctx, &match,
+				       GREP_HEADER_FIELD_MAX, eflags)) {
 			if (match.rm_so == match.rm_eo)
 				break;
 
grep.h +9 -0
index 3cb8a83ae8..808ad76f0c 100644
@@ -191,6 +191,15 @@ void compile_grep_patterns(struct grep_opt *opt);
 void free_grep_patterns(struct grep_opt *opt);
 int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size);
 
+/* The field parameter is only used to filter header patterns
+ * (where appropriate). If filtering isn't desirable
+ * GREP_HEADER_FIELD_MAX should be supplied.
+ */
+int grep_next_match(struct grep_opt *opt,
+		    const char *bol, const char *eol,
+		    enum grep_context ctx, regmatch_t *pmatch,
+		    enum grep_header_field field, int eflags);
+
 struct grep_source {
 	char *name;
 
Signature · 1 line
2.33.0
#2 lore raw

PATCH v13 3/3 grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

If we attempt to grep non-ascii log message text with an ascii pattern, we run into the following issue:

    $ git log --color --author='.var.*Bjar' -1 origin/master | grep ^Author
    grep: (standard input): binary file matches

So, to fix this teach the grep code to use PCRE2_UTF, as long as the log output is encoded in UTF-8.

---
v12: get rid of utf8_all_the_things and fix an issue with one of the unit
tests.

v13: make some clarifications in the commit message.

---
 grep.c                          |  6 +++--
 t/t7812-grep-icase-non-ascii.sh | 48 +++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 2 deletions(-)
grep.c +4 -2
index fe847a0111..f6e113e9f0 100644
@@ -382,8 +382,10 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		}
 		options |= PCRE2_CASELESS;
 	}
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
+	    (!opt->ignore_locale && is_utf8_locale() &&
+	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
+					    (p->fixed || p->is_fixed))))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
 
 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
t/t7812-grep-icase-non-ascii.sh +48 -0

54 lines not on this page · raw message

Signature · 1 line
2.33.0
#1 lore raw

PATCH v13 2/3 pretty: colorize pattern matches in commit messages

In reply to Hamza Mahfooz

The "git log" command limits its output to the commits that contain strings matched by a pattern when the "--grep=<pattern>" option is used, but unlike output from "git grep -e <pattern>", the matches are not highlighted, making them harder to spot.

Teach the pretty-printer code to highlight matches from the "--grep=<pattern>", "--author=<pattern>" and "--committer=<pattern>" options (to view the last one, you may have to ask for --pretty=fuller).

Also, it must be noted that we are effectively greping the content twice (because it would be a hassle to rework the existing matching code to do a /g match and then pass it all down to the coloring code), however it only slows down "git log --author=^H" on this repository by around 1-2% (compared to v2.33.0), so it should be a small enough slow down to justify the addition of the feature.

---
v2: make the commit message whole (add the missing ingredients), rename
append_matched_line() to append_line_with_color(), use colors[GREP_COLOR_MATCH_SELECTED] instead of colors[GREP_COLOR_MATCH_CONTEXT], allow the background color to be customized, don't copy strings to a buffer when not coloring in append_line_with_color(), rename next_match() to grep_next_match(), repurpose grep_next_match()/match_one_pattern() for use in append_line_with_color() (allowing us to remove duplicated matching code in append_line_with_color()), document how to customize the feature and modify some of the tests to fit the feature better.

v3: fix a formatting issue with the added documentation.

v4: add strbuf_add_with_color(), use the correct color code scheme in the
unit tests and add more unit tests.

v5: separate grep changes from pretty changes and add some performance
analysis in the commit message.

v6: put the documentation in the correct place, cleanup pretty.c and
format the unit tests according to the current convention.

v7: get rid of all manual strbuf management, constify where now appropriate
and fix the header line prefix issue properly.

v8: remove code that relies on grep_header_fields[].

v11: clarify why we are grepping twice.

---
 Documentation/config/color.txt |   7 ++-
 pretty.c                       | 101 +++++++++++++++++++++++++++++----
 t/t4202-log.sh                 |  51 +++++++++++++++++
 3 files changed, 145 insertions(+), 14 deletions(-)
Documentation/config/color.txt +5 -2
index e05d520a86..91d9a9da32 100644
@@ -104,9 +104,12 @@ color.grep.<slot>::
 `matchContext`;;
 	matching text in context lines
 `matchSelected`;;
-	matching text in selected lines
+	matching text in selected lines. Also, used to customize the following
+	linkgit:git-log[1] subcommands: `--grep`, `--author` and `--committer`.
 `selected`;;
-	non-matching text in selected lines
+	non-matching text in selected lines. Also, used to customize the
+	following linkgit:git-log[1] subcommands: `--grep`, `--author` and
+	`--committer`.
 `separator`;;
 	separators between fields on a line (`:`, `-`, and `=`)
 	and between hunks (`--`)
pretty.c +89 -12

158 lines not on this page · raw message

t/t4202-log.sh +51 -0

57 lines not on this page · raw message

Signature · 1 line
2.33.0
#42 lore raw
In reply to Hamza Mahfooz
Quoted 21 lines · Hamza Mahfooz

These changes are made in preparation of, the colorization support for the "git log" subcommands that, rely on regex functionality (i.e. "--author", "--committer" and "--grep"). These changes are necessary primarily because match_one_pattern() expects header lines to be prefixed, however, in pretty, the prefixes are stripped from the lines because the name-email pairs need to go through additional parsing, before they can be printed and because next_match() doesn't handle the case of
"ctx == GREP_CONTEXT_HEAD" at all. So, teach next_match() how to handle the new case and move match_one_pattern()'s core logic to headerless_match_one_pattern() while preserving match_one_pattern()'s uses that depend on the additional processing.

---
v5: separate grep changes from pretty changes.

v6: rescope some variables.

v7: export header_field[] and allow for subsequent matches on header lines
in match_one_pattern().

Quoted 8 lines

v8: add headerless_match_one_pattern() and move header_field[] back.

v9: get rid of the new check in headerless_match_one_pattern(), move the
header pattern filtering logic in grep_next_match() and document grep_next_match() in grep.h.

v10: add a "magic" comment in grep_next_match() to signify a fall through
in the switch statement.

Makes readers curious what happend in v11 and later...

#43 lore raw
Quoted 1 line · Junio C Hamano

Makes readers curious what happend in v11 and later...

It was my understanding that no comment means nothing changed since then, or is something to the effect of "no changes" preferable?

#44 lore raw
Quoted 6 lines · Hamza Mahfooz
Quoted 1 line · Junio C Hamano

Makes readers curious what happend in v11 and later...

It was my understanding that no comment means nothing changed since then, or is something to the effect of "no changes" preferable?

Usually people do not want to spam the list with a multi-patch series when they have nothing new to show, unless there are other reasons to do so, like "there is no change, but I am sending it again because nobody seemed to have time reviewing the series the last time", in which case that would make a good explanation to put there.

#45 lore raw
Quoted 7 lines · Junio C Hamano

Usually people do not want to spam the list with a multi-patch series when they have nothing new to show, unless there are other reasons to do so, like "there is no change, but I am sending it again because nobody seemed to have time reviewing the series the last time", in which case that would make a good explanation to put there.

I made changes to other patches in the series however, (I only document the changes if that patch in particular has changed.)

#46 lore raw
In reply to Junio C Hamano
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Hamza Mahfooz
Quoted 1 line · Junio C Hamano

Makes readers curious what happend in v11 and later...

It was my understanding that no comment means nothing changed since then, or is something to the effect of "no changes" preferable?

Quoted 6 lines

Usually people do not want to spam the list with a multi-patch series when they have nothing new to show, unless there are other reasons to do so, like "there is no change, but I am sending it again because nobody seemed to have time reviewing the series the last time", in which case that would make a good explanation to put there.

Ah, I thought I was responding to a cover letter. OK, so you were saying that there was no change in 1/3, but other steps may be different.

In that case, that makes sense (even though it is more helpful to just say "v13: same as v12" or something).

Thanks.

#3 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Hamza Mahfooz
Quoted 1 line · Hamza Mahfooz

If we attempt to grep non-ascii log message text with an ascii pattern, we

"with an ascii pattern, when Git is built with and told to use pcre2, we"

Quoted 8 lines

run into the following issue:

    $ git log --color --author='.var.*Bjar' -1 origin/master | grep ^Author
    grep: (standard input): binary file matches

So, to fix this teach the grep code to use PCRE2_UTF, as long as the log output is encoded in UTF-8.

grep.c static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt · line 388

Quoted 6 lines
grep.c +4 -2
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
+	    (!opt->ignore_locale && is_utf8_locale() &&
+	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
+					    (p->fixed || p->is_fixed))))

That's a mouthful. It is not obvious what new condition is being added. I had to flip the order to see the only difference is, that

Review

Quoted 5 lines
(quoted diff) +3 -2
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed))) ||
+	    (!opt->ignore_locale && !has_non_ascii(p->pattern)))

... in addition to the case where the original condition holds, if we do not say "ignore locale" and the pattern is ascii-only, we apply these two option flags. And that matches what the proposed log message explained as the condition the problem appears.

So,... looks good, I guess.

Thanks, will queue.

Addendum.

If we were reordering pieces in the condition, I wonder if there is a better way to reorganize it, though. The original is already barely explainable with words, and with this new condition added, I am not sure if anybody can phrase the condition in simple words to others after staring it for a few minutes. I can't.

But straightening it out is best left as a future clean-up patch, separate from this series.

#4 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Am 15.10.21 um 22:03 schrieb Junio C Hamano:

Quoted 10 lines
Quoted 1 line · Hamza Mahfooz

If we attempt to grep non-ascii log message text with an ascii pattern, we

"with an ascii pattern, when Git is built with and told to use pcre2, we"

Quoted 4 lines

run into the following issue:

    $ git log --color --author='.var.*Bjar' -1 origin/master | grep ^Author
    grep: (standard input): binary file matches

I get no error message on macOS 11.6, but this result, with the underlined part in red:

Author: ??var Arnfjörð Bjarmason <[email protected]>

            ^^^^^^^^^^^^^^^^^^

So the pattern matches the second byte of a two-byte character, inserts a color code in the middle and thus produces invalid output in this case.

Quoted 32 lines

grep.c static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt · line 388

Quoted 10 lines

So, to fix this teach the grep code to use PCRE2_UTF, as long as the log output is encoded in UTF-8.

grep.c +4 -2
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
+	    (!opt->ignore_locale && is_utf8_locale() &&
+	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
+					    (p->fixed || p->is_fixed))))

That's a mouthful. It is not obvious what new condition is being added. I had to flip the order to see the only difference is, that

Review

Quoted 5 lines
(quoted diff) +3 -2
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed))) ||
+	    (!opt->ignore_locale && !has_non_ascii(p->pattern)))

... in addition to the case where the original condition holds, if we do not say "ignore locale" and the pattern is ascii-only, we apply these two option flags. And that matches what the proposed log message explained as the condition the problem appears.

So,... looks good, I guess.

Thanks, will queue.

Addendum.

Quoted 9 lines

If we were reordering pieces in the condition, I wonder if there is a better way to reorganize it, though. The original is already barely explainable with words, and with this new condition added, I am not sure if anybody can phrase the condition in simple words to others after staring it for a few minutes. I can't.

But straightening it out is best left as a future clean-up patch, separate from this series.

It can be written as:

	literal = !opt->ignore_case && (p->fixed || p->is_fixed);
	if (!opt->ignore_locale) {
		if (!has_non_ascii(p->pattern) ||
		    (is_utf8_locale() && !literal))
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}

Literal patterns are those that don't use any wildcards or case-folding. If the text is encoded in UTF-8 then we enable PCRE2_UTF either if the pattern only consists of ASCII characters, or if the pattern is encoded in UTF-8 and is not just a literal pattern.

Hmm. Why enable PCRE2_UTF for literal patterns that consist of only ASCII chars?

The old condition was (reformatted to better match the new one):

	if (!opt->ignore_locale) {
		if (is_utf8_locale() && has_non_ascii(p->pattern) && !literal)
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}

Intuitively I'd say the condition should be:

	if (!opt->ignore_locale && is_utf8_locale()) {
		if (has_non_ascii(p->pattern) || !literal)
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}

If both input and pattern are encoded in UTF-8, enable PCRE2_UTF if we have to match non-ASCII characters or do more than just literal matching.

For literal patterns that consist only of ASCII characters we don't need the cost and complication of PCRE2_UTF.

Makes sense?

René

#5 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 12 lines · René Scharfe

Am 15.10.21 um 22:03 schrieb Junio C Hamano:

Quoted 10 lines
Quoted 1 line · Hamza Mahfooz

If we attempt to grep non-ascii log message text with an ascii pattern, we

"with an ascii pattern, when Git is built with and told to use pcre2, we"

Quoted 4 lines

run into the following issue:

    $ git log --color --author='.var.*Bjar' -1 origin/master | grep ^Author
    grep: (standard input): binary file matches
Quoted 8 lines

I get no error message on macOS 11.6, but this result, with the underlined part in red:

Author: ??var Arnfjörð Bjarmason <[email protected]>

            ^^^^^^^^^^^^^^^^^^

So the pattern matches the second byte of a two-byte character, inserts a color code in the middle and thus produces invalid output in this case.

Thanks for digging into these edge cases...

Quoted 42 lines
Quoted 32 lines

grep.c static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt · line 388

Quoted 10 lines

So, to fix this teach the grep code to use PCRE2_UTF, as long as the log output is encoded in UTF-8.

grep.c +4 -2
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
+	    (!opt->ignore_locale && is_utf8_locale() &&
+	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
+					    (p->fixed || p->is_fixed))))

That's a mouthful. It is not obvious what new condition is being added. I had to flip the order to see the only difference is, that

Review

Quoted 5 lines
(quoted diff) +3 -2
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed))) ||
+	    (!opt->ignore_locale && !has_non_ascii(p->pattern)))

... in addition to the case where the original condition holds, if we do not say "ignore locale" and the pattern is ascii-only, we apply these two option flags. And that matches what the proposed log message explained as the condition the problem appears.

So,... looks good, I guess.

Thanks, will queue.

Addendum.

Quoted 9 lines

If we were reordering pieces in the condition, I wonder if there is a better way to reorganize it, though. The original is already barely explainable with words, and with this new condition added, I am not sure if anybody can phrase the condition in simple words to others after staring it for a few minutes. I can't.

But straightening it out is best left as a future clean-up patch, separate from this series.

Quoted 8 lines

It can be written as:

	literal = !opt->ignore_case && (p->fixed || p->is_fixed);
	if (!opt->ignore_locale) {
		if (!has_non_ascii(p->pattern) ||
		    (is_utf8_locale() && !literal))
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}

Whatever we go from here I'm very much for untangling that condition, but I guess it can be done as a follow-up too, I'll defer to Hamza there...

Quoted 22 lines

Literal patterns are those that don't use any wildcards or case-folding. If the text is encoded in UTF-8 then we enable PCRE2_UTF either if the pattern only consists of ASCII characters, or if the pattern is encoded in UTF-8 and is not just a literal pattern.

Hmm. Why enable PCRE2_UTF for literal patterns that consist of only ASCII chars?

The old condition was (reformatted to better match the new one):

	if (!opt->ignore_locale) {
		if (is_utf8_locale() && has_non_ascii(p->pattern) && !literal)
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}

Intuitively I'd say the condition should be:

	if (!opt->ignore_locale && is_utf8_locale()) {
		if (has_non_ascii(p->pattern) || !literal)
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}
Quoted 8 lines

If both input and pattern are encoded in UTF-8, enable PCRE2_UTF if we have to match non-ASCII characters or do more than just literal matching.

For literal patterns that consist only of ASCII characters we don't need the cost and complication of PCRE2_UTF.

Makes sense?

    echo 'René Scharfe' >f &&
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?Ren. S' f; echo $?
    1
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?R[eé]n. S' f; echo $?
    f:René Scharfe
    0

So it's a choose-your-own adventure where you can pick if you're right. I.e. do you want the "." metacharacter to match your "é" or not?

These sorts of patterns demonstrate nicely that the relationship between your pattern being ASCII and wanting or not wanting UTF-8 matching semantics isn't what you might imagine it to be.

If you look at:

    git log --reverse -p -Gis_utf8_locale -- grep.c

And my earlier replies in this thread-at-large (not digging up a reference now, sorry), you'll see that the current behavior in grep.c is really a compromise based on some intersection of user patterns, us potentially grepping arbitrary binary data and/or "valid" encodings, and what PCRE does and doesn't puke on.

It's not "right" by any sense, but we sort of limp along with it, mostly because unlike say Perl's regex engine which really is used for serious production work where Unicode-correctness matters I don't think anyone is using "git grep" for anything like that, it's mostly for people's ad-hoc greps.

Right now I can't remember if the only reason we can't "fix this" is because we'd be too aggressive with the PCRE version dependency, see 95ca1f987ed (grep/pcre2: better support invalid UTF-8 haystacks, 2021-01-24).

#6 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Am 16.10.21 um 19:12 schrieb Ævar Arnfjörð Bjarmason:

Quoted 119 lines · René Scharfe
Quoted 13 lines

Am 15.10.21 um 22:03 schrieb Junio C Hamano:

Quoted 10 lines
Quoted 1 line · Hamza Mahfooz

If we attempt to grep non-ascii log message text with an ascii pattern, we

"with an ascii pattern, when Git is built with and told to use pcre2, we"

Quoted 4 lines

run into the following issue:

    $ git log --color --author='.var.*Bjar' -1 origin/master | grep ^Author
    grep: (standard input): binary file matches
Quoted 8 lines

I get no error message on macOS 11.6, but this result, with the underlined part in red:

Author: ??var Arnfjörð Bjarmason <[email protected]>

            ^^^^^^^^^^^^^^^^^^

So the pattern matches the second byte of a two-byte character, inserts a color code in the middle and thus produces invalid output in this case.

Thanks for digging into these edge cases...

Quoted 42 lines
Quoted 32 lines

grep.c static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt · line 388

Quoted 10 lines

So, to fix this teach the grep code to use PCRE2_UTF, as long as the log output is encoded in UTF-8.

grep.c +4 -2
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
+	    (!opt->ignore_locale && is_utf8_locale() &&
+	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
+					    (p->fixed || p->is_fixed))))

That's a mouthful. It is not obvious what new condition is being added. I had to flip the order to see the only difference is, that

Review

Quoted 5 lines
(quoted diff) +3 -2
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	if ((!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed))) ||
+	    (!opt->ignore_locale && !has_non_ascii(p->pattern)))

... in addition to the case where the original condition holds, if we do not say "ignore locale" and the pattern is ascii-only, we apply these two option flags. And that matches what the proposed log message explained as the condition the problem appears.

So,... looks good, I guess.

Thanks, will queue.

Addendum.

Quoted 9 lines

If we were reordering pieces in the condition, I wonder if there is a better way to reorganize it, though. The original is already barely explainable with words, and with this new condition added, I am not sure if anybody can phrase the condition in simple words to others after staring it for a few minutes. I can't.

But straightening it out is best left as a future clean-up patch, separate from this series.

Quoted 8 lines

It can be written as:

	literal = !opt->ignore_case && (p->fixed || p->is_fixed);
	if (!opt->ignore_locale) {
		if (!has_non_ascii(p->pattern) ||
		    (is_utf8_locale() && !literal))
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}

Whatever we go from here I'm very much for untangling that condition, but I guess it can be done as a follow-up too, I'll defer to Hamza there...

Quoted 22 lines

Literal patterns are those that don't use any wildcards or case-folding. If the text is encoded in UTF-8 then we enable PCRE2_UTF either if the pattern only consists of ASCII characters, or if the pattern is encoded in UTF-8 and is not just a literal pattern.

Hmm. Why enable PCRE2_UTF for literal patterns that consist of only ASCII chars?

The old condition was (reformatted to better match the new one):

	if (!opt->ignore_locale) {
		if (is_utf8_locale() && has_non_ascii(p->pattern) && !literal)
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}

Intuitively I'd say the condition should be:

	if (!opt->ignore_locale && is_utf8_locale()) {
		if (has_non_ascii(p->pattern) || !literal)
			options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
	}
Quoted 8 lines

If both input and pattern are encoded in UTF-8, enable PCRE2_UTF if we have to match non-ASCII characters or do more than just literal matching.

For literal patterns that consist only of ASCII characters we don't need the cost and complication of PCRE2_UTF.

Makes sense?

    echo 'René Scharfe' >f &&
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?Ren. S' f; echo $?
    1
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?R[eé]n. S' f; echo $?
    f:René Scharfe
    0
Quoted 2 lines

So it's a choose-your-own adventure where you can pick if you're right. I.e. do you want the "." metacharacter to match your "é" or not?

Yes, I do, and it's what Hamza's patch is fixing.

Quoted 3 lines

These sorts of patterns demonstrate nicely that the relationship between your pattern being ASCII and wanting or not wanting UTF-8 matching semantics isn't what you might imagine it to be.

Differences are:

o: opt->ignore_locale
h: has_non_ascii(p->pattern)
i: is_utf8_locale()
l: literal

o h i l master hamza rene

0 0 0 0      0     1    0
0 0 0 1      0     1    0
0 0 1 0      0     1    1   <== your first example
0 0 1 1      0     1    0
0 1 1 1      0     0    1

Turning on PCRE2_UTF when is_utf8_locale() == 0 seems wrong (first two lines).

Turning on PCRE2_UTF for literal matching (fourth line) goes against 870eea8166 (grep: do not enter PCRE2_UTF mode on fixed matching, 2019-07-26).

Turning on PCRE2_UTF for literal matching of non-ASCII characters (fifth line) also goes against that, so my intuition betrayed me. When I adjust it, I get:

	if (!opt->ignore_locale && is_utf8_locale() && !literal)
		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);

That looks deceptively simple -- just drop has_non_ascii(p->pattern) from the original condition.

Your second example is handle the same by all versions btw.:

o h i l master hamza rene
0 1 1 0 1 1 1

René

#7 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 15 lines · René Scharfe
Quoted 15 lines
Quoted 8 lines

Literal patterns are those that don't use any wildcards or case-folding. If the text is encoded in UTF-8 then we enable PCRE2_UTF either if the pattern only consists of ASCII characters, or if the pattern is encoded in UTF-8 and is not just a literal pattern.

Hmm. Why enable PCRE2_UTF for literal patterns that consist of only ASCII chars?
...

    echo 'René Scharfe' >f &&
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?Ren. S' f; echo $?
    1
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?R[eé]n. S' f; echo $?
    f:René Scharfe
    0
Quoted 4 lines
Quoted 2 lines

So it's a choose-your-own adventure where you can pick if you're right. I.e. do you want the "." metacharacter to match your "é" or not?

Yes, I do, and it's what Hamza's patch is fixing.

That may be correct but is this discussion still about "Why enable ... for literal patterns that consist of only ASCII"? Calling "." a "metacharacter" and wanting it to match anything other than a single dot would mean the pattern we are discussing is no longer "literal", isn't it? I am puzzled.

Quoted 41 lines
Quoted 3 lines

These sorts of patterns demonstrate nicely that the relationship between your pattern being ASCII and wanting or not wanting UTF-8 matching semantics isn't what you might imagine it to be.

Differences are:

o: opt->ignore_locale
h: has_non_ascii(p->pattern)
i: is_utf8_locale()
l: literal

o h i l master hamza rene

0 0 0 0      0     1    0
0 0 0 1      0     1    0
0 0 1 0      0     1    1   <== your first example
0 0 1 1      0     1    0
0 1 1 1      0     0    1

Turning on PCRE2_UTF when is_utf8_locale() == 0 seems wrong (first two lines).

Turning on PCRE2_UTF for literal matching (fourth line) goes against 870eea8166 (grep: do not enter PCRE2_UTF mode on fixed matching, 2019-07-26).

Turning on PCRE2_UTF for literal matching of non-ASCII characters (fifth line) also goes against that, so my intuition betrayed me. When I adjust it, I get:

	if (!opt->ignore_locale && is_utf8_locale() && !literal)
		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);

That looks deceptively simple -- just drop has_non_ascii(p->pattern) from the original condition.

Your second example is handle the same by all versions btw.:

o h i l master hamza rene
0 1 1 0 1 1 1

René

#8 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Am 17.10.21 um 08:00 schrieb Junio C Hamano:

Quoted 20 lines
Quoted 18 lines · René Scharfe
Quoted 17 lines
Quoted 8 lines

Literal patterns are those that don't use any wildcards or case-folding. If the text is encoded in UTF-8 then we enable PCRE2_UTF either if the pattern only consists of ASCII characters, or if the pattern is encoded in UTF-8 and is not just a literal pattern.

Hmm. Why enable PCRE2_UTF for literal patterns that consist of only ASCII chars?
...

    echo 'René Scharfe' >f &&
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?Ren. S' f; echo $?
    1
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?R[eé]n. S' f; echo $?
    f:René Scharfe
    0

So it's a choose-your-own adventure where you can pick if you're right. I.e. do you want the "." metacharacter to match your "é" or not?

Quoted 7 lines
Quoted 1 line

Yes, I do, and it's what Hamza's patch is fixing.

That may be correct but is this discussion still about "Why enable ... for literal patterns that consist of only ASCII"? Calling "." a "metacharacter" and wanting it to match anything other than a single dot would mean the pattern we are discussing is no longer "literal", isn't it? I am puzzled.

Right, Ævar's comment is not about my question, but highlights an inconsistency in master that is fixed by Hamza's patch.

I'll repeat and extend my question: Hamza's patch enables PCRE2_UTF for non-ASCII patterns even if they are literal or our locale is not UTF-8. The following change would fix the edge case mentioned in its commit message without these side-effects. Am I correct?

grep.c +1 -1
index fe847a0111..5badb6d851 100644
@@ -382,7 +382,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		}
 		options |= PCRE2_CASELESS;
 	}
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	if (!opt->ignore_locale && is_utf8_locale() &&
 	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
 
#9 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 23 lines · René Scharfe

Am 17.10.21 um 08:00 schrieb Junio C Hamano:

Quoted 22 lines
Quoted 19 lines · René Scharfe
Quoted 15 lines
Quoted 8 lines

Literal patterns are those that don't use any wildcards or case-folding. If the text is encoded in UTF-8 then we enable PCRE2_UTF either if the pattern only consists of ASCII characters, or if the pattern is encoded in UTF-8 and is not just a literal pattern.

Hmm. Why enable PCRE2_UTF for literal patterns that consist of only ASCII chars?
...

    echo 'René Scharfe' >f &&
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?Ren. S' f; echo $?
    1
    $ git -P grep --no-index -P '^(?:You are (?:wrong|correct), )?R[eé]n. S' f; echo $?
    f:René Scharfe
    0
Quoted 2 lines

So it's a choose-your-own adventure where you can pick if you're right. I.e. do you want the "." metacharacter to match your "é" or not?

Yes, I do, and it's what Hamza's patch is fixing.

Quoted 8 lines
Quoted 5 lines

That may be correct but is this discussion still about "Why enable ... for literal patterns that consist of only ASCII"? Calling "." a "metacharacter" and wanting it to match anything other than a single dot would mean the pattern we are discussing is no longer "literal", isn't it? I am puzzled.

Right, Ævar's comment is not about my question, but highlights an inconsistency in master that is fixed by Hamza's patch.

Yes, sorry about that. Just generally about the messy semantics.

Quoted 10 lines

I'll repeat and extend my question: Hamza's patch enables PCRE2_UTF for non-ASCII patterns even if they are literal or our locale is not UTF-8. The following change would fix the edge case mentioned in its commit message without these side-effects. Am I correct?

grep.c +0 -0
index fe847a0111..5badb6d851 100644

grep.c static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt · line 387

Quoted 7 lines
grep.c +1 -1
@@ -382,7 +382,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		}
 		options |= PCRE2_CASELESS;
 	}
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	if (!opt->ignore_locale && is_utf8_locale() &&
 	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);

I haven't had time to carefully look into this, but one caveat to check out is if it works with older PCRE and whether you need e.g. the GIT_PCRE2_VERSION_10_36_OR_HIGHER.

I tried your suggestion on top of Hamza's series, compiled PCRE v2 10.23, tested, and also tried manually removing the PCRE2_MATCH_INVALID_UTF flag and tested again.

We pass all tests with both, so maybe this is safe to do (or maybe we're missing some test we haven't thought of yet...).

One thing that makes me nervous is that we had breakages in the past once the patches escaped into the wild, particularly because the code being modified here has is_utf8_locale(), and our tests run under LANG=c LC_ALL=C.

I tried running all the tests with a non-C locale, there's a lot of failures, but none new with this change. As an aside the below patch makes all but one shortlog test pass for me. I wonder if we shouldn't do this for real to smoke out any $LANG or $LC_ALL dependencies.

I.e. almost all of the failures were due to relying on the sort order of sort(1), and in one case comm(1), the first hunk here is also redundant to defining our own ls(1) wrapper....

t/t5319-multi-pack-index.sh +5 -5
index bd17f308b38..738ca6ef587 100755
@@ -693,12 +693,12 @@ test_expect_success 'expire removes unreferenced packs' '
 		^refs/heads/C
 		EOF
 		git multi-pack-index write &&
-		ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
+		ls .git/objects/pack | sort | grep -v -e pack-[AB] >expect &&
 		git multi-pack-index expire &&
-		ls .git/objects/pack >actual &&
+		ls .git/objects/pack | sort >actual &&
 		test_cmp expect actual &&
-		ls .git/objects/pack/ | grep idx >expect-idx &&
-		test-tool read-midx .git/objects | grep idx >actual-midx &&
+		ls .git/objects/pack/ | sort | grep idx >expect-idx &&
+		test-tool read-midx .git/objects | sort | grep idx >actual-midx &&
 		test_cmp expect-idx actual-midx &&
 		git multi-pack-index verify &&
 		git fsck
@@ -802,7 +802,7 @@ test_expect_success 'expire works when adding new packs' '
 		refs/heads/E
 		EOF
 		git multi-pack-index expire &&
-		ls .git/objects/pack/ | grep idx >expect &&
+		ls .git/objects/pack/ | sort | grep idx >expect &&
 		test-tool read-midx .git/objects | grep idx >actual &&
 		test_cmp expect actual &&
 		git multi-pack-index verify
t/test-lib.sh +10 -2
index 8361b5c1c57..f4f9d231f28 100644
@@ -417,14 +417,22 @@ test -n "$BASH_VERSION" && shopt -u checkwinsize 2>/dev/null
 
 # For repeatability, reset the environment to known value.
 # TERM is sanitized below, after saving color control sequences.
-LANG=C
-LC_ALL=C
+LANG=en_US.UTF-8
+LC_ALL=en_US.UTF-8
 PAGER=cat
 TZ=UTC
 COLUMNS=80
 export LANG LC_ALL PAGER TZ COLUMNS
 EDITOR=:
 
+sort () {
+	LC_ALL=C LANG=C command sort "$@"
+}
+
+comm () {
+	LC_ALL=C LANG=C command comm "$@"
+}
+
 # A call to "unset" with no arguments causes at least Solaris 10
 # /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
 # deriving from the command substitution clustered with the other
#11 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Andreas Schwab
Quoted 1 line · Andreas Schwab

This breaks 'PCRE v2: grep ASCII from invalid UTF-8 data'.

What PCREv2 version are you using? I wonder if it's got to do with the 10.36 boundary, as noted in 95ca1f987ed (grep/pcre2: better support invalid UTF-8 haystacks, 2021-01-24). One of the two tests matching your description tests for that bug.

I can't remember much of the details of the bug, and it seems PCREv2's bug tracker went private, or at least I can't view
https://bugs.exim.org/show_bug.cgi?id=2642 anymore.

#10 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Hamza Mahfooz

This breaks 'PCRE v2: grep ASCII from invalid UTF-8 data'.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
#12 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Ævar Arnfjörð Bjarmason
Quoted 3 lines

I can't remember much of the details of the bug, and it seems PCREv2's bug tracker went private, or at least I can't view
https://bugs.exim.org/show_bug.cgi?id=2642 anymore.

It was shutdown; all new development is now done in github[1], old closed bugs were not migrated over though but the fix is pretty straight forward if it needs backporting[2]

I would have expected the less risky change from René[3] to be released though, but might be a good opportunity to take a deeper look at the underlying problem and also have a likely solution to both the regression and the original bug.

Carlo

[1] https://github.com/PhilipHazel/pcre2
[2] https://github.com/PhilipHazel/pcre2/commit/f8cbb1f58df05f175a6898f35dc18e26be6362d0
[3] https://lore.kernel.org/git/[email protected]/

#13 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Ævar Arnfjörð Bjarmason
Quoted 1 line · Ævar Arnfjörð Bjarmason

What PCREv2 version are you using?

10.31

https://build.opensuse.org/package/show/openSUSE:Leap:15.3:Update/pcre2

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
#14 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 8 lines · Andreas Schwab
Quoted 1 line · Ævar Arnfjörð Bjarmason

What PCREv2 version are you using?

10.31

https://build.opensuse.org/package/show/openSUSE:Leap:15.3:Update/pcre2

interesting; that version doesn't need the workaround because it doesn't have MATCH_INVALID_UTF that was released with 10.34, but it has a recently backported fix for a similar issue that might be confusing git and it is somehow related to it.

a full log (run it with -x -i, and get the last lines) would be useful.

curious also if you see other problems using git log --grep and which locale do you have configured?

Carlo

#15 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 1 line · Carlo Arenas

curious also if you see other problems using git log --grep and

Not sure what you are asking for.

Quoted 1 line

which locale do you have configured?

There is no locale configured during build.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
#16 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Carlo Arenas

expecting success of 7812.13 'PCRE v2: grep ASCII from invalid UTF-8 data':

        git grep -h "var" invalid-0x80 >actual &&
        test_cmp expected actual &&
        git grep -h "(*NO_JIT)var" invalid-0x80 >actual &&
        test_cmp expected actual

++ git grep -h var invalid-0x80
++ test_cmp expected actual
++ test 2 -ne 2
++ eval 'diff -u' '"$@"'
+++ diff -u expected actual
++ git grep -h '(*NO_JIT)var' invalid-0x80
fatal: pcre2_match failed with error code -22: UTF-8 error: isolated byte with 0x80 bit set
error: last command exited with $?=128
not ok 13 - PCRE v2: grep ASCII from invalid UTF-8 data
#

#               git grep -h "var" invalid-0x80 >actual &&
#               test_cmp expected actual &&
#               git grep -h "(*NO_JIT)var" invalid-0x80 >actual &&
#               test_cmp expected actual

#

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
#17 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 7 lines · Andreas Schwab

expecting success of 7812.13 'PCRE v2: grep ASCII from invalid UTF-8 data':

        git grep -h "var" invalid-0x80 >actual &&
        test_cmp expected actual &&
        git grep -h "(*NO_JIT)var" invalid-0x80 >actual &&
        test_cmp expected actual
Quoted 7 lines

++ git grep -h var invalid-0x80
++ test_cmp expected actual
++ test 2 -ne 2
++ eval 'diff -u' '"$@"'
+++ diff -u expected actual
++ git grep -h '(*NO_JIT)var' invalid-0x80
fatal: pcre2_match failed with error code -22: UTF-8 error: isolated byte with 0x80 bit set

That is exactly what I was worried about, this is not failing one test, but making git grep unusable in any repository that has any binary files that might be reachable by it, and it is likely affecting anyone using PCRE older than 10.34

Reverting this specific commit might fix it and is unlikely to introduce other issues, did you try it?

Carlo

#18 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 2 lines · Carlo Arenas

Reverting this specific commit might fix it and is unlikely to introduce other issues, did you try it?

Yes, of course.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
#19 lore raw

PATCH 1/1 grep: avoid setting UTF mode when not needed

Since ae39ba431a (grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data, 2021-10-15), PCRE_UTF mode is enabled in cases where it will fail because of UTF-8 validation, which is needed for versions of PCRE2 older than 10.34.

Revert the change on logic to avoid failures that were reported from the test cases, but that should also reflect in normal use when JIT is enabled and could result in crashes (or worse), as UTF-8 validation is skipped.

Keeping the tests, as they pass even without the fix as replicated locally in Debian 10 and the CI.

---
 grep.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
grep.c +2 -4
index f6e113e9f0..fe847a0111 100644
@@ -382,10 +382,8 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		}
 		options |= PCRE2_CASELESS;
 	}
-	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
-	    (!opt->ignore_locale && is_utf8_locale() &&
-	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
-					    (p->fixed || p->is_fixed))))
+	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
 
 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
Signature · 1 line
2.34.0.352.g07dee3c5e1
#20 lore raw

Re: [PATCH] grep: avoid setting UTF mode when not needed

Quoted 25 lines · Carlo Marcelo Arenas Belón

Since ae39ba431a (grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data, 2021-10-15), PCRE_UTF mode is enabled in cases where it will fail because of UTF-8 validation, which is needed for versions of PCRE2 older than 10.34.

Revert the change on logic to avoid failures that were reported from the test cases, but that should also reflect in normal use when JIT is enabled and could result in crashes (or worse), as UTF-8 validation is skipped.

Keeping the tests, as they pass even without the fix as replicated locally in Debian 10 and the CI.

---
 grep.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
grep.c +0 -0
index f6e113e9f0..fe847a0111 100644
@@ -382,10 +382,8 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		}
 		options |= PCRE2_CASELESS;

grep.c static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt · line 389

Quoted 10 lines
grep.c +2 -4
 	}
-	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
-	    (!opt->ignore_locale && is_utf8_locale() &&
-	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
-					    (p->fixed || p->is_fixed))))
+	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
 
 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER

Hrm.

A few things:

First, if we've got a post-PCREv2 version whatever fix let's guard that with an ifdef, see thep GIT_PCRE2_VERSION_*_HIGHER at the top of grep.h.

It really helps to have those, both to know to test on those older versions, and also so that we can at some point in the future bump the required version and drop these workarounds entirely (as we do with git-curl-compat.h).

Secondly, whatever do here let's first fix the test added in ae39ba431a, so we're not groping around in the dark even more.

I didn't spot this at the time but the test that Hamza added in that based on my initial report[1] is broken & doesn't test anything meaningful. It needs to have this applied:

	diff --git a/t/t7812-grep-icase-non-ascii.sh b/t/t7812-grep-icase-non-ascii.sh
	index 22487d90fdc..1da6b07a579 100755
	--- a/t/t7812-grep-icase-non-ascii.sh
	+++ b/t/t7812-grep-icase-non-ascii.sh
	@@ -60,7 +60,7 @@ test_expect_success GETTEXT_LOCALE,PCRE 'log --author with an ascii pattern on U
	        test_write_lines "forth" >file4 &&
	        git add file4 &&
	        git commit --author="À Ú Thor <[email protected]>" -m sécond &&
	-       git log -1 --color=always --perl-regexp --author=".*Thor" >log &&
	+       git log -1 --color=always --perl-regexp --author=". . Thor" >log &&
	        grep Author log >actual.raw &&
	        test_decode_color <actual.raw >actual &&
	        test_cmp expected actual

I.e. the whole point of using the color output to test this is to discover where PCRE2 is going to consider a character boundary to be, using .* means that it won't be tested at add, since .* will happily eat up whatever arbitrary data it finds with or without UTF-8 mode.

Other tests added in that & adjacent (if any?) commits may have the same issue, I haven't dug into it.

If we lead with that patch we'll get the test passing on master as before, but with your patch above it'll break. I.e. the "when not needed' in the $subject isn't true, it's just that the test is completely broken.

In the context of this being a pretty urgent post-release fix (but I don't know if Junio would consider a point-release, so perhaphs it's not) I'd be OK with either of:

A. Let's back out this new log grep color thing entirely while we
reconsider this. The gitster/hm/paint-hits-in-log-grep topic currently reverts cleanly.

B. Don't break the new log grep color thing, and also fix the 'grepping
binary' regression (which is much more important than having A)

But let's not go for some in-between where we break the new feature to the point of it being worse than the state of not having it at all in v2.33.0.

I.e. without the that log grep color feature we wouldn't screw up the display of non-ASCII characters in log output (yay), in v2.34.0 we don't, but also color the match (yay), but we broke grepping binary *files* (boo!).

I think the approach I suggested in [2] is a much more viable way forward, i.e. let's stop fiddling with this giant nested if statement that's mainly meant for the grep-a-file-case, revert to the pre-log-grep-color state, and have the log-grep-color mode pass in a "yes, I'd like the UTF-8 mode, please".

Having said that that's probably also broken, just in ways we're less likely to spot (or maybe some of the log encoding settings/reencoding saves us there?), we may need to have that ifdef'd to 10.34 and higher, or have some opt-in setting for this.

1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/

#21 lore raw

Re: [PATCH] grep: avoid setting UTF mode when not needed

Does anyone know if René's patch causes older PCRE versions to break? If it doesn't I think René's patch plus Ævar's fix is the way to go.

Quoted 133 lines · Ævar Arnfjörð Bjarmason
Quoted 43 lines · Carlo Marcelo Arenas Belón

Since ae39ba431a (grep/pcre2: fix an edge case concerning ascii
patterns
and UTF-8 data, 2021-10-15), PCRE_UTF mode is enabled in cases
where it
will fail because of UTF-8 validation, which is needed for versions
of
PCRE2 older than 10.34.

Revert the change on logic to avoid failures that were reported
from the
test cases, but that should also reflect in normal use when JIT is
enabled
and could result in crashes (or worse), as UTF-8 validation is
skipped.

Keeping the tests, as they pass even without the fix as replicated
locally
in Debian 10 and the CI.

---

  grep.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/grep.c b/grep.c
index f6e113e9f0..fe847a0111 100644
--- a/grep.c
+++ b/grep.c
@@ -382,10 +382,8 @@ static void compile_pcre2_pattern(struct
grep_pat *p, const struct grep_opt *opt
}
options |= PCRE2_CASELESS;
}
- if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
- (!opt->ignore_locale && is_utf8_locale() &&
- has_non_ascii(p->pattern) && !(!opt->ignore_case &&
- (p->fixed || p->is_fixed))))
+ if (!opt->ignore_locale && is_utf8_locale() &&
has_non_ascii(p->pattern) &&
+ !(!opt->ignore_case && (p->fixed || p->is_fixed)))
options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);

Quoted 1 line
  #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER

Hrm.

A few things:

First, if we've got a post-PCREv2 version whatever fix let's guard that with an ifdef, see thep GIT_PCRE2_VERSION_*_HIGHER at the top of grep.h.

It really helps to have those, both to know to test on those older versions, and also so that we can at some point in the future bump the required version and drop these workarounds entirely (as we do with git-curl-compat.h).

Secondly, whatever do here let's first fix the test added in ae39ba431a, so we're not groping around in the dark even more.

I didn't spot this at the time but the test that Hamza added in that based on my initial report[1] is broken & doesn't test anything meaningful. It needs to have this applied:

	diff --git a/t/t7812-grep-icase-non-ascii.sh 
b/t/t7812-grep-icase-non-ascii.sh
	index 22487d90fdc..1da6b07a579 100755
	--- a/t/t7812-grep-icase-non-ascii.sh
	+++ b/t/t7812-grep-icase-non-ascii.sh
	@@ -60,7 +60,7 @@ test_expect_success GETTEXT_LOCALE,PCRE 'log 
--author with an ascii pattern on U
	        test_write_lines "forth" >file4 &&
	        git add file4 &&
	        git commit --author="À Ú Thor <[email protected]>" -m 

sécond &&
- git log -1 --color=always --perl-regexp --author=".*Thor"

Quoted 1 line

log &&

+ git log -1 --color=always --perl-regexp --author=". . Thor"

Quoted 1 line

log &&

	        grep Author log >actual.raw &&
	        test_decode_color <actual.raw >actual &&
	        test_cmp expected actual

I.e. the whole point of using the color output to test this is to discover where PCRE2 is going to consider a character boundary to be, using .* means that it won't be tested at add, since .* will happily eat up whatever arbitrary data it finds with or without UTF-8 mode.

Other tests added in that & adjacent (if any?) commits may have the same issue, I haven't dug into it.

If we lead with that patch we'll get the test passing on master as before, but with your patch above it'll break. I.e. the "when not needed' in the $subject isn't true, it's just that the test is completely broken.

In the context of this being a pretty urgent post-release fix (but I don't know if Junio would consider a point-release, so perhaphs it's not) I'd be OK with either of:

A. Let's back out this new log grep color thing entirely while we
reconsider this. The gitster/hm/paint-hits-in-log-grep topic currently reverts cleanly.

B. Don't break the new log grep color thing, and also fix the
'grepping
binary' regression (which is much more important than having A)

But let's not go for some in-between where we break the new feature to the point of it being worse than the state of not having it at all in v2.33.0.

I.e. without the that log grep color feature we wouldn't screw up the display of non-ASCII characters in log output (yay), in v2.34.0 we don't, but also color the match (yay), but we broke grepping binary *files* (boo!).

I think the approach I suggested in [2] is a much more viable way forward, i.e. let's stop fiddling with this giant nested if statement that's mainly meant for the grep-a-file-case, revert to the pre-log-grep-color state, and have the log-grep-color mode pass in a "yes, I'd like the UTF-8 mode, please".

Having said that that's probably also broken, just in ways we're less likely to spot (or maybe some of the log encoding settings/reencoding saves us there?), we may need to have that ifdef'd to 10.34 and higher, or have some opt-in setting for this.

1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/

#23 lore raw

Re: [PATCH] grep: avoid setting UTF mode when not needed

In reply to Ævar Arnfjörð Bjarmason
Quoted 2 lines · Ævar Arnfjörð Bjarmason

First, if we've got a post-PCREv2 version whatever fix let's guard that with an ifdef, see thep GIT_PCRE2_VERSION_*_HIGHER at the top of grep.h.

The availability of PCRE2_MATCH_INVALID_UTF does that implicitly, but adding one makes sense to make it explicit; specially if it leads to better testing.

Quoted 7 lines

In the context of this being a pretty urgent post-release fix (but I don't know if Junio would consider a point-release, so perhaphs it's not) I'd be OK with either of:

A. Let's back out this new log grep color thing entirely while we
reconsider this. The gitster/hm/paint-hits-in-log-grep topic currently reverts cleanly.

Agree that reverting the whole feature makes more sense, but was aiming to the minimum change required to allow for a "brown bag" release with this.

The way the PCRE2 integration works, uses the fast path that presumes valid UTF-8 and is documented[1] to have "Undefined Behaviour" when the subject that is being searched on is not.

That has been shown before to lead to crashes or infinite loops

Carlo

[1] https://www.pcre.org/current/doc/html/pcre2jit.html#SEC4

#24 lore raw

Re: [PATCH] grep: avoid setting UTF mode when not needed

In reply to Ævar Arnfjörð Bjarmason
Quoted 11 lines · Ævar Arnfjörð Bjarmason

In the context of this being a pretty urgent post-release fix (but I don't know if Junio would consider a point-release, so perhaphs it's not) I'd be OK with either of:

A. Let's back out this new log grep color thing entirely while we
reconsider this. The gitster/hm/paint-hits-in-log-grep topic currently reverts cleanly.

B. Don't break the new log grep color thing, and also fix the 'grepping
binary' regression (which is much more important than having A)

Quoted 8 lines

But let's not go for some in-between where we break the new feature to the point of it being worse than the state of not having it at all in v2.33.0.

I.e. without the that log grep color feature we wouldn't screw up the display of non-ASCII characters in log output (yay), in v2.34.0 we don't, but also color the match (yay), but we broke grepping binary *files* (boo!).

Sorry, but with too many new patches on the list that are not urgent, while we wanted to see us work solely on post-release regression fixes, I do not seem to be able to locate the reports of this "breakage" and the other binary-file breakage.

But in any case, yes, since the 2.33.0 cycle was run deliberately loosely to take undercooked topics to 'next' without much reviews (no, "I looked at some part and they looked OK" is not a review), and because any topic in 'next', by default, graduates to 'master' solely on time basis, I am fully expecting that we'd have to issue 2.33.1 (and possibly .2) during the first two or three weeks of this cycle. So let's make sure we fix any iffy ones.

Downthread Carlo seem to agree with you that it would be better to revert the paint-hits-in-log-grep topic wholesale; I have not yet formed an opinion, as I haven't seen the breakage reports as I said.

As I will be offline most of the day and perhaps tomorrow, nothing may happen in the meantime on my end, but hopefully we'll see a reviewed and ready-to-be-applied patchset that people are happy with by the time I look at the list again ;-)?

Thanks.

#25 lore raw

PATCH v2 1/1 grep: avoid setting UTF mode when dangerous with PCRE

In reply to Carlo Marcelo Arenas Belón

Since ae39ba431a (grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data, 2021-10-15), PCRE2_UTF mode is enabled for cases where it will trigger UTF-8 validation errors (as reported) or can result in undefined behaviour.

Our use of PCRE2 only allows searching through non UTF-8 validated data safely through the use of the PCRE2_MATCH_INVALID_UTF flag, that is only available after 10.34, so restrict the change to newer versions of PCRE and revert to the old logic for older releases, which will still allow for matching not using UTF-8 for likely most usecases (as shown in the tests).

Fix one test that was using an expression that wouldn't fail without the new code so it can be forced to fail if it is missing and restrict it to run only for newer PCRE releases; while at it do some minor refactoring to cleanup the fallout for when that test might be skipped or might succeed under the new conditions.

Keeping the overly complex and unnecessary logic for now, to reduce risk but with the hope that it will be cleaned up later.

---
v2:
* restrict the code at compile time instead of reverting
* "fix" test to document the behaviour under both PCRE code versions
* update commit message to better explain the issue

 grep.c                          |  7 ++++++-
 t/t7812-grep-icase-non-ascii.sh | 32 ++++++++++++++++++--------------
 2 files changed, 24 insertions(+), 15 deletions(-)
grep.c +6 -1
index f6e113e9f0..0126aa3db4 100644
@@ -382,12 +382,17 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		}
 		options |= PCRE2_CASELESS;
 	}
+#ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
 	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
 	    (!opt->ignore_locale && is_utf8_locale() &&
 	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
 					    (p->fixed || p->is_fixed))))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
-
+#else
+	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+		options |= PCRE2_UTF;
+#endif
 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
 	/* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
 	if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS))
t/t7812-grep-icase-non-ascii.sh +18 -14
index 22487d90fd..3bfe1ee728 100755
@@ -53,14 +53,27 @@ test_expect_success REGEX_LOCALE 'pickaxe -i on non-ascii' '
 	test_cmp expected actual
 '
 
-test_expect_success GETTEXT_LOCALE,PCRE 'log --author with an ascii pattern on UTF-8 data' '
-	cat >expected <<-\EOF &&
-	Author: <BOLD;RED>À Ú Thor<RESET> <[email protected]>
-	EOF
+test_expect_success GETTEXT_LOCALE,PCRE 'setup ascii pattern on UTF-8 data' '
 	test_write_lines "forth" >file4 &&
 	git add file4 &&
 	git commit --author="À Ú Thor <[email protected]>" -m sécond &&
-	git log -1 --color=always --perl-regexp --author=".*Thor" >log &&
+	test_write_lines "fifth" >file5 &&
+	git add file5 &&
+	GIT_COMMITTER_NAME="Ç O Mîtter" &&
+	GIT_COMMITTER_EMAIL="[email protected]" &&
+	git -c i18n.commitEncoding=latin1 commit -m thïrd
+'
+
+test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
+	test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
+'
+
+test_expect_success GETTEXT_LOCALE,PCRE,PCRE2_MATCH_INVALID_UTF 'log --author with an ascii pattern on UTF-8 data' '
+	cat >expected <<-\EOF &&
+	Author: <BOLD;RED>A U Thor<RESET> <[email protected]>
+	Author: <BOLD;RED>À Ú Thor<RESET> <[email protected]>
+	EOF
+	git log --color=always --perl-regexp --author=". . Thor" >log &&
 	grep Author log >actual.raw &&
 	test_decode_color <actual.raw >actual &&
 	test_cmp expected actual
@@ -70,11 +83,6 @@ test_expect_success GETTEXT_LOCALE,PCRE 'log --committer with an ascii pattern o
 	cat >expected <<-\EOF &&
 	Commit:     Ç<BOLD;RED> O Mîtter <[email protected]><RESET>
 	EOF
-	test_write_lines "fifth" >file5 &&
-	git add file5 &&
-	GIT_COMMITTER_NAME="Ç O Mîtter" &&
-	GIT_COMMITTER_EMAIL="[email protected]" &&
-	git -c i18n.commitEncoding=latin1 commit -m thïrd &&
 	git -c i18n.logOutputEncoding=latin1 log -1 --pretty=fuller --color=always --perl-regexp --committer=" O.*" >log &&
 	grep Commit: log >actual.raw &&
 	test_decode_color <actual.raw >actual &&
@@ -141,10 +149,6 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invali
 	test_cmp invalid-0xe5 actual
 '
 
-test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
-	test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
-'
-
 test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
 	test_might_fail git grep -hi "Æ" invalid-0x80 >actual &&
 	test_might_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual
Signature · 1 line
2.34.0.352.g07dee3c5e1
#22 lore raw

Re: [PATCH] grep: avoid setting UTF mode when not needed

In reply to Hamza Mahfooz
Quoted 2 lines · Hamza Mahfooz

Does anyone know if René's patch causes older PCRE versions to break? If it doesn't I think René's patch plus Ævar's fix is the way to go.

I haven't tested, but I think a very good thing to do/test (hint hint!) is to add a CI job where we build/test git in combination with various PCRE versions.

PCRE2 used to be in SVN, but nowadays it's in a git repository on
github: https://github.com/PhilipHazel/pcre2

It should be a small matter of copying the existing template to set up a "matrix" where we test various known older pcre versions, they're available as tags in the git repository.

"--enable-jit" and "--disable-jit" should be part of that matrix too, and "--enable-utf" and "--disable-utf".

See the "microsoft/vcpkg" and "regular:" parts of .github/workflows/main.git for relevant examples, i.e. this would e.g. clone pcre2.git into compat/pcre2-repo, check out a given version from the matrix. Then:

    cd compat/pcre2-repo &&
    ./autogen.sh &&
    ./configure --prefix="$PWD/inst" &&
    make install

Then at the top-level:

make USE_LIBPCRE=Y CFLAGS=-O3 LIBPCREDIR="$PWD/compat/pcre2-repo/inst"

Then run "make test", for saving some CPU & speeding up the runs it should be more than sufficient to only run those tests that match *{grep,log,pickaxe}*

#31 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Carlo Arenas

Am 16.11.21 um 10:38 schrieb Carlo Arenas:

Quoted 8 lines · Andreas Schwab
Quoted 7 lines

expecting success of 7812.13 'PCRE v2: grep ASCII from invalid UTF-8 data':

        git grep -h "var" invalid-0x80 >actual &&
        test_cmp expected actual &&
        git grep -h "(*NO_JIT)var" invalid-0x80 >actual &&
        test_cmp expected actual
Quoted 12 lines
Quoted 7 lines

++ git grep -h var invalid-0x80
++ test_cmp expected actual
++ test 2 -ne 2
++ eval 'diff -u' '"$@"'
+++ diff -u expected actual
++ git grep -h '(*NO_JIT)var' invalid-0x80
fatal: pcre2_match failed with error code -22: UTF-8 error: isolated byte with 0x80 bit set

That is exactly what I was worried about, this is not failing one test, but making git grep unusable in any repository that has any binary files that might be reachable by it, and it is likely affecting anyone using PCRE older than 10.34

Let's have a look at the map. Here are the differences between the versions regarding use of PCRE2_UTF:

o: opt->ignore_locale
h: has_non_ascii(p->pattern)
i: is_utf8_locale()
l: !opt->ignore_case && (p->fixed || p->is_fixed)

o h i l master hamza rene2

0 0 0 0      0     1     0
0 0 0 1      0     1     0
0 0 1 0      0     1     1
0 0 1 1      0     1     0  <== 7812.13, confirmed using fprint() debugging

So http://public-inbox.org/git/[email protected]/
should not have this breakage, because it doesn't enable PCRE2_UTF for literal patterns.

René

#32 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 32 lines · René Scharfe

Am 16.11.21 um 10:38 schrieb Carlo Arenas:

Quoted 8 lines · Andreas Schwab
Quoted 7 lines

expecting success of 7812.13 'PCRE v2: grep ASCII from invalid UTF-8 data':

        git grep -h "var" invalid-0x80 >actual &&
        test_cmp expected actual &&
        git grep -h "(*NO_JIT)var" invalid-0x80 >actual &&
        test_cmp expected actual
Quoted 12 lines
Quoted 7 lines

++ git grep -h var invalid-0x80
++ test_cmp expected actual
++ test 2 -ne 2
++ eval 'diff -u' '"$@"'
+++ diff -u expected actual
++ git grep -h '(*NO_JIT)var' invalid-0x80
fatal: pcre2_match failed with error code -22: UTF-8 error: isolated byte with 0x80 bit set

That is exactly what I was worried about, this is not failing one test, but making git grep unusable in any repository that has any binary files that might be reachable by it, and it is likely affecting anyone using PCRE older than 10.34

Let's have a look at the map. Here are the differences between the versions regarding use of PCRE2_UTF:

o: opt->ignore_locale
h: has_non_ascii(p->pattern)
i: is_utf8_locale()
l: !opt->ignore_case && (p->fixed || p->is_fixed)

o h i l master hamza rene2

Quoted 8 lines
0 0 0 0      0     1     0
0 0 0 1      0     1     0
0 0 1 0      0     1     1
0 0 1 1      0     1     0  <== 7812.13, confirmed using fprint() debugging

So http://public-inbox.org/git/[email protected]/
should not have this breakage, because it doesn't enable PCRE2_UTF for literal patterns.

Correct, and indeed I had your expression instead of the ugly one in my draft for v2[1] but then I found the tests were even more broken than reported originally and got worried it might introduce yet another issue because of the brittleness of the rest of the code around it. I am hoping it will be introduced in a follow up series though, and unless this code gets thrown away in the promised refactoring by Ævar which I haven't seen yet and once this is in maint.

IMHO and in line with the previous warnings you raised[2] during the development of this feature, it might be worth looking at it more deeply, but at least the proposed patch keeps the feature working in practice (the only case that won't work as expected will be when the expression includes "." with the intention of matching a UTF-8 character and while using PCRE2 older than 10.34) and more importantly fixes the regression that it introduced.

Carlo

[1] https://lore.kernel.org/git/[email protected]/
[2] https://lore.kernel.org/git/[email protected]/

#33 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to René Scharfe
Quoted 31 lines · René Scharfe

Am 16.11.21 um 10:38 schrieb Carlo Arenas:

Quoted 8 lines · Andreas Schwab
Quoted 7 lines

expecting success of 7812.13 'PCRE v2: grep ASCII from invalid UTF-8 data':

        git grep -h "var" invalid-0x80 >actual &&
        test_cmp expected actual &&
        git grep -h "(*NO_JIT)var" invalid-0x80 >actual &&
        test_cmp expected actual
Quoted 12 lines
Quoted 7 lines

++ git grep -h var invalid-0x80
++ test_cmp expected actual
++ test 2 -ne 2
++ eval 'diff -u' '"$@"'
+++ diff -u expected actual
++ git grep -h '(*NO_JIT)var' invalid-0x80
fatal: pcre2_match failed with error code -22: UTF-8 error: isolated byte with 0x80 bit set

That is exactly what I was worried about, this is not failing one test, but making git grep unusable in any repository that has any binary files that might be reachable by it, and it is likely affecting anyone using PCRE older than 10.34

Let's have a look at the map. Here are the differences between the versions regarding use of PCRE2_UTF:

o: opt->ignore_locale
h: has_non_ascii(p->pattern)
i: is_utf8_locale()
l: !opt->ignore_case && (p->fixed || p->is_fixed)

o h i l master hamza rene2

Quoted 8 lines
0 0 0 0      0     1     0
0 0 0 1      0     1     0
0 0 1 0      0     1     1
0 0 1 1      0     1     0  <== 7812.13, confirmed using fprint() debugging

So http://public-inbox.org/git/[email protected]/
should not have this breakage, because it doesn't enable PCRE2_UTF for literal patterns.

PCRE2_UTF will also matter for literal patterns. Try to peel apart the two bytes in "é" and match them under -i with/without PCRE_UTF.

I don't know what the right behavior should be here (haven't had time to dig), but it matters for matching.

#34 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 3 lines · Ævar Arnfjörð Bjarmason

PCRE2_UTF will also matter for literal patterns. Try to peel apart the two bytes in "é" and match them under -i with/without PCRE_UTF.

Is there a real use case for why someone would do that? and how is that "literal" valid UTF to warrant setting PCRE2_UTF?
I would expect that someone including random bytes in the expression is really more interested in binary matching anyway and the use of -i with it probably should be an error.

Indeed I suspect the fact that pcre2_compile lets it through might be a bug in PCRE2

$ pcre2test
PCRE2 version 10.39 2021-10-29
  re> /\303/utf,caseless
data> \303
 0: \x{c3}
data> é
No match

Carlo

#35 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Quoted 8 lines · Carlo Arenas
Quoted 3 lines · Ævar Arnfjörð Bjarmason

PCRE2_UTF will also matter for literal patterns. Try to peel apart the two bytes in "é" and match them under -i with/without PCRE_UTF.

Is there a real use case for why someone would do that? and how is that "literal" valid UTF to warrant setting PCRE2_UTF?

We don't check the validity of the input pattern as correct UTF-8 before feeding it to PCRE. We don't even check if you're under a UTF-8 locale, a call to is_utf8_locale() is one of the conditions we'll try (but check how loose we are under NO_GETTEXT=Y even then), but it's not a necessary one.

So in practice we'll likely have users pasting say Big5, UTF-32, JIS encoding or whatever into "git grep" and then grepping arbitrary binary data.

I don't really have a specific use-case in mind. I'm just trying to counter the apparent recurring misconception that's popped up more than once in these recent threads that the UTF-8 *mode* is only something we need to worry about if the pattern contains non-ASCII.

The implications of UTF-8 as a matching mode go much deeper than that in Perl's and PCRE's regex engines, e.g. in this case what's considered a character boundary.

Quoted 7 lines

I would expect that someone including random bytes in the expression is really more interested in binary matching anyway and the use of -i with it probably should be an error.

Indeed I suspect the fact that pcre2_compile lets it through might be a bug in PCRE2

Quoted 7 lines
$ pcre2test
PCRE2 version 10.39 2021-10-29
  re> /\303/utf,caseless
data> \303
 0: \x{c3}
data> é
No match

It's really not "random bytes".

We're not talking about someone doing a git grep where the argument is fed from a "cat" of /dev/urandom. Just plain old boring natural language encodings in common use can and will conflict with what's considered a character boundary in UTF-8,

Anyway, I haven't had time to re-page this topic at large into my wetware and really don't know what we should be doing exactly at this point to move forward, except my previous suggestion to either revert the recent changes, or at least to narrow them down so that we get the old behavior for everything except the revision.c "git log" caller.

I think a really good next step aside from dealing with that immediate problem would be to harden some of our test data in a way similar to what we've got in t7816-grep-binary-pattern.sh, but for partial matches, mixtures of valid/invalid/partial character patterns and subjects etc.

We might be able to lift some tests from existing test suites, perl.git's t/re/re_tests and pcre2.git's RunGrepTest (And testdata/ directory) are probably good places to start looking.

We don't need to test e.g. PCRE itself independently, but it is worth testing how whatever special-sauce we add on top (if and when to turn on its flags based on heuristics) impacts things.

We've also got the problem that whatever code we write will target multiple PCREv2 versions, which isn't something PCREv2 itself needs to deal with.

#26 lore raw

Re: [PATCH v2] grep: avoid setting UTF mode when dangerous with PCRE

In reply to Carlo Marcelo Arenas Belón
Quoted 21 lines · Carlo Marcelo Arenas Belón

Since ae39ba431a (grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data, 2021-10-15), PCRE2_UTF mode is enabled for cases where it will trigger UTF-8 validation errors (as reported) or can result in undefined behaviour.

Our use of PCRE2 only allows searching through non UTF-8 validated data safely through the use of the PCRE2_MATCH_INVALID_UTF flag, that is only available after 10.34, so restrict the change to newer versions of PCRE and revert to the old logic for older releases, which will still allow for matching not using UTF-8 for likely most usecases (as shown in the tests).

Fix one test that was using an expression that wouldn't fail without the new code so it can be forced to fail if it is missing and restrict it to run only for newer PCRE releases; while at it do some minor refactoring to cleanup the fallout for when that test might be skipped or might succeed under the new conditions.

Keeping the overly complex and unnecessary logic for now, to reduce risk but with the hope that it will be cleaned up later.

Quoted 8 lines

---
v2:
* restrict the code at compile time instead of reverting
* "fix" test to document the behaviour under both PCRE code versions
* update commit message to better explain the issue

So, is this the final verrsion everybody is happy with, instead of just reverting the whole "paint hits in log --grep" topic, or did I miss more discussion in the original thread?

With too many patches on the list, it is a bit hard to keep track of the more urgent regresison fixes X-<.

Thanks.

Quoted 87 lines
 grep.c                          |  7 ++++++-
 t/t7812-grep-icase-non-ascii.sh | 32 ++++++++++++++++++--------------
 2 files changed, 24 insertions(+), 15 deletions(-)
grep.c +6 -1
index f6e113e9f0..0126aa3db4 100644
@@ -382,12 +382,17 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		}
 		options |= PCRE2_CASELESS;
 	}
+#ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
 	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
 	    (!opt->ignore_locale && is_utf8_locale() &&
 	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
 					    (p->fixed || p->is_fixed))))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
-
+#else
+	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+		options |= PCRE2_UTF;
+#endif
 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
 	/* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
 	if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS))
t/t7812-grep-icase-non-ascii.sh +18 -14
index 22487d90fd..3bfe1ee728 100755
@@ -53,14 +53,27 @@ test_expect_success REGEX_LOCALE 'pickaxe -i on non-ascii' '
 	test_cmp expected actual
 '
 
-test_expect_success GETTEXT_LOCALE,PCRE 'log --author with an ascii pattern on UTF-8 data' '
-	cat >expected <<-\EOF &&
-	Author: <BOLD;RED>À Ú Thor<RESET> <[email protected]>
-	EOF
+test_expect_success GETTEXT_LOCALE,PCRE 'setup ascii pattern on UTF-8 data' '
 	test_write_lines "forth" >file4 &&
 	git add file4 &&
 	git commit --author="À Ú Thor <[email protected]>" -m sécond &&
-	git log -1 --color=always --perl-regexp --author=".*Thor" >log &&
+	test_write_lines "fifth" >file5 &&
+	git add file5 &&
+	GIT_COMMITTER_NAME="Ç O Mîtter" &&
+	GIT_COMMITTER_EMAIL="[email protected]" &&
+	git -c i18n.commitEncoding=latin1 commit -m thïrd
+'
+
+test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
+	test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
+'
+
+test_expect_success GETTEXT_LOCALE,PCRE,PCRE2_MATCH_INVALID_UTF 'log --author with an ascii pattern on UTF-8 data' '
+	cat >expected <<-\EOF &&
+	Author: <BOLD;RED>A U Thor<RESET> <[email protected]>
+	Author: <BOLD;RED>À Ú Thor<RESET> <[email protected]>
+	EOF
+	git log --color=always --perl-regexp --author=". . Thor" >log &&
 	grep Author log >actual.raw &&
 	test_decode_color <actual.raw >actual &&
 	test_cmp expected actual
@@ -70,11 +83,6 @@ test_expect_success GETTEXT_LOCALE,PCRE 'log --committer with an ascii pattern o
 	cat >expected <<-\EOF &&
 	Commit:     Ç<BOLD;RED> O Mîtter <[email protected]><RESET>
 	EOF
-	test_write_lines "fifth" >file5 &&
-	git add file5 &&
-	GIT_COMMITTER_NAME="Ç O Mîtter" &&
-	GIT_COMMITTER_EMAIL="[email protected]" &&
-	git -c i18n.commitEncoding=latin1 commit -m thïrd &&
 	git -c i18n.logOutputEncoding=latin1 log -1 --pretty=fuller --color=always --perl-regexp --committer=" O.*" >log &&
 	grep Commit: log >actual.raw &&
 	test_decode_color <actual.raw >actual &&
@@ -141,10 +149,6 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invali
 	test_cmp invalid-0xe5 actual
 '
 
-test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
-	test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
-'
-
 test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
 	test_might_fail git grep -hi "Æ" invalid-0x80 >actual &&
 	test_might_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual
#27 lore raw

Re: [PATCH v2] grep: avoid setting UTF mode when dangerous with PCRE

Quoted 4 lines · Junio C Hamano

So, is this the final verrsion everybody is happy with, instead of just reverting the whole "paint hits in log --grep" topic, or did I miss more discussion in the original thread?

This will likely need at least some ACKs.

Apologies for the noise.

Carlo

#28 lore raw

Re: [PATCH v2] grep: avoid setting UTF mode when dangerous with PCRE

If none of the patches that are trying to fix our usage of PCRE2 are ACKed. It would be preferable (from my perspective) to revert only ae39ba431a (grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data, 2021-10-15) and disable the feature (for the time being) if PCRE2 is selected, like so:

pretty.c +1 -1
index 1af5b093ae..a53d008d1e 100644
@@ -452,7 +452,7 @@ static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
 buf = line;
 eol = buf + linelen;
 
- if (!opt || !want_color(color) || opt->invert)
+ if (!opt || !want_color(color) || opt->invert || opt->pcre2)
  goto end;
 
 line_color = opt->colors[GREP_COLOR_SELECTED];
Quoted 6 lines · Carlo Arenas

This will likely need at least some ACKs.

Apologies for the noise.

Carlo

#36 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Ævar Arnfjörð Bjarmason
Quoted 14 lines · Ævar Arnfjörð Bjarmason
Quoted 14 lines

Let's have a look at the map. Here are the differences between the versions regarding use of PCRE2_UTF:

o: opt->ignore_locale
h: has_non_ascii(p->pattern)
i: is_utf8_locale()
l: !opt->ignore_case && (p->fixed || p->is_fixed)

o h i l master hamza rene2

0 0 0 0      0     1     0
0 0 0 1      0     1     0
0 0 1 0      0     1     1
0 0 1 1      0     1     0  <== 7812.13, confirmed using fprint() debugging
Quoted 6 lines
Quoted 3 lines

So http://public-inbox.org/git/[email protected]/
should not have this breakage, because it doesn't enable PCRE2_UTF for literal patterns.

PCRE2_UTF will also matter for literal patterns. Try to peel apart the two bytes in "é" and match them under -i with/without PCRE_UTF.

Sorry for being late to the party, but doesn't "literal" in the context of this thread mean the column "l" above, i.e. we are not ignoring case and fixed or is_fixed member is set? So "under -i" disqualifies as an example for "will also matter for literal", doesn't it?

In hindsight, I guess we could have pushed a bit harder when René's

- if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+ if (!opt->ignore_locale && is_utf8_locale() &&
!(!opt->ignore_case && (p->fixed || p->is_fixed)))
options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);

in https://public-inbox.org/git/[email protected]/
(is that what is called 'rene2' above?) was raised on Oct 17th to amend/fix Hamza's [v13 3/3]; that would have prevented 'master' from having this breakage?

Carlo, in your [PATCH v2] in <[email protected]>, I see that the #else side for older PCREv2 users essentially reverts what Hamza's [PATCH v13 3/3] did to this area.

+#else
+ if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+ !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+ options |= PCRE2_UTF;
+#endif

I guess this is a lot of change in the amount of text involved but the least amount of actual change in the behaviour. For those with newer PCREv2, the behaviour would be the same as v2.34.0, and for others, the behaviour would be the same as v2.33.0.

Having said all that, because the consensus seems to be that the whole "when we should match in UTF mode" may need to be rethought, I think reverting Hamza's [v13 3/3] would be the simplest way to clean up the mess for v2.34.1 that will give us a cleaner slate to later build on, than applying this patch.

So, I dunno. Comments from those involved in the discussion?

Thanks.

#37 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Am 18.11.21 um 19:17 schrieb Junio C Hamano:

Quoted 20 lines
Quoted 18 lines · Ævar Arnfjörð Bjarmason
Quoted 17 lines

Let's have a look at the map. Here are the differences between the versions regarding use of PCRE2_UTF:

o: opt->ignore_locale
h: has_non_ascii(p->pattern)
i: is_utf8_locale()
l: !opt->ignore_case && (p->fixed || p->is_fixed)

o h i l master hamza rene2

0 0 0 0      0     1     0
0 0 0 1      0     1     0
0 0 1 0      0     1     1
0 0 1 1      0     1     0  <== 7812.13, confirmed using fprint() debugging

So http://public-inbox.org/git/[email protected]/
should not have this breakage, because it doesn't enable PCRE2_UTF for literal patterns.

Quoted 8 lines
Quoted 2 lines

PCRE2_UTF will also matter for literal patterns. Try to peel apart the two bytes in "é" and match them under -i with/without PCRE_UTF.

Sorry for being late to the party, but doesn't "literal" in the context of this thread mean the column "l" above, i.e. we are not ignoring case and fixed or is_fixed member is set? So "under -i" disqualifies as an example for "will also matter for literal", doesn't it?

Correct.

Quoted 11 lines

In hindsight, I guess we could have pushed a bit harder when René's

(quoted diff) +1 -1
-	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	if (!opt->ignore_locale && is_utf8_locale() &&
 	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);

in https://public-inbox.org/git/[email protected]/
(is that what is called 'rene2' above?) was raised on Oct 17th to amend/fix Hamza's [v13 3/3]; that would have prevented 'master' from having this breakage?

Yes, that the change I meant with "rene2".

Quoted 15 lines

Carlo, in your [PATCH v2] in <[email protected]>, I see that the #else side for older PCREv2 users essentially reverts what Hamza's [PATCH v13 3/3] did to this area.

(quoted diff) +5 -0
+#else
+	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+		options |= PCRE2_UTF;
+#endif

I guess this is a lot of change in the amount of text involved but the least amount of actual change in the behaviour. For those with newer PCREv2, the behaviour would be the same as v2.34.0, and for others, the behaviour would be the same as v2.33.0.

Quoted 5 lines

Having said all that, because the consensus seems to be that the whole "when we should match in UTF mode" may need to be rethought, I think reverting Hamza's [v13 3/3] would be the simplest way to clean up the mess for v2.34.1 that will give us a cleaner slate to later build on, than applying this patch.

Makes sense to me. It gives a better starting point to solve the issue afresh without getting entangled in mind-melting boolean expressions.

René

#30 lore raw

Re: [PATCH v2] grep: avoid setting UTF mode when dangerous with PCRE

In reply to Carlo Marcelo Arenas Belón

Building on to your patch we can disable the feature if an older version of PCRE2 is used, like so:

pretty.c +4 -1
index 1af5b093ae..9bd3f61b30 100644
@@ -451,8 +451,11 @@ static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
 
 buf = line;
 eol = buf + linelen;
-
+#ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
 if (!opt || !want_color(color) || opt->invert)
+#else
+ if (!opt || !want_color(color) || opt->invert || opt->pcre2)
+#endif
  goto end;
 
 line_color = opt->colors[GREP_COLOR_SELECTED];
Quoted 132 lines · Carlo Marcelo Arenas Belón

Since ae39ba431a (grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data, 2021-10-15), PCRE2_UTF mode is enabled for cases where it will trigger UTF-8 validation errors (as reported) or can result in undefined behaviour.

Our use of PCRE2 only allows searching through non UTF-8 validated data safely through the use of the PCRE2_MATCH_INVALID_UTF flag, that is only available after 10.34, so restrict the change to newer versions of PCRE and revert to the old logic for older releases, which will still allow for matching not using UTF-8 for likely most usecases (as shown in the tests).

Fix one test that was using an expression that wouldn't fail without the new code so it can be forced to fail if it is missing and restrict it to run only for newer PCRE releases; while at it do some minor refactoring to cleanup the fallout for when that test might be skipped or might succeed under the new conditions.

Keeping the overly complex and unnecessary logic for now, to reduce risk but with the hope that it will be cleaned up later.

---
v2:
* restrict the code at compile time instead of reverting
* "fix" test to document the behaviour under both PCRE code versions
* update commit message to better explain the issue

 grep.c                          |  7 ++++++-
 t/t7812-grep-icase-non-ascii.sh | 32 ++++++++++++++++++--------------
 2 files changed, 24 insertions(+), 15 deletions(-)
grep.c +0 -0
index f6e113e9f0..0126aa3db4 100644
@@ -382,12 +382,17 @@ static void compile_pcre2_pattern(struct 

grep_pat *p, const struct grep_opt *opt

grep.c +3 -1
 		}
 		options |= PCRE2_CASELESS;
 	}
+#ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
 	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
 	    (!opt->ignore_locale && is_utf8_locale() &&
 	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
 					    (p->fixed || p->is_fixed))))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
-
+#else
+	if (!opt->ignore_locale && is_utf8_locale() && 

has_non_ascii(p->pattern) &&

grep.c +3 -0
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+		options |= PCRE2_UTF;
+#endif
 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
 	/* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 

10.36 */
if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF |
PCRE2_CASELESS))

t/t7812-grep-icase-non-ascii.sh +0 -0
index 22487d90fd..3bfe1ee728 100755
@@ -53,14 +53,27 @@ test_expect_success REGEX_LOCALE 'pickaxe -i on 

non-ascii' '

t/t7812-grep-icase-non-ascii.sh +0 -1
 	test_cmp expected actual
 '
 
-test_expect_success GETTEXT_LOCALE,PCRE 'log --author with an ascii 

pattern on UTF-8 data' '

t/t7812-grep-icase-non-ascii.sh +1 -3
-	cat >expected <<-\EOF &&
-	Author: <BOLD;RED>À Ú Thor<RESET> <[email protected]>
-	EOF
+test_expect_success GETTEXT_LOCALE,PCRE 'setup ascii pattern on 

UTF-8 data' '

t/t7812-grep-icase-non-ascii.sh +17 -9
 	test_write_lines "forth" >file4 &&
 	git add file4 &&
 	git commit --author="À Ú Thor <[email protected]>" -m sécond &&
-	git log -1 --color=always --perl-regexp --author=".*Thor" >log &&
+	test_write_lines "fifth" >file5 &&
+	git add file5 &&
+	GIT_COMMITTER_NAME="Ç O Mîtter" &&
+	GIT_COMMITTER_EMAIL="[email protected]" &&
+	git -c i18n.commitEncoding=latin1 commit -m thïrd
+'
+
+test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
+	test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
+'
+
+test_expect_success GETTEXT_LOCALE,PCRE,PCRE2_MATCH_INVALID_UTF 'log 
--author with an ascii pattern on UTF-8 data' '
+	cat >expected <<-\EOF &&
+	Author: <BOLD;RED>A U Thor<RESET> <[email protected]>
+	Author: <BOLD;RED>À Ú Thor<RESET> <[email protected]>
+	EOF
+	git log --color=always --perl-regexp --author=". . Thor" >log &&
 	grep Author log >actual.raw &&
 	test_decode_color <actual.raw >actual &&
 	test_cmp expected actual
@@ -70,11 +83,6 @@ test_expect_success GETTEXT_LOCALE,PCRE 'log 
--committer with an ascii pattern o
 	cat >expected <<-\EOF &&
 	Commit:     Ç<BOLD;RED> O Mîtter <[email protected]><RESET>
 	EOF
-	test_write_lines "fifth" >file5 &&
-	git add file5 &&
-	GIT_COMMITTER_NAME="Ç O Mîtter" &&
-	GIT_COMMITTER_EMAIL="[email protected]" &&
-	git -c i18n.commitEncoding=latin1 commit -m thïrd &&
 	git -c i18n.logOutputEncoding=latin1 log -1 --pretty=fuller 
--color=always --perl-regexp --committer=" O.*" >log &&
 	grep Commit: log >actual.raw &&
 	test_decode_color <actual.raw >actual &&
@@ -141,10 +149,6 @@ test_expect_success GETTEXT_LOCALE,LIBPCRE2 

'PCRE v2: grep non-ASCII from invali

t/t7812-grep-icase-non-ascii.sh +0 -4
 	test_cmp invalid-0xe5 actual
 '
 
-test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
-	test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
-'
-
 test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII 

from invalid UTF-8 data with -i' '

t/t7812-grep-icase-non-ascii.sh +0 -1
 	test_might_fail git grep -hi "Æ" invalid-0x80 >actual &&
 	test_might_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual
--

2.34.0.352.g07dee3c5e1

#29 lore raw

Re: [PATCH v2] grep: avoid setting UTF mode when dangerous with PCRE

In reply to Hamza Mahfooz
Quoted 12 lines · Hamza Mahfooz

If none of the patches that are trying to fix our usage of PCRE2 are ACKed.
It would be preferable (from my perspective) to revert only ae39ba431a (grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data, 2021-10-15) and disable the feature (for the time being) if PCRE2 is selected, like so:

pretty.c +0 -0
index 1af5b093ae..a53d008d1e 100644

pretty.c static void append_line_with_color(struct strbuf · line 457

Quoted 7 lines
pretty.c +1 -1
@@ -452,7 +452,7 @@ static void append_line_with_color(struct strbuf
 *sb, struct grep_opt *opt,
 buf = line;
 eol = buf + linelen;
 
- if (!opt || !want_color(color) || opt->invert)
+ if (!opt || !want_color(color) || opt->invert || opt->pcre2)
  goto end;

I see that depending on LC_ALL the behavior of grep.patternType=extended can exhibit some of the same issues with character splitting, so perhaps what we'd do with the C library serves as a useful guide?

#38 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to René Scharfe
Quoted 8 lines · René Scharfe

Am 18.11.21 um 19:17 schrieb Junio C Hamano:

Quoted 1 line

Ævar Arnfjörð Bjarmason <[email protected]> writes:

[...]

Quoted 5 lines

I guess this is a lot of change in the amount of text involved but the least amount of actual change in the behaviour. For those with newer PCREv2, the behaviour would be the same as v2.34.0, and for others, the behaviour would be the same as v2.33.0.

Quoted 8 lines
Quoted 5 lines

Having said all that, because the consensus seems to be that the whole "when we should match in UTF mode" may need to be rethought, I think reverting Hamza's [v13 3/3] would be the simplest way to clean up the mess for v2.34.1 that will give us a cleaner slate to later build on, than applying this patch.

Makes sense to me. It gives a better starting point to solve the issue afresh without getting entangled in mind-melting boolean expressions.

Yes, agreed. As noted I haven't had time to dig deeply into this, but from what I've seen so far there doesn't seem to be any obvious way forward in terms of a quick fix.

I thought perhaps your patch would be that (but I haven't looked into it carefully enough), but since you're on-board with reverting & retrying.

#39 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

Am 19.11.21 um 08:00 schrieb Ævar Arnfjörð Bjarmason:

Quoted 20 lines · René Scharfe
Quoted 9 lines

Am 18.11.21 um 19:17 schrieb Junio C Hamano:

Quoted 1 line

Ævar Arnfjörð Bjarmason <[email protected]> writes:

[...]

Quoted 5 lines

I guess this is a lot of change in the amount of text involved but the least amount of actual change in the behaviour. For those with newer PCREv2, the behaviour would be the same as v2.34.0, and for others, the behaviour would be the same as v2.33.0.

Quoted 8 lines
Quoted 5 lines

Having said all that, because the consensus seems to be that the whole "when we should match in UTF mode" may need to be rethought, I think reverting Hamza's [v13 3/3] would be the simplest way to clean up the mess for v2.34.1 that will give us a cleaner slate to later build on, than applying this patch.

Makes sense to me. It gives a better starting point to solve the issue afresh without getting entangled in mind-melting boolean expressions.

Quoted 6 lines

Yes, agreed. As noted I haven't had time to dig deeply into this, but from what I've seen so far there doesn't seem to be any obvious way forward in terms of a quick fix.

I thought perhaps your patch would be that (but I haven't looked into it carefully enough), but since you're on-board with reverting & retrying.

That patch should fix the edge case without any side-effects -- at least I haven't seen any reports of ill effects that would apply to it. It's easier to understand and reason about when applied after reverting, I think. But it's only for grep.c and I don't know the situation in t/.

René

#41 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to Ævar Arnfjörð Bjarmason
Quoted 11 lines · Ævar Arnfjörð Bjarmason
Quoted 3 lines · René Scharfe

Makes sense to me. It gives a better starting point to solve the issue afresh without getting entangled in mind-melting boolean expressions.

Yes, agreed. As noted I haven't had time to dig deeply into this, but from what I've seen so far there doesn't seem to be any obvious way forward in terms of a quick fix.

I thought perhaps your patch would be that (but I haven't looked into it carefully enough), but since you're on-board with reverting & retrying.

OK. Here is what I'll queue 'master', in the hope of later merging down and be part of v2.34.1.

Thanks, all.

----- >8 --------- >8 --------- >8 --------- >8 -----
Subject: [PATCH] Revert "grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data"

This reverts commit ae39ba431ab861548eb60b4bd2e1d8b8813db76f, as it breaks "grep" when looking for a string in non UTF-8 haystack, when linked with certain versions of PCREv2 library.

---
 grep.c                          |  6 ++---
 t/t7812-grep-icase-non-ascii.sh | 48 ---------------------------------
 2 files changed, 2 insertions(+), 52 deletions(-)
grep.c +2 -4
index f6e113e9f0..fe847a0111 100644
@@ -382,10 +382,8 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		}
 		options |= PCRE2_CASELESS;
 	}
-	if ((!opt->ignore_locale && !has_non_ascii(p->pattern)) ||
-	    (!opt->ignore_locale && is_utf8_locale() &&
-	     has_non_ascii(p->pattern) && !(!opt->ignore_case &&
-					    (p->fixed || p->is_fixed))))
+	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
+	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
 
 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
t/t7812-grep-icase-non-ascii.sh +0 -48

54 lines not on this page · raw message

Signature · 1 line
2.34.0-202-gd9146917d7
#40 lore raw

Re: [PATCH v13 3/3] grep/pcre2: fix an edge case concerning ascii patterns and UTF-8 data

In reply to René Scharfe
Quoted 26 lines · René Scharfe

Am 19.11.21 um 08:00 schrieb Ævar Arnfjörð Bjarmason:

Quoted 24 lines · René Scharfe
Quoted 9 lines

Am 18.11.21 um 19:17 schrieb Junio C Hamano:

Quoted 1 line

Ævar Arnfjörð Bjarmason <[email protected]> writes:

[...]

Quoted 5 lines

I guess this is a lot of change in the amount of text involved but the least amount of actual change in the behaviour. For those with newer PCREv2, the behaviour would be the same as v2.34.0, and for others, the behaviour would be the same as v2.33.0.

Quoted 8 lines
Quoted 5 lines

Having said all that, because the consensus seems to be that the whole "when we should match in UTF mode" may need to be rethought, I think reverting Hamza's [v13 3/3] would be the simplest way to clean up the mess for v2.34.1 that will give us a cleaner slate to later build on, than applying this patch.

Makes sense to me. It gives a better starting point to solve the issue afresh without getting entangled in mind-melting boolean expressions.

Yes, agreed. As noted I haven't had time to dig deeply into this, but from what I've seen so far there doesn't seem to be any obvious way forward in terms of a quick fix.

Quoted 5 lines
Quoted 2 lines

I thought perhaps your patch would be that (but I haven't looked into it carefully enough), but since you're on-board with reverting & retrying.

That patch should fix the edge case without any side-effects -- at least I haven't seen any reports of ill effects that would apply to it.

Since it isn't restricted to log, it will still cause a regression to the git grep case with binary data for versions of PCRE2 older than 10.34 and unlike the previous one it might not trigger an error in the testsuite just because we are missing a test for it.

Quoted 3 lines

It's
easier to understand and reason about when applied after reverting, I think. But it's only for grep.c and I don't know the situation in t/.

We had been focusing in PCRE in this discussion, but I see the strict behaviour of older PCRE2 as just a "coal mine canary" to point to the bigger problem that we are expecting git's regex to handle safely and correctly from the point of UTF, what is technically a binary match with --color making the mismatch obvious.

The issue is not unique to PCRE, and seems Ævar also acknowledges[1] that by seeing the same bug this was attempting to fix with probably some version of glibc's ERE. I suspect FreeBSD's (and derivatives) is also broken and might be throwing REGILLSEQ errors as well, so I think that it is better to revert the whole thing.

Carlo

[1] https://lore.kernel.org/git/[email protected]/