From 7854684d8b1f3fa008e94ce77deb581fdcf508f5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 13 Oct 2015 15:45:38 +0200 Subject: [PATCH] fix memory leaks in all tests that create an order without calling free_order at the end. --- src/economy.test.c | 8 ++------ src/give.test.c | 3 +++ src/kernel/jsonconf.test.c | 1 + src/kernel/order.test.c | 1 + src/keyword.test.c | 1 + src/laws.test.c | 22 ++++++++++++++++------ src/piracy.test.c | 2 ++ src/spy.test.c | 3 +++ 8 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/economy.test.c b/src/economy.test.c index fe31e5976..08b8bea56 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -141,14 +141,12 @@ static struct unit *create_recruiter(void) { static void test_heroes_dont_recruit(CuTest * tc) { unit *u; - order *ord; test_cleanup(); u = create_recruiter(); fset(u, UFL_HERO); - ord = create_order(K_RECRUIT, default_locale, "1"); - unit_addorder(u, ord); + unit_addorder(u, create_order(K_RECRUIT, default_locale, "1")); economics(u->region); @@ -160,13 +158,11 @@ static void test_heroes_dont_recruit(CuTest * tc) { static void test_normals_recruit(CuTest * tc) { unit *u; - order *ord; test_cleanup(); u = create_recruiter(); - ord = create_order(K_RECRUIT, default_locale, "1"); - unit_addorder(u, ord); + unit_addorder(u, create_order(K_RECRUIT, default_locale, "1")); economics(u->region); diff --git a/src/give.test.c b/src/give.test.c index c9e005b8f..401cd1fb8 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -217,6 +217,7 @@ static void test_give_men_requires_contact(CuTest * tc) { give_cmd(env.src, ord); CuAssertPtrEquals(tc, 0, test_find_messagetype(env.f1->msgs, "give_person")); CuAssertPtrNotNull(tc, test_find_messagetype(env.f1->msgs, "feedback_no_contact")); + free_order(ord); test_cleanup(); } @@ -290,6 +291,7 @@ static void test_give_herbs(CuTest * tc) { give_cmd(env.src, ord); CuAssertIntEquals(tc, 0, i_get(env.src->items, env.itype)); CuAssertIntEquals(tc, 10, i_get(env.dst->items, env.itype)); + free_order(ord); test_cleanup(); } @@ -342,6 +344,7 @@ static void test_give_invalid_target(CuTest *tc) { give_cmd(env.src, ord); CuAssertIntEquals(tc, 10, i_get(env.src->items, env.itype)); CuAssertPtrNotNull(tc, test_find_messagetype(env.f1->msgs, "feedback_unit_not_found")); + free_order(ord); test_cleanup(); } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index d7a4ad0cd..2d9394749 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -577,6 +577,7 @@ static void test_infinitive_from_config(CuTest *tc) { ord = create_order(K_STUDY, lang, ""); CuAssertStrEquals(tc, "LERNE", get_command(ord, buffer, sizeof(buffer))); + free_order(ord); test_cleanup(); } diff --git a/src/kernel/order.test.c b/src/kernel/order.test.c index 6900da420..84dc61323 100644 --- a/src/kernel/order.test.c +++ b/src/kernel/order.test.c @@ -123,6 +123,7 @@ static void test_init_order(CuTest *tc) { CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); CuAssertStrEquals(tc, "hurr", getstrtoken()); CuAssertStrEquals(tc, "durr", getstrtoken()); + free_order(ord); } static void test_getstrtoken(CuTest *tc) { diff --git a/src/keyword.test.c b/src/keyword.test.c index e0274d9cf..da87c860a 100644 --- a/src/keyword.test.c +++ b/src/keyword.test.c @@ -35,6 +35,7 @@ static void test_infinitive(CuTest *tc) { ord = create_order(K_STUDY, lang, ""); CuAssertStrEquals(tc, "LERNE", get_command(ord, buffer, sizeof(buffer))); + free_order(ord); test_cleanup(); } diff --git a/src/laws.test.c b/src/laws.test.c index 5918001d9..474f95276 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -467,6 +467,7 @@ static void test_reserve_cmd(CuTest *tc) { CuAssertIntEquals(tc, 200, reserve_cmd(u1, ord)); CuAssertIntEquals(tc, 200, i_get(u1->items, rtype->itype)); CuAssertIntEquals(tc, 0, i_get(u2->items, rtype->itype)); + free_order(ord); test_cleanup(); } @@ -517,6 +518,7 @@ static void test_pay_cmd(CuTest *tc) { assert(ord); CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord)); CuAssertIntEquals(tc, BLD_DONTPAY, b->flags&BLD_DONTPAY); + free_order(ord); test_cleanup(); } @@ -541,6 +543,7 @@ static void test_pay_cmd_other_building(CuTest *tc) { CuAssertPtrEquals(tc, fix.u1, building_owner(b)); CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord)); CuAssertIntEquals(tc, BLD_DONTPAY, b->flags&BLD_DONTPAY); + free_order(ord); test_cleanup(); } @@ -559,6 +562,7 @@ static void test_pay_cmd_must_be_owner(CuTest *tc) { assert(ord); CuAssertIntEquals(tc, 0, pay_cmd(fix.u2, ord)); CuAssertIntEquals(tc, 0, b->flags&BLD_DONTPAY); + free_order(ord); test_cleanup(); } @@ -566,8 +570,8 @@ static void test_new_units(CuTest *tc) { unit *u; faction *f; region *r; - order *ord; const struct locale *loc; + test_cleanup(); test_create_world(); f = test_create_faction(NULL); @@ -577,9 +581,7 @@ static void test_new_units(CuTest *tc) { assert(u && !u->next); loc = get_locale("de"); assert(loc); - ord = create_order(K_MAKETEMP, loc, "hurr"); - assert(ord); - u->orders = ord; + u->orders = create_order(K_MAKETEMP, loc, "hurr"); new_units(); CuAssertPtrNotNull(tc, u->next); test_cleanup(); @@ -708,6 +710,7 @@ static void test_reserve_self(CuTest *tc) { CuAssertIntEquals(tc, 100, reserve_self(u1, ord)); CuAssertIntEquals(tc, 100, i_get(u1->items, rtype->itype)); CuAssertIntEquals(tc, 100, i_get(u2->items, rtype->itype)); + free_order(ord); test_cleanup(); } @@ -879,13 +882,13 @@ static void test_long_order_normal(CuTest *tc) { // TODO: write more tests unit *u; order *ord; + test_cleanup(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); fset(u, UFL_MOVED); fset(u, UFL_LONGACTION); u->faction->locale = get_or_create_locale("de"); - ord = create_order(K_MOVE, u->faction->locale, 0); - unit_addorder(u, ord); + unit_addorder(u, ord = create_order(K_MOVE, u->faction->locale, 0)); update_long_order(u); CuAssertPtrEquals(tc, ord->data, u->thisorder->data); CuAssertIntEquals(tc, 0, fval(u, UFL_MOVED)); @@ -1121,6 +1124,7 @@ static void test_mail_unit(CuTest *tc) { ord = create_order(K_MAIL, u->faction->locale, "EINHEIT %s 'Hodor!'", itoa36(u->no)); mail_cmd(u, ord); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "unitmessage")); + free_order(ord); test_cleanup(); } @@ -1132,6 +1136,7 @@ static void test_mail_faction(CuTest *tc) { ord = create_order(K_MAIL, u->faction->locale, "PARTEI %s 'Hodor!'", itoa36(u->faction->no)); mail_cmd(u, ord); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "regionmessage")); + free_order(ord); test_cleanup(); } @@ -1143,6 +1148,7 @@ static void test_mail_region(CuTest *tc) { ord = create_order(K_MAIL, u->faction->locale, "REGION 'Hodor!'", itoa36(u->no)); mail_cmd(u, ord); CuAssertPtrNotNull(tc, test_find_messagetype(u->region->msgs, "mail_result")); + free_order(ord); test_cleanup(); } @@ -1155,6 +1161,7 @@ static void test_mail_unit_no_msg(CuTest *tc) { mail_cmd(u, ord); CuAssertPtrEquals(tc, 0, test_find_messagetype(u->faction->msgs, "unitmessage")); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error30")); + free_order(ord); test_cleanup(); } @@ -1167,6 +1174,7 @@ static void test_mail_faction_no_msg(CuTest *tc) { mail_cmd(u, ord); CuAssertPtrEquals(tc, 0, test_find_messagetype(u->faction->msgs, "regionmessage")); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error30")); + free_order(ord); test_cleanup(); } @@ -1179,6 +1187,7 @@ static void test_mail_faction_no_target(CuTest *tc) { mail_cmd(u, ord); CuAssertPtrEquals(tc, 0, test_find_messagetype(u->faction->msgs, "regionmessage")); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error66")); + free_order(ord); test_cleanup(); } @@ -1191,6 +1200,7 @@ static void test_mail_region_no_msg(CuTest *tc) { mail_cmd(u, ord); CuAssertPtrEquals(tc, 0, test_find_messagetype(u->region->msgs, "mail_result")); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error30")); + free_order(ord); test_cleanup(); } diff --git a/src/piracy.test.c b/src/piracy.test.c index c521b1be6..9074c1e33 100644 --- a/src/piracy.test.c +++ b/src/piracy.test.c @@ -61,6 +61,7 @@ static void test_piracy_cmd(CuTest * tc) { CuAssertPtrNotNullMsg(tc, "successful PIRACY sets attribute", r->attribs); // FIXME: this is testing implementation, not interface CuAssertPtrNotNullMsg(tc, "successful PIRACY message", test_find_messagetype(f->msgs, "piratesawvictim")); CuAssertPtrNotNullMsg(tc, "successful PIRACY movement", test_find_messagetype(f->msgs, "shipsail")); + free_order(ord); test_cleanup(); } @@ -93,6 +94,7 @@ static void test_piracy_cmd_errors(CuTest * tc) { test_clear_messages(f); piracy_cmd(u, ord); CuAssertPtrNotNullMsg(tc, "must specify target for PIRACY", test_find_messagetype(f->msgs, "piratenovictim")); + free_order(ord); test_cleanup(); } diff --git a/src/spy.test.c b/src/spy.test.c index 95f1a6dbc..00a1767d0 100644 --- a/src/spy.test.c +++ b/src/spy.test.c @@ -110,6 +110,7 @@ static void test_sabotage_self(CuTest *tc) { assert(ord); CuAssertIntEquals(tc, 0, sabotage_cmd(u, ord)); CuAssertPtrEquals(tc, 0, r->ships); + free_order(ord); test_cleanup(); } @@ -139,6 +140,7 @@ static void test_sabotage_other_fail(CuTest *tc) { msg = test_get_last_message(u->faction->msgs); CuAssertStrEquals(tc, "destroy_ship_3", test_get_messagetype(msg)); CuAssertPtrNotNull(tc, r->ships); + free_order(ord); test_cleanup(); } @@ -164,6 +166,7 @@ static void test_sabotage_other_success(CuTest *tc) { set_level(u2, SK_SPY, 1); CuAssertIntEquals(tc, 0, sabotage_cmd(u2, ord)); CuAssertPtrEquals(tc, 0, r->ships); + free_order(ord); test_cleanup(); }