From 194f96c50f7203f89c18a1ff34db6235c4c3259e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 28 Aug 2016 21:03:23 +0100 Subject: [PATCH] eliminate some possible null-pointer exceptions --- src/battle.c | 12 ++++++++---- src/move.c | 16 +++++++++++----- src/spells.c | 1 + 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/battle.c b/src/battle.c index 9d6448be4..762a37e7a 100644 --- a/src/battle.c +++ b/src/battle.c @@ -218,6 +218,7 @@ static void message_faction(battle * b, faction * f, struct message *m) assert(f); if (f->battles == NULL || f->battles->r != r) { struct bmsg *bm = (struct bmsg *)calloc(1, sizeof(struct bmsg)); + assert(bm || !"out of memory"); bm->next = f->battles; f->battles = bm; bm->r = r; @@ -251,6 +252,7 @@ static void fbattlerecord(battle * b, faction * f, const char *s) static bool set_enemy(side * as, side * ds, bool attacking) { int i; + assert(as && ds); for (i = 0; i != MAXSIDES; ++i) { if (ds->enemies[i] == NULL) ds->enemies[i] = as; @@ -1942,10 +1944,12 @@ int skilldiff(troop at, troop dt, int dist) is_protected = 2; } } - if (magicwalls_ct - && curse_active(get_curse(df->building->attribs, magicwalls_ct))) { - /* Verdoppelt Burgenbonus */ - skdiff -= df->building->type->protection(df->building, du, DEFENSE_BONUS); + if (df->building->type->protection) { + if (magicwalls_ct + && curse_active(get_curse(df->building->attribs, magicwalls_ct))) { + /* Verdoppelt Burgenbonus */ + skdiff -= df->building->type->protection(df->building, du, DEFENSE_BONUS); + } } } /* Goblin-Verteidigung diff --git a/src/move.c b/src/move.c index 09f3c2b3f..6d398d2f9 100644 --- a/src/move.c +++ b/src/move.c @@ -127,6 +127,7 @@ get_followers(unit * target, region * r, const region_list * route_end, const attrib *a = a_find(uf->attribs, &at_follow); if (a && a->data.v == target) { follower *fnew = (follower *)malloc(sizeof(follower)); + assert(fnew || !"out of memory"); fnew->uf = uf; fnew->ut = target; fnew->route_end = route_end; @@ -585,18 +586,21 @@ static void leave_trail(ship * sh, region * from, region_list * route) while (a != NULL && a->type == &at_shiptrail) { td = (traveldir *)a->data.v; - if (td->no == sh->no) + if (td->no == sh->no) { + td->dir = dir; + td->age = 2; break; + } a = a->next; } - if (a == NULL || a->type != &at_shiptrail) { + if (a == NULL) { a = a_add(&(r->attribs), a_new(&at_shiptrail)); td = (traveldir *)a->data.v; td->no = sh->no; + td->dir = dir; + td->age = 2; } - td->dir = dir; - td->age = 2; } route = route->next; r = rn; @@ -769,8 +773,9 @@ static void msg_to_ship_inmates(ship *sh, unit **firstu, unit **lastu, message * *lastu = u->next; } } - if (shipfirst) + if (shipfirst) { *firstu = shipfirst; + } for (u = *firstu; u != *lastu; u = u->next) { freset(u->faction, FFL_MARK); } @@ -1540,6 +1545,7 @@ static arg_regions *var_copy_regions(const region_list * begin, int size) assert(size > 0); arg_regions *dst = (arg_regions *)malloc(sizeof(arg_regions) + sizeof(region *) * (size_t)size); + assert(dst || !"out of memory"); dst->nregions = size; dst->regions = (region **)(dst + 1); for (rsrc = begin; i != size; rsrc = rsrc->next) { diff --git a/src/spells.c b/src/spells.c index 1da440df2..9dd7ef5bf 100644 --- a/src/spells.c +++ b/src/spells.c @@ -1263,6 +1263,7 @@ add_ironweapon(const struct item_type *type, const struct item_type *rusty, float chance) { iron_weapon *iweapon = malloc(sizeof(iron_weapon)); + assert(iweapon || !"out of memory"); iweapon->type = type; iweapon->rusty = rusty; iweapon->chance = chance;