diff --git a/src/kernel/save.c b/src/kernel/save.c index 26b7a12ab..9dc0030e0 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1076,7 +1076,8 @@ void write_faction(gamedata *data, const faction * f) ursprung *ur; assert(f->_alive); - write_faction_reference(f, data->store); + assert(f->no > 0 && f->no <= MAX_UNIT_NR); + WRITE_INT(data->store, f->no); WRITE_INT(data->store, f->subscription); #if RELEASE_VERSION >= SPELL_LEVEL_VERSION WRITE_INT(data->store, f->max_spelllevel); diff --git a/src/renumber.c b/src/renumber.c index ec9d61c66..1319eaa5f 100644 --- a/src/renumber.c +++ b/src/renumber.c @@ -88,11 +88,15 @@ int renumber_cmd(unit * u, order * ord) s = gettoken(token, sizeof(token)); if (s && *s) { int id = atoi36((const char *)s); - attrib *a = a_find(f->attribs, &at_number); - if (!a) - a = a_add(&f->attribs, a_new(&at_number)); - a->data.i = id; + if (id > 0 && id <= MAX_UNIT_NR) { + attrib *a = a_find(f->attribs, &at_number); + if (!a) + a = a_add(&f->attribs, a_new(&at_number)); + a->data.i = id; + break; + } } + cmistake(u, ord, 114, MSG_EVENT); break; case P_UNIT: diff --git a/src/renumber.test.c b/src/renumber.test.c index dfac52e8c..11fb022aa 100644 --- a/src/renumber.test.c +++ b/src/renumber.test.c @@ -51,6 +51,37 @@ static void test_renumber_faction_duplicate(CuTest *tc) { test_teardown(); } +static void test_renumber_faction_invalid(CuTest *tc) { + unit *u; + faction *f; + int no; + const struct locale *lang; + + test_setup_ex(tc); + u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); + no = f->no; + lang = f->locale; + u->thisorder = create_order(K_NUMBER, lang, "%s [halima]", LOC(lang, parameters[P_FACTION])); + renumber_cmd(u, u->thisorder); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error114")); + renumber_factions(); + CuAssertIntEquals(tc, no, f->no); + + test_clear_messages(f); + free_order(u->thisorder); + u->thisorder = create_order(K_NUMBER, lang, "%s 10000", LOC(lang, parameters[P_FACTION])); + renumber_cmd(u, u->thisorder); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error114")); + + test_clear_messages(f); + free_order(u->thisorder); + u->thisorder = create_order(K_NUMBER, lang, "%s 0", LOC(lang, parameters[P_FACTION])); + renumber_cmd(u, u->thisorder); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error114")); + + test_cleanup(); +} + static void test_renumber_building(CuTest *tc) { unit *u; int uno, no; @@ -232,5 +263,6 @@ CuSuite *get_renumber_suite(void) SUITE_ADD_TEST(suite, test_renumber_ship_duplicate); SUITE_ADD_TEST(suite, test_renumber_faction); SUITE_ADD_TEST(suite, test_renumber_faction_duplicate); + SUITE_ADD_TEST(suite, test_renumber_faction_invalid); return suite; }