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

11
dict.c
View File

@@ -64,13 +64,15 @@ dictopen(char *path)
Biobuf *b;
Str key;
char *line, *tab;
int len;
int len, lineno;
b = Bopen(path, OREAD);
if(b == nil)
die("can't open: %s", path);
h = hmapalloc(4096);
lineno = 0;
while((line = Brdstr(b, '\n', 1)) != nil){
lineno++;
len = strlen(line);
if(len > 0 && line[len-1] == '\r')
line[--len] = '\0';
@@ -84,6 +86,8 @@ dictopen(char *path)
continue;
}
*tab = '\0';
if(utflen(line) > Maxrunes)
die("dictionary key too long: %s:%d", path, lineno);
sinit(&key, line, tab - line);
hmapset(&h, &key, tab+1, len - (tab - line) - 1);
free(line);
@@ -101,7 +105,10 @@ dictinit(char *dir)
for(i = 0; i < nlang; i++){
if(langs[i].dictname == nil)
continue;
snprint(path, sizeof(path), "%s/%s.dict", dir, langs[i].dictname);
if(snprint(path, sizeof(path), "%s/%s.dict", dir,
langs[i].dictname) >= (int)sizeof path)
die("dictionary path too long: %s/%s.dict",
dir, langs[i].dictname);
langs[i].dict = dictopen(path);
}
}