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

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

From
QXQixing ksyx Xue <qixingxue@outlook.com>
Date
Jul 21, 2023, 16:36 UTC
Message-ID
<ME2P282MB2242C7D0E3D92E2CE5A13B68B43FA@ME2P282MB2242.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
Previous: Qixing ksyx Xue
Message 3 of 3 in “gitweb: wrong capture group used for grep search”
  1. gitweb: wrong capture group used for grep searchQixing ksyx Xue, Jun 7, 2023
  2. gitweb: wrong capture group used for grep searchQixing ksyx Xue, Jul 21, 2023
  3. gitweb: wrong capture group used for grep searchQixing ksyx Xue, Jul 21, 2023

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

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