fix: harden small core containers

This commit is contained in:
2026-08-12 15:54:16 +09:00
parent cec62cc40f
commit 103f2ec448
11 changed files with 136 additions and 18 deletions

14
hash.c
View File

@@ -25,7 +25,7 @@ hmapalloc(int nbuckets)
int nsz;
if(nbuckets < 1)
die("hmapalloc: no buckets");
return nil;
nsz = Tagsize;
store = emalloc(sizeof(*h) + nbuckets * nsz);
h = store;
@@ -54,6 +54,8 @@ hmapget(Hmap *h, Str *key)
Hnode *n;
uchar *v;
if(h == nil || key == nil || key->n < 0 || key->n > Maxrunes)
return nil;
v = h->nodes + (hash(key) % h->nbs) * h->nsz;
for(;;){
n = (Hnode*)v;
@@ -67,7 +69,7 @@ hmapget(Hmap *h, Str *key)
}
static char*
sdup(Str *s)
sdup(Str *s, int *len)
{
char buf[Maxutf];
char *p;
@@ -77,6 +79,7 @@ sdup(Str *s)
p = emalloc(n + 1);
memmove(p, buf, n);
p[n] = '\0';
*len = n;
return p;
}
@@ -121,6 +124,10 @@ hmapset(Hmap **store, Str *key, const char *val, int vlen)
int next;
vlong diff;
if(store == nil || *store == nil || key == nil ||
key->n < 0 || key->n > Maxrunes || vlen < 0 ||
(vlen > 0 && val == nil))
return;
newval = memdup(val, vlen);
h = *store;
v = h->nodes + (hash(key) % h->nbs) * h->nsz;
@@ -151,8 +158,7 @@ hmapset(Hmap **store, Str *key, const char *val, int vlen)
n = (Hnode*)v;
replace:
if(n->filled == 0){
n->key = sdup(key);
n->klen = strlen(n->key);
n->key = sdup(key, &n->klen);
n->filled = 1;
}
n->next = next;