1From 1f88976610d5bcc15ad58c9345848d736d64fd55 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 17:16:07 +0200 4Subject: [PATCH] gregex: Do not try access the undefined match offsets if we 5 have no match 6 7In case we're getting NO-MATCH "errors", we were still recomputing the 8match offsets and taking decisions based on that, that might lead to 9undefined behavior. 10 11Avoid this by just returning early a FALSE result (but with no error) in 12case there's no result to proceed on. 13 14Fixes: #2741 15--- 16 glib/gregex.c | 6 ++++++ 17 glib/tests/regex.c | 6 ++++++ 18 2 files changed, 12 insertions(+) 19 20diff --git a/glib/gregex.c b/glib/gregex.c 21index 219d9cee34..f2a5b5fd1c 100644 22--- a/glib/gregex.c 23+++ b/glib/gregex.c 24@@ -1073,6 +1073,12 @@ g_match_info_next (GMatchInfo *match_info, 25 match_info->regex->pattern, match_error (match_info->matches)); 26 return FALSE; 27 } 28+ else if (match_info->matches == PCRE2_ERROR_NOMATCH) 29+ { 30+ /* We're done with this match info */ 31+ match_info->pos = -1; 32+ return FALSE; 33+ } 34 else 35 if (!recalc_match_offsets (match_info, error)) 36 return FALSE; 37diff --git a/glib/tests/regex.c b/glib/tests/regex.c 38index 10daa7814a..291c21b4c7 100644 39--- a/glib/tests/regex.c 40+++ b/glib/tests/regex.c 41@@ -1669,6 +1669,12 @@ test_class (void) 42 res = g_match_info_next (match, NULL); 43 g_assert (!res); 44 45+ /* Accessing match again should not crash */ 46+ g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, 47+ "*match_info->pos >= 0*"); 48+ g_assert_false (g_match_info_next (match, NULL)); 49+ g_test_assert_expected_messages (); 50+ 51 g_match_info_free (match); 52 g_regex_unref (regex); 53 } 54-- 55GitLab 56 57