1From e8628a7ed59e54b5a5e498de0375f101a4e76e64 Mon Sep 17 00:00:00 2001 2From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net> 3Date: Tue, 6 Sep 2022 19:05:24 +0200 4Subject: [PATCH] regex: Compute the offsets size based on match results 5 6While the ovector count would include all the allocated space, we only 7care about the actual match values, so avoid wasting allocations and 8just use the ones we need to hold the offsets. 9--- 10 glib/gregex.c | 16 +++++++++++++--- 11 1 file changed, 13 insertions(+), 3 deletions(-) 12 13diff --git a/glib/gregex.c b/glib/gregex.c 14index cf86f0fe0d..8a3be9076b 100644 15--- a/glib/gregex.c 16+++ b/glib/gregex.c 17@@ -832,10 +832,20 @@ recalc_match_offsets (GMatchInfo *match_info, 18 GError **error) 19 { 20 PCRE2_SIZE *ovector; 21+ uint32_t ovector_size = 0; 22 uint32_t pre_n_offset; 23 uint32_t i; 24 25- if (pcre2_get_ovector_count (match_info->match_data) > G_MAXUINT32 / 2) 26+ g_assert (!IS_PCRE2_ERROR (match_info->matches)); 27+ 28+ if (match_info->matches == PCRE2_ERROR_PARTIAL) 29+ ovector_size = 1; 30+ else if (match_info->matches > 0) 31+ ovector_size = match_info->matches; 32+ 33+ g_assert (ovector_size != 0); 34+ 35+ if (pcre2_get_ovector_count (match_info->match_data) < ovector_size) 36 { 37 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH, 38 _("Error while matching regular expression %s: %s"), 39@@ -844,7 +854,7 @@ recalc_match_offsets (GMatchInfo *match_info, 40 } 41 42 pre_n_offset = match_info->n_offsets; 43- match_info->n_offsets = pcre2_get_ovector_count (match_info->match_data) * 2; 44+ match_info->n_offsets = ovector_size * 2; 45 ovector = pcre2_get_ovector_pointer (match_info->match_data); 46 47 if (match_info->n_offsets != pre_n_offset) 48@@ -2387,7 +2397,7 @@ g_regex_match_all_full (const GRegex *regex, 49 _("Error while matching regular expression %s: %s"), 50 regex->pattern, match_error (info->matches)); 51 } 52- else if (info->matches > 0) 53+ else if (info->matches != PCRE2_ERROR_NOMATCH) 54 { 55 if (!recalc_match_offsets (info, error)) 56 info->matches = PCRE2_ERROR_NOMATCH; 57-- 58GitLab 59 60