mk: fix hash function (#315)

Avoid signed integer overflow using ulong instead of long h.
This commit is contained in:
Neven Sajko 2020-01-14 04:05:03 +01:00 committed by Russ Cox
parent 6bddb06b71
commit 26cae02da7

View file

@ -1,7 +1,7 @@
#include "mk.h" #include "mk.h"
#define NHASH 4099 #define NHASH 4099
#define HASHMUL 79L /* this is a good value */ #define HASHMUL 79UL /* this is a good value */
static Symtab *hash[NHASH]; static Symtab *hash[NHASH];
void void
@ -21,14 +21,12 @@ syminit(void)
Symtab * Symtab *
symlook(char *sym, int space, void *install) symlook(char *sym, int space, void *install)
{ {
long h; ulong h;
char *p; char *p;
Symtab *s; Symtab *s;
for(p = sym, h = space; *p; h += *p++) for(p = sym, h = space; *p; h += *p++)
h *= HASHMUL; h *= HASHMUL;
if(h < 0)
h = ~h;
h %= NHASH; h %= NHASH;
for(s = hash[h]; s; s = s->next) for(s = hash[h]; s; s = s->next)
if((s->space == space) && (strcmp(s->name, sym) == 0)) if((s->space == space) && (strcmp(s->name, sym) == 0))
@ -47,7 +45,7 @@ symlook(char *sym, int space, void *install)
void void
symdel(char *sym, int space) symdel(char *sym, int space)
{ {
long h; ulong h;
char *p; char *p;
Symtab *s, *ls; Symtab *s, *ls;
@ -55,8 +53,6 @@ symdel(char *sym, int space)
for(p = sym, h = space; *p; h += *p++) for(p = sym, h = space; *p; h += *p++)
h *= HASHMUL; h *= HASHMUL;
if(h < 0)
h = ~h;
h %= NHASH; h %= NHASH;
for(s = hash[h], ls = 0; s; ls = s, s = s->next) for(s = hash[h], ls = 0; s; ls = s, s = s->next)
if((s->space == space) && (strcmp(s->name, sym) == 0)){ if((s->space == space) && (strcmp(s->name, sym) == 0)){