From cf3870509aa408284e7dfb5185dad59dd29e3a50 Mon Sep 17 00:00:00 2001 From: Wang Renxin Date: Wed, 5 Apr 2017 16:54:53 +0800 Subject: [PATCH] *fixed a wrong hash bug with pointer, which may cause crash on some 64bit systems. --- HISTORY | 3 +++ core/my_basic.c | 17 +++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/HISTORY b/HISTORY index 5241d0f..e3bec92 100755 --- a/HISTORY +++ b/HISTORY @@ -1,3 +1,6 @@ +Apr. 5 2017 +Fixed a wrong hash bug with pointer, which may cause crash on some 64bit systems + Mar. 29 2017 Added an alive object checker of referenced usertype diff --git a/core/my_basic.c b/core/my_basic.c index 0a5ece8..8d8bd8a 100755 --- a/core/my_basic.c +++ b/core/my_basic.c @@ -2701,14 +2701,15 @@ static unsigned _ht_hash_string(void* ht, void* d) { static unsigned _ht_hash_intptr(void* ht, void* d) { unsigned result = 0; _ht_node_t* self = (_ht_node_t*)ht; - intptr_t i = *(intptr_t*)d; mb_assert(ht); - if(self->array_size == 1) + if(self->array_size == 1) { result = 0; - else - result = (unsigned)(i % self->array_size); + } else { + uintptr_t u = (uintptr_t)d; + result = (unsigned)(u % self->array_size); + } return result; } @@ -2723,8 +2724,8 @@ static unsigned _ht_hash_ref(void* ht, void* d) { if(self->array_size == 1) { result = 0; } else { - result = (unsigned)(intptr_t)ref; - result %= self->array_size; + uintptr_t u = (uintptr_t)ref; + result = (unsigned)(u % self->array_size); } return result; @@ -2825,8 +2826,8 @@ static int _ht_cmp_string(void* d1, void* d2) { } static int _ht_cmp_intptr(void* d1, void* d2) { - intptr_t i1 = *(intptr_t*)d1; - intptr_t i2 = *(intptr_t*)d2; + intptr_t i1 = (intptr_t)d1; + intptr_t i2 = (intptr_t)d2; if(i1 < i2) return -1;