From ff09defa695b9d1f0f7ecb3471fc5714f8e7d7ec Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 11 Nov 2016 01:59:43 +0100 Subject: [PATCH] prevent badly naming units/factions/regions. --- src/laws.c | 14 +++++++------- src/laws.test.c | 26 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/laws.c b/src/laws.c index 9e53a465f..de87afeb1 100644 --- a/src/laws.c +++ b/src/laws.c @@ -79,17 +79,17 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include #include #include -#include #include #include #include -#include -#include +#include #include +#include #include /* libc includes */ #include @@ -1627,6 +1627,7 @@ bool renamed_building(const building * b) static int rename_cmd(unit * u, order * ord, char **s, const char *s2) { + char name[NAMESIZE]; assert(s2); if (!s2[0]) { cmistake(u, ord, 84, MSG_EVENT); @@ -1635,12 +1636,11 @@ static int rename_cmd(unit * u, order * ord, char **s, const char *s2) /* TODO: Validate to make sure people don't have illegal characters in * names, phishing-style? () come to mind. */ + strlcpy(name, s2, sizeof(name)); + unicode_utf8_trim(name); free(*s); - *s = _strdup(s2); - if (strlen(s2) >= NAMESIZE) { - (*s)[NAMESIZE] = 0; - } + *s = _strdup(name); return 0; } diff --git a/src/laws.test.c b/src/laws.test.c index 65f48012d..0f3566714 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -1024,7 +1024,7 @@ static void test_ally_cmd_errors(CuTest *tc) { int fid; order *ord; - test_cleanup(); + test_setup(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); fid = u->faction->no + 1; CuAssertPtrEquals(tc, 0, findfaction(fid)); @@ -1037,12 +1037,33 @@ static void test_ally_cmd_errors(CuTest *tc) { test_cleanup(); } +static void test_name_cmd(CuTest *tc) { + unit *u; + faction *f; + order *ord; + + test_setup(); + u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); + + ord = create_order(K_NAME, f->locale, "%s ' Ho\tdor '", LOC(f->locale, parameters[P_UNIT])); + name_cmd(u, ord); + CuAssertStrEquals(tc, "Hodor", u->_name); + free_order(ord); + + ord = create_order(K_NAME, f->locale, "%s ' Ho\tdor '", LOC(f->locale, parameters[P_FACTION])); + name_cmd(u, ord); + CuAssertStrEquals(tc, "Hodor", f->name); + free_order(ord); + + test_cleanup(); +} + static void test_ally_cmd(CuTest *tc) { unit *u; faction * f; order *ord; - test_cleanup(); + test_setup(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); f = test_create_faction(0); @@ -1444,6 +1465,7 @@ CuSuite *get_laws_suite(void) CuSuite *suite = CuSuiteNew(); 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_ally_cmd_errors); SUITE_ADD_TEST(suite, test_long_order_normal); SUITE_ADD_TEST(suite, test_long_order_none);