From 4097e43ce6f14a9144e36121b8b80475dadf0508 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 20 Dec 2014 22:18:38 +0100 Subject: [PATCH] DESCRIBE would crash if not given a description. Added a test for DESCRIBE UNIT. --- src/laws.c | 11 ++++++++--- src/laws.test.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/laws.c b/src/laws.c index 6085f5f33..968f36137 100755 --- a/src/laws.c +++ b/src/laws.c @@ -1634,9 +1634,14 @@ int display_cmd(unit * u, struct order *ord) const char *s2 = getstrtoken(); free(*s); - *s = _strdup(s2); - if (strlen(s2) >= DISPLAYSIZE) { - (*s)[DISPLAYSIZE] = 0; + if (s2) { + *s = _strdup(s2); + if (strlen(s2) >= DISPLAYSIZE) { + (*s)[DISPLAYSIZE] = 0; + } + } + else { + *s = 0; } } diff --git a/src/laws.test.c b/src/laws.test.c index f4ae84a16..9f91ffc77 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -198,6 +198,34 @@ static void test_enter_ship(CuTest * tc) test_cleanup(); } +static void test_display_cmd(CuTest *tc) { + unit *u; + faction *f; + region *r; + order *ord; + + test_cleanup(); + r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION)); + f = test_create_faction(0); + f->locale = get_or_create_locale("de"); + assert(r && f); + test_translate_param(f->locale, P_UNIT, "EINHEIT"); + u = test_create_unit(f, r); + assert(u); + + ord = create_order(K_DISPLAY, f->locale, "EINHEIT Hodor"); + CuAssertIntEquals(tc, 0, display_cmd(u, ord)); + CuAssertStrEquals(tc, "Hodor", u->display); + free_order(ord); + + ord = create_order(K_DISPLAY, f->locale, "EINHEIT"); + CuAssertIntEquals(tc, 0, display_cmd(u, ord)); + CuAssertPtrEquals(tc, NULL, u->display); + free_order(ord); + + test_cleanup(); +} + static void test_fishing_feeds_2_people(CuTest * tc) { const resource_type *rtype; @@ -620,5 +648,6 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_contact); SUITE_ADD_TEST(suite, test_enter_building); SUITE_ADD_TEST(suite, test_enter_ship); + SUITE_ADD_TEST(suite, test_display_cmd); return suite; }