fix(data): reject partial and malformed map data

This commit is contained in:
2026-08-14 23:09:55 +09:00
parent 38475318db
commit bfe83d0119
10 changed files with 220 additions and 125 deletions

View File

@@ -3,29 +3,41 @@
void
trie_exact_prefix_and_duplicate(struct ct *t)
{
char *v, *sentinel;
static const struct {
char *key;
int klen;
int match;
char *want;
} cases[] = {
{ "a", 1, TrieExact, "alpha" },
{ "dupli", 5, TriePrefix, nil },
{ "duplicate", 9, TrieExact, "second" },
{ "missing", 7, TrieMiss, nil },
{ nil, 1, TrieMiss, nil },
{ "a", -1, TrieMiss, nil },
};
char *v;
Trie *trie;
int n;
int i, match, n;
trie = trieopen("data/trie.map");
v = trieget(trie, "a", 1, &n);
if(!CT_CHECK(t, v != nil))
goto cleanup;
CT_EQ_INT(t, 5, n);
CT_EQ_MEM(t, "alpha", v, n);
sentinel = "unchanged";
v = sentinel;
n = 77;
CT_CHECK(t, trielookup(trie, "dupli", 5, &v, &n));
CT_EQ_PTR(t, sentinel, v);
CT_EQ_INT(t, 77, n);
v = trieget(trie, "duplicate", 9, &n);
if(!CT_CHECK(t, v != nil))
goto cleanup;
CT_EQ_INT(t, 6, n);
CT_EQ_MEM(t, "second", v, n);
CT_EQ_PTR(t, nil, trieget(trie, "missing", 7, &n));
cleanup:
for(i = 0; i < nelem(cases); i++){
v = "unchanged";
n = 77;
match = trielookup(trie, cases[i].key, cases[i].klen, &v, &n);
if(match != cases[i].match){
CT_ERRORF(t, "case %d: want match %d, got %d",
i, cases[i].match, match);
continue;
}
if(cases[i].want == nil){
CT_EQ_PTR(t, nil, v);
CT_EQ_INT(t, 0, n);
}else{
CT_EQ_INT(t, strlen(cases[i].want), n);
CT_EQ_MEM(t, cases[i].want, v, n);
}
}
trieclose(trie);
}
@@ -37,18 +49,15 @@ trie_optional_outputs_and_invalid_lengths(struct ct *t)
int n;
trie = trieopen("data/trie.map");
CT_CHECK(t, trieget(trie, "a", 1, nil) != nil);
v = nil;
CT_CHECK(t, trielookup(trie, "a", 1, &v, nil));
CT_EQ_INT(t, TrieExact, trielookup(trie, "a", 1, &v, nil));
CT_CHECK(t, v != nil);
n = -1;
CT_CHECK(t, trielookup(trie, "a", 1, nil, &n));
CT_EQ_INT(t, TrieExact, trielookup(trie, "a", 1, nil, &n));
CT_EQ_INT(t, 5, n);
CT_CHECK(t, trielookup(trie, "dupli", 5, nil, nil));
CT_EQ_PTR(t, nil, trieget(trie, nil, 1, &n));
CT_EQ_PTR(t, nil, trieget(trie, "a", -1, &n));
CT_CHECK(t, !trielookup(trie, nil, 1, &v, &n));
CT_CHECK(t, !trielookup(trie, "a", -1, &v, &n));
CT_EQ_INT(t, TriePrefix,
trielookup(trie, "dupli", 5, nil, nil));
CT_EQ_INT(t, TriePrefix, trielookup(trie, nil, 0, &v, &n));
CT_EQ_PTR(t, nil, v);
CT_EQ_INT(t, 0, n);
trieclose(trie);
}