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(); }