add a test and fix potential crashes in SABOTAGE command.

sometimes even a simple test will uncover a ton of small errors.
This commit is contained in:
Enno Rehling 2015-07-07 09:29:43 +02:00
parent e25d3c8ed1
commit 1e669472a6
2 changed files with 50 additions and 14 deletions

View File

@ -394,7 +394,7 @@ static int try_destruction(unit * u, unit * u2, const ship * sh, int skilldiff)
return 1; /* success */
}
static void sink_ship(region * r, ship * sh, const char *name, unit * saboteur)
static void sink_ship(region * r, ship * sh, unit * saboteur)
{
unit **ui, *u;
region *safety = r;
@ -404,6 +404,9 @@ static void sink_ship(region * r, ship * sh, const char *name, unit * saboteur)
message *sink_msg = NULL;
faction *f;
assert(r);
assert(sh);
assert(saboteur);
for (f = NULL, u = r->units; u; u = u->next) {
/* slight optimization to avoid dereferencing u->faction each time */
if (f != u->faction) {
@ -426,7 +429,7 @@ static void sink_ship(region * r, ship * sh, const char *name, unit * saboteur)
}
}
}
for (ui = &r->units; *ui; ui = &(*ui)->next) {
for (ui = &r->units; *ui;) {
unit *u = *ui;
/* inform this faction about the sinking ship: */
@ -471,12 +474,13 @@ static void sink_ship(region * r, ship * sh, const char *name, unit * saboteur)
add_message(&u->faction->msgs, msg);
msg_release(msg);
if (dead == u->number) {
/* the poor creature, she dies */
if (remove_unit(ui, u) != 0) {
ui = &u->next;
if (remove_unit(ui, u) == 0) {
/* ui is already pointing at u->next */
continue;
}
}
}
ui = &u->next;
}
if (sink_msg)
msg_release(sink_msg);
@ -487,19 +491,21 @@ static void sink_ship(region * r, ship * sh, const char *name, unit * saboteur)
int sabotage_cmd(unit * u, struct order *ord)
{
const char *s;
int i;
param_t p;
ship *sh;
unit *u2;
char buffer[DISPLAYSIZE];
region *r = u->region;
int skdiff;
region *r;
int skdiff = INT_MAX;
assert(u);
assert(ord);
init_order(ord);
s = getstrtoken();
i = findparam(s, u->faction->locale);
p = findparam(s, u->faction->locale);
switch (i) {
switch (p) {
case P_SHIP:
sh = u->ship;
if (!sh) {
@ -507,10 +513,13 @@ int sabotage_cmd(unit * u, struct order *ord)
return 0;
}
u2 = ship_owner(sh);
skdiff =
eff_skill(u, SK_SPY, r) - crew_skill(r, u2->faction, sh, SK_PERCEPTION);
r = u->region;
if (u2->faction != u->faction) {
skdiff =
eff_skill(u, SK_SPY, r) - crew_skill(r, u2->faction, sh, SK_PERCEPTION);
}
if (try_destruction(u, u2, sh, skdiff)) {
sink_ship(r, sh, buffer, u);
sink_ship(r, sh, u);
}
break;
default:

View File

@ -5,9 +5,11 @@
#include <kernel/region.h>
#include <kernel/unit.h>
#include <kernel/faction.h>
#include <kernel/order.h>
#include <kernel/item.h>
#include <kernel/messages.h>
#include <util/attrib.h>
#include <util/language.h>
#include <util/message.h>
#include <util/crmessage.h>
#include <tests.h>
@ -16,6 +18,7 @@
#include "spy.h"
#include <assert.h>
#include <stdio.h>
#include <CuTest.h>
@ -98,12 +101,36 @@ static void test_all_spy_message(CuTest *tc) {
test_cleanup();
}
static void test_sabotage_self(CuTest *tc) {
unit *u;
region *r;
order *ord;
struct locale *lang;
test_cleanup();
lang = get_or_create_locale("de");
locale_setstring(lang, parameters[P_SHIP], "SCHIFF");
test_create_world();
init_locales();
r = test_create_region(0, 0, NULL);
assert(r);
u = test_create_unit(test_create_faction(NULL), r);
assert(u && u->faction && u->region==r);
u->ship = test_create_ship(r, test_create_shiptype("boat"));
assert(u->ship);
ord = create_order(K_SABOTAGE, lang, "SCHIFF");
assert(ord);
CuAssertIntEquals(tc, 0, sabotage_cmd(u, ord));
CuAssertPtrEquals(tc, 0, r->ships);
test_cleanup();
}
CuSuite *get_spy_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_simple_spy_message);
SUITE_ADD_TEST(suite, test_all_spy_message);
SUITE_ADD_TEST(suite, test_sabotage_self);
return suite;
}