fix overflow

This commit is contained in:
方而静 2024-06-14 11:10:08 +08:00
parent 9390b0d638
commit ed2e95b3f4

View File

@ -17,10 +17,10 @@ inline static int pkpy_Dict__idx_size(const pkpy_Dict* self) {
return 4; 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 < 255) return 255;
if(self->_htcap < 65535) return 65535; 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); } 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}; ._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); int sz = pkpy_Dict__idx_size(self);
switch(sz) { switch(sz) {
case 1: return ((uint8_t*)self->_hashtable)[h]; 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 null = pkpy_Dict__idx_null(self);
const int mask = self->_htcap - 1; const int mask = self->_htcap - 1;
for(int h = hash & mask;; h = (h * 5 + 1) & mask) { 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; if(idx == null) return h;
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); 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 null = pkpy_Dict__idx_null(self);
const int mask = self->_htcap - 1; const int mask = self->_htcap - 1;
for(int h = hash & mask;; h = (h * 5 + 1) & mask) { 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; if(idx == null) return h;
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); 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); int64_t hash = pkpy_Var__hash__(vm, key);
int h = pkpy_Dict__probe1(self, vm, key, hash); 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)) { if(idx == pkpy_Dict__idx_null(self)) {
idx = self->_entries.count; idx = self->_entries.count;
c11_vector__push(struct pkpy_DictEntry, 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 { } else {
self->count += 1; self->count += 1;
h = pkpy_Dict__probe0(self, vm, key, hash); 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); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
entry->key = key; entry->key = key;
entry->val = val; 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); int64_t hash = pkpy_Var__hash__(vm, key);
int h = pkpy_Dict__probe1(self, vm, key, hash); 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; if(idx == pkpy_Dict__idx_null(self)) return false;
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); 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) { bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) {
int64_t hash = pkpy_Var__hash__(vm, key); int64_t hash = pkpy_Var__hash__(vm, key);
int h = pkpy_Dict__probe1(self, vm, key, hash); 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; if(idx == null) return false;
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); 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); int64_t hash = pkpy_Var__hash__(vm, key);
int h = pkpy_Dict__probe1(self, vm, key, hash); 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; if(idx == pkpy_Dict__idx_null(self)) return NULL;
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);