mirror of
				https://gitlab.gnome.org/GNOME/glib.git
				synced 2025-11-04 10:08:56 +01:00 
			
		
		
		
	gregex: Do not try access the undefined match offsets if we have no match
In case we're getting NO-MATCH "errors", we were still recomputing the match offsets and taking decisions based on that, that might lead to undefined behavior. Avoid this by just returning early a FALSE result (but with no error) in case there's no result to proceed on. Fixes: #2741
This commit is contained in:
		@@ -1073,6 +1073,12 @@ g_match_info_next (GMatchInfo  *match_info,
 | 
				
			|||||||
                   match_info->regex->pattern, match_error (match_info->matches));
 | 
					                   match_info->regex->pattern, match_error (match_info->matches));
 | 
				
			||||||
      return FALSE;
 | 
					      return FALSE;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  else if (match_info->matches == PCRE2_ERROR_NOMATCH)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      /* We're done with this match info */
 | 
				
			||||||
 | 
					      match_info->pos = -1;
 | 
				
			||||||
 | 
					      return FALSE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  else
 | 
					  else
 | 
				
			||||||
    if (!recalc_match_offsets (match_info, error))
 | 
					    if (!recalc_match_offsets (match_info, error))
 | 
				
			||||||
      return FALSE;
 | 
					      return FALSE;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1669,6 +1669,12 @@ test_class (void)
 | 
				
			|||||||
  res = g_match_info_next (match, NULL);
 | 
					  res = g_match_info_next (match, NULL);
 | 
				
			||||||
  g_assert (!res);
 | 
					  g_assert (!res);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /* Accessing match again should not crash */
 | 
				
			||||||
 | 
					  g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
 | 
				
			||||||
 | 
					                         "*match_info->pos >= 0*");
 | 
				
			||||||
 | 
					  g_assert_false (g_match_info_next (match, NULL));
 | 
				
			||||||
 | 
					  g_test_assert_expected_messages ();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  g_match_info_free (match);
 | 
					  g_match_info_free (match);
 | 
				
			||||||
  g_regex_unref (regex);
 | 
					  g_regex_unref (regex);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user