jdict

command line tool for looking up terms in yomidict dictionaries
git clone anongit@rnpnr.xyz:jdict.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 7d70a88259439455fb3094ed7dc8629c06940311
Parent: a7f1aab739bc4ced8b7aa2bab001aff5d3929a0f
Author: Randy Palamar
Date:   Sun, 19 Jun 2022 09:57:32 -0600

make make_ent() slightly less hacky

now it checks that the toks beyond what was passed are actually allocated
instead of assuming that they are

Diffstat:
Mjdict.c | 16+++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/jdict.c b/jdict.c @@ -44,20 +44,22 @@ usage(void) /* takes a token of type YOMI_ENTRY and creates a DictEnt */ static DictEnt * -make_ent(YomiTok *tok, char *data) +make_ent(YomiTok *toks, size_t ntoks, char *data) { size_t i; DictEnt *d; YomiTok *tstr, *tdefs; - if (tok->type != YOMI_ENTRY) + if (toks[0].type != YOMI_ENTRY) return NULL; /* FIXME: hacky but works */ - /* term = YOMI_ENT tok + 1 */ - tstr = tok + 1; - /* definition array = YOMI_ENT tok + 6 */ - tdefs = tok + 6; + /* definition array = YOMI_ENTRY tok + 6 */ + if (ntoks - 6 < 0) + return NULL; + tdefs = toks + 6; + /* term = YOMI_ENTRY tok + 1 */ + tstr = toks + 1; d = xreallocarray(NULL, 1, sizeof(DictEnt)); d->term = strndup(data + tstr->start, tstr->end - tstr->start); @@ -113,7 +115,7 @@ parse_term_bank(DictEnt *ents, size_t *nents, const char *tbank, size_t stride) ents = xreallocarray(ents, (*nents) + r/YOMI_TOKS_PER_ENT, sizeof(DictEnt)); for (i = 0; i < r; i++) { if (toks[i].type == YOMI_ENTRY) { - e = make_ent(&toks[i], data); + e = make_ent(&toks[i], r - i, data); if (e == NULL) return NULL; memcpy(&ents[(*nents)++], e, sizeof(DictEnt));