From ed2e95b3f4f90efe5cf219e15a0cd280e1887175 Mon Sep 17 00:00:00 2001 From: szdytom Date: Fri, 14 Jun 2024 11:10:08 +0800 Subject: [PATCH] fix overflow --- src/objects/dict.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/objects/dict.c b/src/objects/dict.c index df285611..48643235 100644 --- a/src/objects/dict.c +++ b/src/objects/dict.c @@ -17,10 +17,10 @@ inline static int pkpy_Dict__idx_size(const pkpy_Dict* self) { return 4; } -inline static int pkpy_Dict__idx_null(const pkpy_Dict* self) { +inline static unsigned int pkpy_Dict__idx_null(const pkpy_Dict* self) { if(self->_htcap < 255) return 255; if(self->_htcap < 65535) return 65535; - return 4294967295; + return 4294967295u; // 2^32 - 1 } inline static int pkpy_Dict__ht_byte_size(const pkpy_Dict* self) { return self->_htcap * pkpy_Dict__idx_size(self); } @@ -48,7 +48,7 @@ pkpy_Dict pkpy_Dict__copy(const pkpy_Dict* self) { ._hashtable = ht_clone}; } -static int pkpy_Dict__htget(const pkpy_Dict* self, int h) { +static unsigned int pkpy_Dict__htget(const pkpy_Dict* self, int h) { int sz = pkpy_Dict__idx_size(self); switch(sz) { case 1: return ((uint8_t*)self->_hashtable)[h]; @@ -72,7 +72,7 @@ static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, pkpy_Var key, int6 const int null = pkpy_Dict__idx_null(self); const int mask = self->_htcap - 1; for(int h = hash & mask;; h = (h * 5 + 1) & mask) { - int idx = pkpy_Dict__htget(self, h); + unsigned int idx = pkpy_Dict__htget(self, h); if(idx == null) return h; struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); @@ -85,7 +85,7 @@ static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, pkpy_Var key, int6 const int null = pkpy_Dict__idx_null(self); const int mask = self->_htcap - 1; for(int h = hash & mask;; h = (h * 5 + 1) & mask) { - int idx = pkpy_Dict__htget(self, h); + unsigned int idx = pkpy_Dict__htget(self, h); if(idx == null) return h; struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); @@ -114,7 +114,7 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) { int64_t hash = pkpy_Var__hash__(vm, key); int h = pkpy_Dict__probe1(self, vm, key, hash); - int idx = pkpy_Dict__htget(self, h); + unsigned int idx = pkpy_Dict__htget(self, h); if(idx == pkpy_Dict__idx_null(self)) { idx = self->_entries.count; c11_vector__push(struct pkpy_DictEntry, @@ -137,7 +137,7 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) { } else { self->count += 1; h = pkpy_Dict__probe0(self, vm, key, hash); - idx = pkpy_Dict__htget(self, h); + unsigned idx = pkpy_Dict__htget(self, h); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); entry->key = key; entry->val = val; @@ -149,7 +149,7 @@ bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, pkpy_Var key) { int64_t hash = pkpy_Var__hash__(vm, key); int h = pkpy_Dict__probe1(self, vm, key, hash); - int idx = pkpy_Dict__htget(self, h); + unsigned int idx = pkpy_Dict__htget(self, h); if(idx == pkpy_Dict__idx_null(self)) return false; struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); @@ -186,7 +186,7 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) { bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) { int64_t hash = pkpy_Var__hash__(vm, key); int h = pkpy_Dict__probe1(self, vm, key, hash); - int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self); + unsigned int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self); if(idx == null) return false; struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); @@ -201,7 +201,7 @@ const pkpy_Var *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, pkpy_Var key int64_t hash = pkpy_Var__hash__(vm, key); int h = pkpy_Dict__probe1(self, vm, key, hash); - int idx = pkpy_Dict__htget(self, h); + unsigned int idx = pkpy_Dict__htget(self, h); if(idx == pkpy_Dict__idx_null(self)) return NULL; struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);