fix bug 2386, int overflow

This commit is contained in:
Enno Rehling 2017-11-19 02:44:13 +01:00
parent 0386aafa7b
commit 7e1f3dbdc5
2 changed files with 21 additions and 1 deletions

View File

@ -1060,7 +1060,7 @@ void clone_men(const unit * u, unit * dst, int n)
transfer_curse(u, dst, n);
}
set_number(dst, dst->number + n);
dst->hp += u->hp * dst->number / u->number;
dst->hp += (long)u->hp * dst->number / u->number;
assert(dst->hp >= dst->number);
/* TODO: Das ist schnarchlahm! und gehoert nicht hierhin */
a = a_find(dst->attribs, &at_effect);

View File

@ -525,6 +525,25 @@ static void test_unlimited_units(CuTest *tc) {
test_cleanup();
}
static void test_clone_men_bug_2386(CuTest *tc) {
unit *u1, *u2;
region *r;
faction *f;
test_setup();
r = test_create_region(0, 0, NULL);
f = test_create_faction(NULL);
u1 = test_create_unit(f, r);
scale_number(u1, 8237);
u1->hp = 39 * u1->number;
u2 = test_create_unit(f, r);
scale_number(u2, 0);
clone_men(u1, u2, 8100);
CuAssertIntEquals(tc, 8100, u2->number);
CuAssertIntEquals(tc, u2->number * 39, u2->hp);
test_cleanup();
}
static void test_clone_men(CuTest *tc) {
unit *u1, *u2;
region *r;
@ -557,6 +576,7 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_unit_name_from_race);
SUITE_ADD_TEST(suite, test_update_monster_name);
SUITE_ADD_TEST(suite, test_clone_men);
SUITE_ADD_TEST(suite, test_clone_men_bug_2386);
SUITE_ADD_TEST(suite, test_remove_unit);
SUITE_ADD_TEST(suite, test_remove_empty_units);
SUITE_ADD_TEST(suite, test_remove_units_without_faction);