Merge branch '2820-str-equal-cxx' into 'main'

gstrfuncs: Fix regression in C++ types accepted by g_str_equal()

Closes #2820

See merge request GNOME/glib!3094
This commit is contained in:
Philip Withnall 2022-11-29 12:00:12 +00:00
commit 5f945e5464
2 changed files with 24 additions and 1 deletions

View File

@ -162,7 +162,7 @@ gboolean g_str_equal (gconstpointer v1,
gconstpointer v2); gconstpointer v2);
/* Macro for optimization in the case it is not used as callback function */ /* Macro for optimization in the case it is not used as callback function */
#define g_str_equal(v1, v2) (strcmp ((gconstpointer) (v1), (gconstpointer) (v2)) == 0) #define g_str_equal(v1, v2) (strcmp ((const char *) (v1), (const char *) (v2)) == 0)
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
guint g_str_hash (gconstpointer v); guint g_str_hash (gconstpointer v);

View File

@ -299,6 +299,28 @@ test_steal_pointer (void)
g_assert_null (ptr); g_assert_null (ptr);
} }
static void
test_str_equal (void)
{
const char *str_a = "a";
char *str_b = g_strdup ("b");
gconstpointer str_a_ptr = str_a, str_b_ptr = str_b;
const unsigned char *str_c = (const unsigned char *) "c";
g_test_summary ("Test typechecking and casting of arguments to g_str_equal() macro in C++");
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2820");
/* We dont actually care what the comparisons do at runtime. What were
* checking here is that the types dont emit warnings at compile time. */
g_assert_true (g_str_equal ("a", str_a));
g_assert_false (g_str_equal ("a", str_b));
g_assert_true (g_str_equal (str_a, str_a_ptr));
g_assert_false (g_str_equal (str_a_ptr, str_b_ptr));
g_assert_false (g_str_equal (str_c, str_b));
g_free (str_b);
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -321,6 +343,7 @@ main (int argc, char *argv[])
g_test_add_func ("/C++/inlined-not-inlined-functions", test_inline_no_inline_macros); g_test_add_func ("/C++/inlined-not-inlined-functions", test_inline_no_inline_macros);
g_test_add_func ("/C++/clear-pointer", test_clear_pointer); g_test_add_func ("/C++/clear-pointer", test_clear_pointer);
g_test_add_func ("/C++/steal-pointer", test_steal_pointer); g_test_add_func ("/C++/steal-pointer", test_steal_pointer);
g_test_add_func ("/C++/str-equal", test_str_equal);
return g_test_run (); return g_test_run ();
} }