From 257ae3874973ad9e5c07adc9b4d6e997e76c08b6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 27 Feb 2016 13:10:23 +0100 Subject: [PATCH] fix spellbook leak --- src/kernel/item.c | 13 ++++++++++--- src/kernel/spell.c | 7 +++++-- src/reports.test.c | 11 ++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/kernel/item.c b/src/kernel/item.c index 1265a101d..182e71fbe 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -1188,6 +1188,7 @@ static item *default_spoil(const struct race *rc, int size) } static void free_itype(item_type *itype) { + assert(itype); free(itype->construction); free(itype->_appearance[0]); free(itype->_appearance[1]); @@ -1195,14 +1196,14 @@ static void free_itype(item_type *itype) { } static void free_wtype(weapon_type *wtype) { + assert(wtype); free(wtype->damage[0]); free(wtype->damage[1]); free(wtype); } -int free_rtype_cb(const void * match, const void * key, size_t keylen, void *cbdata) { - resource_type *rtype; - cb_get_kv(match, &rtype, sizeof(rtype)); +void free_rtype(resource_type *rtype) { + assert(rtype); if (rtype->wtype) { free_wtype(rtype->wtype); } @@ -1214,6 +1215,12 @@ int free_rtype_cb(const void * match, const void * key, size_t keylen, void *cbd } free(rtype->_name); free(rtype); +} + +int free_rtype_cb(const void * match, const void * key, size_t keylen, void *cbdata) { + resource_type *rtype; + cb_get_kv(match, &rtype, sizeof(rtype)); + free_rtype(rtype); return 0; } diff --git a/src/kernel/spell.c b/src/kernel/spell.c index ebd5d44ef..ec651bd79 100644 --- a/src/kernel/spell.c +++ b/src/kernel/spell.c @@ -36,8 +36,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static critbit_tree cb_spells; quicklist * spells; -static void free_spell_cb(void *cbdata) { - spell *sp = (spell *)cbdata; +static void free_spell(spell *sp) { free(sp->syntax); free(sp->parameter); free(sp->sname); @@ -45,6 +44,10 @@ static void free_spell_cb(void *cbdata) { free(sp); } +static void free_spell_cb(void *cbdata) { + free_spell((spell *)cbdata); +} + void free_spells(void) { cb_clear(&cb_spells); ql_foreach(spells, free_spell_cb); diff --git a/src/reports.test.c b/src/reports.test.c index e00e50a8b..29adb0915 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -317,6 +317,12 @@ static void setup_spell_fixture(spell_fixture * spf) { spf->sbe = spellbook_get(spf->spb, spf->sp); } +static void cleanup_spell_fixture(spell_fixture *spf) { + spellbook_clear(spf->spb); + free(spf->spb); + test_cleanup(); +} + static void check_spell_syntax(CuTest *tc, char *msg, spell_fixture *spell, char *syntax) { stream strm; char buf[1024]; @@ -405,9 +411,8 @@ static void test_write_spell_syntax(CuTest *tc) { set_parameter(spell, "kc+"); check_spell_syntax(tc, "kc+", &spell, " ZAUBERE \"Testzauber\" ( REGION | EINHEIT [ ...] | SCHIFF \n [ ...] | BURG [ ...] )"); - spellbook_clear(spell.spb); - free(spell.spb); - test_cleanup(); + + cleanup_spell_fixture(&spell); } CuSuite *get_reports_suite(void)