fix: int overflow in hash_string() function results in negative bucket id

This commit is contained in:
overflowerror 2024-05-02 17:36:51 +02:00
parent 650e47f693
commit cdc7eab841
2 changed files with 6 additions and 3 deletions

View file

@ -7,8 +7,8 @@
#include "alloc.h"
#include "list.h"
int hash_string(const char* str) {
int result = 0;
unsigned int hash_string(const char* str) {
unsigned int result = 0;
while (*str != '\0') {
result = *str + 31 * result;
str++;
@ -38,7 +38,7 @@ struct dict_pair* dict_search_in_bucket(dict_bucket_t bucket, const char* key) {
}
dict_bucket_t dict_find_bucket(dict_t* dict, const char* key) {
int hash = hash_string(key);
unsigned int hash = hash_string(key);
return dict->buckets[hash % NUMBER_OF_BUCKETS];
}

View file

@ -0,0 +1,3 @@
var program = "foo\n";
print(program);