From 7a74805067b54d93b1c93d99d6afc4be85fb3ebe Mon Sep 17 00:00:00 2001 From: implicitfield <114500360+implicitfield@users.noreply.github.com> Date: Fri, 22 Sep 2023 23:01:25 +0400 Subject: [PATCH] Tests: Avoid casting function types in LibC/TestSearch.cpp Doing so causes a function type mismatch, which makes the test crash when built with a new enough version of UBSan. --- Tests/LibC/TestSearch.cpp | 46 +++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/Tests/LibC/TestSearch.cpp b/Tests/LibC/TestSearch.cpp index 2b7a0d30ad8..aedc479bd75 100644 --- a/Tests/LibC/TestSearch.cpp +++ b/Tests/LibC/TestSearch.cpp @@ -12,9 +12,13 @@ #define NODE(node) static_cast(node) #define ROOTP(root) reinterpret_cast(root) -#define COMP(func) reinterpret_cast(func) #define U8(value) static_cast(value) +static int comparison_function(void const* node1, void const* node2) +{ + return strcmp(reinterpret_cast(node1), reinterpret_cast(node2)); +} + struct twalk_test_entry { void const* node; VISIT order; @@ -33,62 +37,62 @@ TEST_CASE(tsearch) char* search; // Try a nullptr rootp. - ret = tsearch("buggie", nullptr, COMP(strcmp)); + ret = tsearch("buggie", nullptr, comparison_function); EXPECT_EQ(ret, nullptr); // Try creating a new tree. key = "5"; - ret = tsearch(key, ROOTP(&root), COMP(strcmp)); + ret = tsearch(key, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root); EXPECT_EQ(NODE(ret)->key, key); // Insert an element on the left side. key = "3"; - ret = tsearch(key, ROOTP(&root), COMP(strcmp)); + ret = tsearch(key, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->left); EXPECT_EQ(NODE(ret)->key, key); // Insert an element on the right side. key = "7"; - ret = tsearch(key, ROOTP(&root), COMP(strcmp)); + ret = tsearch(key, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->right); EXPECT_EQ(NODE(ret)->key, key); // Add another layer for testing. - ret = tsearch("2", ROOTP(&root), COMP(strcmp)); + ret = tsearch("2", ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->left->left); - ret = tsearch("4", ROOTP(&root), COMP(strcmp)); + ret = tsearch("4", ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->left->right); - ret = tsearch("6", ROOTP(&root), COMP(strcmp)); + ret = tsearch("6", ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->right->left); - ret = tsearch("8", ROOTP(&root), COMP(strcmp)); + ret = tsearch("8", ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->right->right); // Find the root element. // strdup ensures that we are using the comparator. search = strdup("5"); - ret = tsearch(search, ROOTP(&root), COMP(strcmp)); + ret = tsearch(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root); free(search); // Find the lowest-level elements. search = strdup("2"); - ret = tsearch(search, ROOTP(&root), COMP(strcmp)); + ret = tsearch(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->left->left); free(search); search = strdup("4"); - ret = tsearch(search, ROOTP(&root), COMP(strcmp)); + ret = tsearch(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->left->right); free(search); search = strdup("6"); - ret = tsearch(search, ROOTP(&root), COMP(strcmp)); + ret = tsearch(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->right->left); free(search); search = strdup("8"); - ret = tsearch(search, ROOTP(&root), COMP(strcmp)); + ret = tsearch(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->right->right); free(search); @@ -102,11 +106,11 @@ TEST_CASE(tfind) char* search; // Try a nullptr rootp. - ret = tfind("buggie", nullptr, COMP(strcmp)); + ret = tfind("buggie", nullptr, comparison_function); EXPECT_EQ(ret, nullptr); // Search for something that doesn't exist. - ret = tfind("buggie", ROOTP(&root), COMP(strcmp)); + ret = tfind("buggie", ROOTP(&root), comparison_function); EXPECT_EQ(ret, nullptr); // Construct a tree for testing. @@ -121,28 +125,28 @@ TEST_CASE(tfind) // Find the root element. // strdup ensures that we are using the comparator. search = strdup("5"); - ret = tfind(search, ROOTP(&root), COMP(strcmp)); + ret = tfind(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root); free(search); // Find the lowest-level elements. search = strdup("2"); - ret = tfind(search, ROOTP(&root), COMP(strcmp)); + ret = tfind(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->left->left); free(search); search = strdup("4"); - ret = tfind(search, ROOTP(&root), COMP(strcmp)); + ret = tfind(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->left->right); free(search); search = strdup("6"); - ret = tfind(search, ROOTP(&root), COMP(strcmp)); + ret = tfind(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->right->left); free(search); search = strdup("8"); - ret = tfind(search, ROOTP(&root), COMP(strcmp)); + ret = tfind(search, ROOTP(&root), comparison_function); EXPECT_EQ(ret, root->right->right); free(search);