From 8c27aef8836800bd83d92cad6b994ed1fea5891e Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sat, 28 Jan 2017 21:18:21 -0800 Subject: [PATCH] avl_tree.h: avoid bad function pointer cast 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 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/include/wimlib/avl_tree.h b/include/wimlib/avl_tree.h index ed615edc..b7a9bb1b 100644 --- a/include/wimlib/avl_tree.h +++ b/include/wimlib/avl_tree.h @@ -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; } /* -- 2.43.0