From 99c385412ac391b4e3ddec777107689979d871d1 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 10 Oct 2015 22:50:15 +0200 Subject: [PATCH] fix crash in mail_cmd when order is missing a message --- src/buildno.h | 2 +- src/laws.c | 8 ++++---- src/laws.test.c | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/buildno.h b/src/buildno.h index 11bf425d6..7eabd81de 100644 --- a/src/buildno.h +++ b/src/buildno.h @@ -1,3 +1,3 @@ #define VERSION_MAJOR 3 #define VERSION_MINOR 6 -#define VERSION_BUILD 4 +#define VERSION_BUILD 5 diff --git a/src/laws.c b/src/laws.c index 488f6bbda..aedc34dc9 100755 --- a/src/laws.c +++ b/src/laws.c @@ -2027,7 +2027,7 @@ int mail_cmd(unit * u, struct order *ord) } s = getstrtoken(); - if (!s[0]) { + if (!s || !s[0]) { cmistake(u, ord, 30, MSG_MESSAGE); break; } @@ -2050,7 +2050,7 @@ int mail_cmd(unit * u, struct order *ord) } s = getstrtoken(); - if (!s[0]) { + if (!s || !s[0]) { cmistake(u, ord, 30, MSG_MESSAGE); break; } @@ -2082,7 +2082,7 @@ int mail_cmd(unit * u, struct order *ord) s = getstrtoken(); - if (!s[0]) { + if (!s || !s[0]) { cmistake(u, ord, 30, MSG_MESSAGE); break; } @@ -2111,7 +2111,7 @@ int mail_cmd(unit * u, struct order *ord) s = getstrtoken(); - if (!s[0]) { + if (!s || !s[0]) { cmistake(u, ord, 30, MSG_MESSAGE); break; } diff --git a/src/laws.test.c b/src/laws.test.c index a824f4cab..431c03845 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -970,7 +970,7 @@ static void test_ally_cmd(CuTest *tc) { test_cleanup(); } -void test_nmr_warnings(CuTest *tc) { +static void test_nmr_warnings(CuTest *tc) { faction *f1, *f2; test_cleanup(); set_param(&global.parameters, "nmr.timeout", "3"); @@ -989,6 +989,43 @@ void test_nmr_warnings(CuTest *tc) { test_cleanup(); } +static void test_mail_cmd(CuTest *tc) { + unit *u; + order *ord; + faction *f; + struct locale *lang; + + test_cleanup(); + f = test_create_faction(0); + f->locale = lang = get_or_create_locale("de"); + locale_setstring(lang, parameters[P_UNIT], "EINHEIT"); + init_parameters(lang); + u = test_create_unit(f, test_create_region(0, 0, 0)); + ord = create_order(K_MAIL, u->faction->locale, "EINHEIT %s 'Hodor!'", itoa36(u->no)); + mail_cmd(u, ord); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "unitmessage")); + test_cleanup(); +} + +static void test_mail_cmd_no_msg(CuTest *tc) { + unit *u; + order *ord; + faction *f; + struct locale *lang; + + test_cleanup(); + f = test_create_faction(0); + f->locale = lang = get_or_create_locale("de"); + locale_setstring(lang, parameters[P_UNIT], "EINHEIT"); + init_parameters(lang); + u = test_create_unit(f, test_create_region(0, 0, 0)); + ord = create_order(K_MAIL, u->faction->locale, "EINHEIT %s", itoa36(u->no)); + mail_cmd(u, ord); + CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "unitmessage")); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error30")); + test_cleanup(); +} + CuSuite *get_laws_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -1033,6 +1070,8 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_force_leave_ships); SUITE_ADD_TEST(suite, test_force_leave_ships_on_ocean); SUITE_ADD_TEST(suite, test_peasant_luck_effect); + SUITE_ADD_TEST(suite, test_mail_cmd); + SUITE_ADD_TEST(suite, test_mail_cmd_no_msg); (void)test_luck_message; /* disabled, breaks on travis */ return suite;