fix wrong operator precedence in destructor macros

Sun, 09 Apr 2023 20:00:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 09 Apr 2023 20:00:44 +0200
changeset 680
19379743e5a0
parent 679
022fbd4bc057
child 681
502105523db7

fix wrong operator precedence in destructor macros

src/cx/collection.h file | annotate | diff | comparison | revisions
src/list.c file | annotate | diff | comparison | revisions
tests/test_list.cpp file | annotate | diff | comparison | revisions
--- a/src/cx/collection.h	Sun Apr 09 19:37:00 2023 +0200
+++ b/src/cx/collection.h	Sun Apr 09 20:00:44 2023 +0200
@@ -112,7 +112,7 @@
  * @param e the element
  */
 #define cx_invoke_simple_destructor(c, e) \
-    (c)->simple_destructor((c)->store_pointer ? (*((void **) e)) : e)
+    (c)->simple_destructor((c)->store_pointer ? (*((void **) (e))) : (e))
 
 /**
  * Invokes the advanced destructor function for a specific element.
@@ -125,7 +125,7 @@
  */
 #define cx_invoke_advanced_destructor(c, e) \
     (c)->advanced_destructor((c)->destructor_data, \
-    (c)->store_pointer ? (*((void **) e)) : e)
+    (c)->store_pointer ? (*((void **) (e))) : (e))
 
 
 #define cx_invoke_destructor(c, e) \
--- a/src/list.c	Sun Apr 09 19:37:00 2023 +0200
+++ b/src/list.c	Sun Apr 09 20:00:44 2023 +0200
@@ -219,11 +219,11 @@
         CxList const *list,
         CxList const *other
 ) {
-    if (list->cl->compare == other->cl->compare) {
-        // same compare function, lists are compatible
-        return list->cl->compare(list, other);
-    } else {
-        // different compare functions, use iterator
+    if ((list->store_pointer ^ other->store_pointer) ||
+        ((list->climpl == NULL) ^ (other->climpl != NULL)) ||
+        ((list->climpl != NULL ? list->climpl->compare : list->cl->compare) !=
+         (other->climpl != NULL ? other->climpl->compare : other->cl->compare))) {
+        // lists are definitely different - cannot use internal compare function
         if (list->size == other->size) {
             CxIterator left = cxListIterator(list);
             CxIterator right = cxListIterator(other);
@@ -241,6 +241,9 @@
         } else {
             return list->size < other->size ? -1 : 1;
         }
+    } else {
+        // lists are compatible
+        return list->cl->compare(list, other);
     }
 }
 
--- a/tests/test_list.cpp	Sun Apr 09 19:37:00 2023 +0200
+++ b/tests/test_list.cpp	Sun Apr 09 20:00:44 2023 +0200
@@ -751,8 +751,6 @@
         cxListClear(list);
         EXPECT_EQ(testdata_len, destr_test_ctr);
         EXPECT_EQ(testdata.data[testdata_len - 1], destr_last_value + off);
-
-
     }
 
     void verifySimpleDestructor(CxList *list) {

mercurial