prevent badly naming units/factions/regions.

This commit is contained in:
Enno Rehling 2016-11-11 01:59:43 +01:00
parent e0add2275f
commit ff09defa69
2 changed files with 31 additions and 9 deletions

View File

@ -79,17 +79,17 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/language.h> #include <util/language.h>
#include <util/lists.h> #include <util/lists.h>
#include <util/log.h> #include <util/log.h>
#include <util/message.h>
#include <util/parser.h> #include <util/parser.h>
#include <util/password.h> #include <util/password.h>
#include <quicklist.h>
#include <util/rand.h> #include <util/rand.h>
#include <util/rng.h> #include <util/rng.h>
#include <util/umlaut.h> #include <util/umlaut.h>
#include <util/message.h> #include <util/unicode.h>
#include <util/rng.h>
#include <attributes/otherfaction.h> #include <attributes/otherfaction.h>
#include <quicklist.h>
#include <iniparser.h> #include <iniparser.h>
/* libc includes */ /* libc includes */
#include <assert.h> #include <assert.h>
@ -1627,6 +1627,7 @@ bool renamed_building(const building * b)
static int rename_cmd(unit * u, order * ord, char **s, const char *s2) static int rename_cmd(unit * u, order * ord, char **s, const char *s2)
{ {
char name[NAMESIZE];
assert(s2); assert(s2);
if (!s2[0]) { if (!s2[0]) {
cmistake(u, ord, 84, MSG_EVENT); 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 /* TODO: Validate to make sure people don't have illegal characters in
* names, phishing-style? () come to mind. */ * names, phishing-style? () come to mind. */
strlcpy(name, s2, sizeof(name));
unicode_utf8_trim(name);
free(*s); free(*s);
*s = _strdup(s2); *s = _strdup(name);
if (strlen(s2) >= NAMESIZE) {
(*s)[NAMESIZE] = 0;
}
return 0; return 0;
} }

View File

@ -1024,7 +1024,7 @@ static void test_ally_cmd_errors(CuTest *tc) {
int fid; int fid;
order *ord; order *ord;
test_cleanup(); test_setup();
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
fid = u->faction->no + 1; fid = u->faction->no + 1;
CuAssertPtrEquals(tc, 0, findfaction(fid)); CuAssertPtrEquals(tc, 0, findfaction(fid));
@ -1037,12 +1037,33 @@ static void test_ally_cmd_errors(CuTest *tc) {
test_cleanup(); 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) { static void test_ally_cmd(CuTest *tc) {
unit *u; unit *u;
faction * f; faction * f;
order *ord; order *ord;
test_cleanup(); test_setup();
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
f = test_create_faction(0); f = test_create_faction(0);
@ -1444,6 +1465,7 @@ CuSuite *get_laws_suite(void)
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_nmr_warnings); SUITE_ADD_TEST(suite, test_nmr_warnings);
SUITE_ADD_TEST(suite, test_ally_cmd); 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_ally_cmd_errors);
SUITE_ADD_TEST(suite, test_long_order_normal); SUITE_ADD_TEST(suite, test_long_order_normal);
SUITE_ADD_TEST(suite, test_long_order_none); SUITE_ADD_TEST(suite, test_long_order_none);