From b46ed37c972f4ece50cbd0acf359df34c6de578e Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 22 Nov 2022 13:12:05 +0000 Subject: [PATCH] gstrfuncs: Fix regression in types accepted by g_str_equal() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new macro form of `g_str_equal()` had stricter type checking than the original function form. That would be nice, except it causes new compiler warnings in third party projects, which counts as an API break for us, so unfortunately we can’t do it. Add some tests to prevent regressions on this again. Signed-off-by: Philip Withnall Fixes: #2809 --- glib/ghash.h | 2 +- glib/tests/strfuncs.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/glib/ghash.h b/glib/ghash.h index 8e7f4a0d1..6a3741576 100644 --- a/glib/ghash.h +++ b/glib/ghash.h @@ -162,7 +162,7 @@ gboolean g_str_equal (gconstpointer v1, gconstpointer v2); /* Macro for optimization in the case it is not used as callback function */ -#define g_str_equal(v1, v2) (strcmp ((v1), (v2)) == 0) +#define g_str_equal(v1, v2) (strcmp ((gconstpointer) (v1), (gconstpointer) (v2)) == 0) GLIB_AVAILABLE_IN_ALL guint g_str_hash (gconstpointer v); diff --git a/glib/tests/strfuncs.c b/glib/tests/strfuncs.c index 082eec074..3f936882f 100644 --- a/glib/tests/strfuncs.c +++ b/glib/tests/strfuncs.c @@ -2130,6 +2130,31 @@ test_transliteration (void) g_free (out); } +static void +test_str_equal (void) +{ + const guchar *unsigned_a = (const guchar *) "a"; + + g_test_summary ("Test macro and function forms of g_str_equal()"); + + /* Test function form. */ + g_assert_true ((g_str_equal) ("a", "a")); + g_assert_false ((g_str_equal) ("a", "b")); + + /* Test macro form. */ + g_assert_true (g_str_equal ("a", "a")); + g_assert_false (g_str_equal ("a", "b")); + + /* As g_str_equal() is defined for use with GHashTable, it takes gconstpointer + * arguments, so can historically accept unsigned arguments. We need to + * continue to support that. */ + g_assert_true ((g_str_equal) (unsigned_a, "a")); + g_assert_false ((g_str_equal) (unsigned_a, "b")); + + g_assert_true (g_str_equal (unsigned_a, "a")); + g_assert_false (g_str_equal (unsigned_a, "b")); +} + /* Testing g_strv_contains() function with various cases */ static void test_strv_contains (void) @@ -2597,6 +2622,7 @@ main (int argc, g_test_add_func ("/strfuncs/strv-length", test_strv_length); g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit); g_test_add_func ("/strfuncs/transliteration", test_transliteration); + g_test_add_func ("/strfuncs/str-equal", test_str_equal); return g_test_run(); }