avl_tree.h: avoid bad function pointer cast
authorEric Biggers <ebiggers3@gmail.com>
Sun, 29 Jan 2017 05:18:21 +0000 (21:18 -0800)
committerEric Biggers <ebiggers3@gmail.com>
Sun, 19 Mar 2017 19:59:25 +0000 (12:59 -0700)
Casting the type of the 'cmp' function, while under normal circumstances
compiled correctly, was not technically correct and was not compatible
with some control flow integrity (CFI) implementations.

include/wimlib/avl_tree.h

index ed615ed..b7a9bb1 100644 (file)
@@ -145,10 +145,18 @@ avl_tree_lookup_node(const struct avl_tree_node *root,
                     int (*cmp)(const struct avl_tree_node *,
                                const struct avl_tree_node *))
 {
-       return avl_tree_lookup(root,
-                              (const void *)node,
-                              (int (*) (const void *,
-                                        const struct avl_tree_node *))cmp);
+       const struct avl_tree_node *cur = root;
+
+       while (cur) {
+               int res = (*cmp)(node, cur);
+               if (res < 0)
+                       cur = cur->left;
+               else if (res > 0)
+                       cur = cur->right;
+               else
+                       break;
+       }
+       return (struct avl_tree_node*)cur;
 }
 
 /*