threads / patch / 59841

patchgitweb: wrong capture group used for grep search

Subject: [PATCH] gitweb: wrong capture group used for grep search

## tl;dr

3 messages between Jun 7, 2023 and Jul 21, 2023. Diffs are folded; open one to read it.

replies: 2people: 1as markdown or json

Qixing ksyx Xue· Jun 7, 2023, 00:00 UTC · lore

The capture group numberings $1, $2, and $3 used in the original script does not work well when the search term itself has capture group, and could be replicated with the simplest case of p(a|b), whose search result with regard to original text `XpaY' would yield `Xpaa' since capture group 3 now becomes the one in the search term, instead of some suffix. This patch fixes the problem with named capture group with capture names that hopefully would not conflict.

Signed-off-by: Qixing ksyx Xue <qixingxue@outlook.com>
---
 gitweb/gitweb.perl | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Show changes to gitweb/gitweb.perl +4 −4
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index e66eb3d9ba..e868122853 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -6418,12 +6418,12 @@ sub git_search_files {
 			print "<div class=\"binary\">Binary file</div>\n";
 		} else {
 			$ltext = untabify($ltext);
-			if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
-				$ltext = esc_html($1, -nbsp=>1);
+			if ($ltext =~ m/^(?<__gitweb__prefix>.*)(?<__gitweb__matching_text>$search_regexp)(?<__gitweb__suffix>.*)$/i) {
+				$ltext = esc_html($+{__gitweb__prefix}, -nbsp=>1);
 				$ltext .= '<span class="match">';
-				$ltext .= esc_html($2, -nbsp=>1);
+				$ltext .= esc_html($+{__gitweb__matching_text}, -nbsp=>1);
 				$ltext .= '</span>';
-				$ltext .= esc_html($3, -nbsp=>1);
+				$ltext .= esc_html($+{__gitweb__suffix}, -nbsp=>1);
 			} else {
 				$ltext = esc_html($ltext, -nbsp=>1);
 			}
-- 
2.30.0
Qixing ksyx Xue· Jul 21, 2023, 16:36 UTC · re: Qixing ksyx Xue · lore

The capture group numberings $1, $2, and $3 used in the original script does not work well when the search term itself has capture group, and could be replicated with the simplest case of p(a|b), whose search result with regard to original text `XpaY' would yield `Xpaa' since capture group 3 now becomes the one in the search term, instead of some suffix. This patch fixes the problem with named capture group with capture names that hopefully would not conflict.

Signed-off-by: Qixing ksyx Xue <qixingxue@outlook.com>
---
 gitweb/gitweb.perl | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Show changes to gitweb/gitweb.perl +4 −4
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index e66eb3d9ba..e868122853 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -6418,12 +6418,12 @@ sub git_search_files {
 			print "<div class=\"binary\">Binary file</div>\n";
 		} else {
 			$ltext = untabify($ltext);
-			if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
-				$ltext = esc_html($1, -nbsp=>1);
+			if ($ltext =~ m/^(?<__gitweb__prefix>.*)(?<__gitweb__matching_text>$search_regexp)(?<__gitweb__suffix>.*)$/i) {
+				$ltext = esc_html($+{__gitweb__prefix}, -nbsp=>1);
 				$ltext .= '<span class="match">';
-				$ltext .= esc_html($2, -nbsp=>1);
+				$ltext .= esc_html($+{__gitweb__matching_text}, -nbsp=>1);
 				$ltext .= '</span>';
-				$ltext .= esc_html($3, -nbsp=>1);
+				$ltext .= esc_html($+{__gitweb__suffix}, -nbsp=>1);
 			} else {
 				$ltext = esc_html($ltext, -nbsp=>1);
 			}
-- 
2.30.0

← back to recent threads