gregex: Ensure we translate the errcode without asserting on G_REGEX_ERROR_COMPILE

Since commit 8d5a44dc in order to ensure that we were setting the errcode in
translate_compile_error(), we did an assert checking whether it was a
valid value, but we assumed that 0 was not a valid error, while it is as
it's the generic G_REGEX_ERROR_COMPILE.

So, set errcode and errmsg to invalid values before translating and
ensure we've change them.

Fixes: #2694
This commit is contained in:
Marco Trevisan (Treviño) 2022-07-15 01:27:33 +02:00
parent c1eb93d2a2
commit c05d09044f
2 changed files with 19 additions and 2 deletions

View File

@ -476,8 +476,12 @@ translate_compile_error (gint *errcode, const gchar **errmsg)
* Note that there can be more PCRE errors with the same GRegexError
* and that some PCRE errors are useless for us.
*/
gint original_errcode = *errcode;
switch (*errcode)
*errcode = -1;
*errmsg = NULL;
switch (original_errcode)
{
case PCRE2_ERROR_END_BACKSLASH:
*errcode = G_REGEX_ERROR_STRAY_BACKSLASH;
@ -725,7 +729,7 @@ translate_compile_error (gint *errcode, const gchar **errmsg)
break;
}
g_assert (*errcode != 0);
g_assert (*errcode != -1);
g_assert (*errmsg != NULL);
}

View File

@ -2187,6 +2187,18 @@ pcre2_ge (guint64 major, guint64 minor)
return (pcre2_major > major) || (pcre2_major == major && pcre2_minor >= minor);
}
static void
test_compile_errors (void)
{
GRegex *regex;
GError *error = NULL;
regex = g_regex_new ("\\o{999}", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &error);
g_assert_null (regex);
g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE);
g_clear_error (&error);
}
int
main (int argc, char *argv[])
{
@ -2204,6 +2216,7 @@ main (int argc, char *argv[])
g_test_add_func ("/regex/multiline", test_multiline);
g_test_add_func ("/regex/explicit-crlf", test_explicit_crlf);
g_test_add_func ("/regex/max-lookbehind", test_max_lookbehind);
g_test_add_func ("/regex/compile-errors", test_compile_errors);
/* TEST_NEW(pattern, compile_opts, match_opts) */
G_GNUC_BEGIN_IGNORE_DEPRECATIONS