From daca71a486490125471d0fb8d596d004485179bf Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Sun, 5 Jun 2011 13:56:34 +0300 Subject: Fix memory corruption, improve conjugator * data/db.struct: Revamp conjugation support tables. * scm/conjugator.scm: Improve irregular conjugation support. * src/ellinika/elmorph.c (_elstr_syllabize): Use calloc. Fix memory corruption. (_elstr_dup): Don't allocate new sylmap if the source one is NULL. (_elstr_free): Call scm_gc_free on elstr. --- src/ellinika/elmorph.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'src/ellinika') diff --git a/src/ellinika/elmorph.c b/src/ellinika/elmorph.c index 75e42f9..faeeb49 100644 --- a/src/ellinika/elmorph.c +++ b/src/ellinika/elmorph.c @@ -40,13 +40,16 @@ static void _elstr_syllabize(struct elstr *elstr) { unsigned *sylmap; - unsigned i, nsyl = 0, accsyl = 0, accchr = 0; + unsigned i, nsyl = 0, accchr = 0; + int accsyl = -1; int dstate = 0; int acc = 0; - if (!elstr->sylmap) - elstr->sylmap = scm_gc_malloc(sizeof(sylmap[0])*elstr->len, - "syllable map"); + if (!elstr->sylmap) { + elstr->sylmap = calloc(elstr->len, sizeof(sylmap[0])); + if (!elstr->sylmap) + scm_memory_error("_elstr_syllabize"); + } sylmap = elstr->sylmap; for (i = 0; i < elstr->len; i++) { @@ -67,11 +70,11 @@ _elstr_syllabize(struct elstr *elstr) } if (dstate) sylmap[nsyl++] = i - 1; - else + else if (nsyl) sylmap[nsyl-1] = i - 1; elstr->nsyl = nsyl; elstr->acc_pos = accchr; - elstr->acc_syl = nsyl - accsyl; + elstr->acc_syl = (accsyl >= 0) ? nsyl - accsyl : 0; } static SCM @@ -114,11 +117,14 @@ _elstr_dup(struct elstr *elstr) elnew->str = calloc(elstr->len, sizeof(elnew->str[0])); if (!elnew->str) scm_memory_error("_elstr_dup"); - elnew->sylmap = calloc(elstr->nsyl, sizeof(elnew->sylmap[0])); - if (!elnew->sylmap) { - free(elnew->str); - scm_memory_error("_elstr_dup"); - } + if (elstr->sylmap) { + elnew->sylmap = calloc(elstr->nsyl, sizeof(elnew->sylmap[0])); + if (!elnew->sylmap) { + free(elnew->str); + scm_memory_error("_elstr_dup"); + } + } else + elnew->sylmap = NULL; memcpy(elnew->str, elstr->str, sizeof(elstr->str[0]) * elstr->len); elnew->len = elstr->len; elnew->nsyl = elstr->nsyl; @@ -151,7 +157,7 @@ _elstr_free(SCM smob) struct elstr *elstr = (struct elstr *) SCM_CDR(smob); free(elstr->str); free(elstr->sylmap); - free(elstr); + scm_gc_free(elstr, sizeof(struct elstr), "elstr"); return 0; } -- cgit v1.2.1