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

3 messages from 2023-06-07 to 2023-07-21. Participants: Qixing ksyx Xue.
Thread: https://gitlist.dev/t/59841

## Qixing ksyx Xue, 2023-06-07 00:00

Subject: [PATCH] gitweb: wrong capture group used for grep search
Message-ID: <ME2P282MB224200468AFBBBD4534ED1F7B453A@ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM>
URL: https://gitlist.dev/e/ME2P282MB224200468AFBBBD4534ED1F7B453A%40ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM

```
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(-)

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, 2023-07-21 16:36

Subject: [PATCH] gitweb: wrong capture group used for grep search
Message-ID: <ME2P282MB2242E63AC1A1AC91E29BF935B43FA@ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM>
URL: https://gitlist.dev/e/ME2P282MB2242E63AC1A1AC91E29BF935B43FA%40ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM
In-Reply-To: <ME2P282MB224200468AFBBBD4534ED1F7B453A@ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM>

```
Rebased to master and a polite ping :)

Best,
Qixing



```

## Qixing ksyx Xue, 2023-07-21 16:36

Subject: [PATCH] gitweb: wrong capture group used for grep search
Message-ID: <ME2P282MB2242C7D0E3D92E2CE5A13B68B43FA@ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM>
URL: https://gitlist.dev/e/ME2P282MB2242C7D0E3D92E2CE5A13B68B43FA%40ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM
In-Reply-To: <ME2P282MB224200468AFBBBD4534ED1F7B453A@ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM>

```
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(-)

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


```
