From d5e85cf3f9b9637e369872c9a003b7cedd5931fe Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Fri, 25 Mar 2016 20:25:18 -0700 Subject: [PATCH 1/2] more efficient hook removal --- uc.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/uc.c b/uc.c index ce9f121d..af504fe1 100644 --- a/uc.c +++ b/uc.c @@ -1029,15 +1029,19 @@ uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback, UNICORN_EXPORT uc_err uc_hook_del(uc_engine *uc, uc_hook hh) { - int i; - struct hook *hook; - for (i = 0; i < UC_HOOK_MAX; i++) { - if (list_remove(&uc->hook[i], (void *)hh)) { - hook = (struct hook *)hh; - if (--hook->refs == 0) { - free(hook); + int i = 0; + struct hook *hook = (struct hook *)hh; + int type = hook->type; + + while ((type >> i) > 0 && i < UC_HOOK_MAX) { + if ((type >> i) & 1) { + if (list_remove(&uc->hook[i], (void *)hh)) { + if (--hook->refs == 0) { + free(hook); + } } } + i++; } return UC_ERR_OK; } From 784efc8be021e99c6ebebc84d8098df4b7f2f879 Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Fri, 25 Mar 2016 20:28:23 -0700 Subject: [PATCH 2/2] fix memory corruption in list_remove --- list.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/list.c b/list.c index 6dbe4782..b1627f59 100644 --- a/list.c +++ b/list.c @@ -54,6 +54,8 @@ bool list_remove(struct list *list, void *data) if (cur->data == data) { if (cur == list->head) { list->head = next; + } else { + prev->next = next; } if (cur == list->tail) { list->tail = prev;