From 0281ebc44b159178594fa58ea06e8e9c995be529 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 14 Jul 2019 09:26:32 +0200 Subject: [PATCH] Bug 2596: Crash durch leeres Banner https://bugs.eressea.de/view.php?id=2596 --- res/core/messages.xml | 7 ++++ res/translations/messages.de.po | 3 ++ res/translations/messages.en.po | 3 ++ src/laws.c | 9 +++-- src/laws.test.c | 64 +++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/res/core/messages.xml b/res/core/messages.xml index fb6cd45bd..766cd481c 100644 --- a/res/core/messages.xml +++ b/res/core/messages.xml @@ -4391,6 +4391,13 @@ + + + + + + + diff --git a/res/translations/messages.de.po b/res/translations/messages.de.po index 21586ac31..a1d30c85c 100644 --- a/res/translations/messages.de.po +++ b/res/translations/messages.de.po @@ -689,6 +689,9 @@ msgstr "\"$unit($unit) in $region($region): '$order($command)' - Die Einheit kan msgid "error85" msgstr "\"$unit($unit) in $region($region): '$order($command)' - Es wurde keine Emailadresse angegeben.\"" +msgid "error125" +msgstr "\"$unit($unit) in $region($region): '$order($command)' - Es wurde kein Banner angegeben.\"" + msgid "starvation" msgstr "\"$unit($unit) verliert in $region($region) $int($dead) von $int($add($live,$dead)) Personen durch Unterernährung.\"" diff --git a/res/translations/messages.en.po b/res/translations/messages.en.po index 095a83212..037b50d4d 100644 --- a/res/translations/messages.en.po +++ b/res/translations/messages.en.po @@ -689,6 +689,9 @@ msgstr "\"$unit($unit) in $region($region): '$order($command)' - The unit cannot msgid "error85" msgstr "\"$unit($unit) in $region($region): '$order($command)' - No email address was supplied.\"" +msgid "error125" +msgstr "\"$unit($unit) in $region($region): '$order($command)' - No banner text was supplied.\"" + msgid "starvation" msgstr "\"$unit($unit) loses $int($dead) of $int($add($live,$dead)) people due to starvation in $region($region).\"" diff --git a/src/laws.c b/src/laws.c index a07c7d517..920de6b03 100644 --- a/src/laws.c +++ b/src/laws.c @@ -2167,8 +2167,13 @@ int banner_cmd(unit * u, struct order *ord) init_order_depr(ord); s = getstrtoken(); - faction_setbanner(u->faction, s); - ADDMSG(&u->faction->msgs, msg_message("changebanner", "value", s)); + if (!s || !s[0]) { + cmistake(u, ord, 125, MSG_EVENT); + } + else { + faction_setbanner(u->faction, s); + ADDMSG(&u->faction->msgs, msg_message("changebanner", "value", s)); + } return 0; } diff --git a/src/laws.test.c b/src/laws.test.c index adda8f5ab..58a6c9a6e 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -1248,6 +1248,68 @@ static void test_ally_cmd_errors(CuTest *tc) { test_teardown(); } +static void test_banner_cmd(CuTest *tc) { + unit *u; + faction *f; + order *ord; + + test_setup(); + mt_create_error(125); + mt_create_va(mt_new("changebanner", NULL), "value:string", MT_NEW_END); + u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); + + ord = create_order(K_BANNER, f->locale, "Hodor!"); + banner_cmd(u, ord); + CuAssertStrEquals(tc, "Hodor!", faction_getbanner(f)); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "changebanner")); + free_order(ord); + test_clear_messages(f); + + ord = create_order(K_BANNER, f->locale, NULL); + banner_cmd(u, ord); + CuAssertStrEquals(tc, "Hodor!", faction_getbanner(f)); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error125")); + free_order(ord); + test_clear_messages(f); + + test_teardown(); +} + +static void test_email_cmd(CuTest *tc) { + unit *u; + faction *f; + order *ord; + + test_setup(); + mt_create_error(85); + mt_create_va(mt_new("changemail", NULL), "value:string", MT_NEW_END); + mt_create_va(mt_new("changemail_invalid", NULL), "value:string", MT_NEW_END); + u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); + + ord = create_order(K_EMAIL, f->locale, "hodor@example.com"); + email_cmd(u, ord); + CuAssertStrEquals(tc, "hodor@example.com", f->email); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "changemail")); + free_order(ord); + test_clear_messages(f); + + ord = create_order(K_EMAIL, f->locale, "example.com"); + email_cmd(u, ord); + CuAssertStrEquals(tc, "hodor@example.com", f->email); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "changemail_invalid")); + free_order(ord); + test_clear_messages(f); + + ord = create_order(K_EMAIL, f->locale, NULL); + email_cmd(u, ord); + CuAssertStrEquals(tc, "hodor@example.com", f->email); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error85")); + free_order(ord); + test_clear_messages(f); + + test_teardown(); +} + static void test_name_cmd(CuTest *tc) { unit *u; faction *f; @@ -2046,6 +2108,8 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_nmr_warnings); SUITE_ADD_TEST(suite, test_ally_cmd); SUITE_ADD_TEST(suite, test_name_cmd); + SUITE_ADD_TEST(suite, test_banner_cmd); + SUITE_ADD_TEST(suite, test_email_cmd); SUITE_ADD_TEST(suite, test_name_cmd_2274); SUITE_ADD_TEST(suite, test_name_unit); SUITE_ADD_TEST(suite, test_name_region);