From f051b5576f0d09c1e61af2703f2e36f45e34549e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 24 May 2008 23:26:44 +0000 Subject: [PATCH] fix some crashes when attributes refer to a unit that is dead. --- src/common/gamecode/creport.c | 2 +- src/common/kernel/building.c | 2 +- src/common/kernel/curse.h | 1 - src/common/kernel/magic.c | 26 +++++++++++++++++++------- src/common/kernel/ship.c | 2 +- src/common/kernel/unit.c | 2 +- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/common/gamecode/creport.c b/src/common/gamecode/creport.c index 04000bbac..aa83b6ad3 100644 --- a/src/common/gamecode/creport.c +++ b/src/common/gamecode/creport.c @@ -678,7 +678,7 @@ cr_output_unit(FILE * F, const region * r, itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct)); } - assert(u); + assert(u && u->number); #if KARMA_MODULE if (fspecial(u->faction, FS_HIDDEN)) diff --git a/src/common/kernel/building.c b/src/common/kernel/building.c index 4f971ee2f..7278a33d9 100644 --- a/src/common/kernel/building.c +++ b/src/common/kernel/building.c @@ -374,7 +374,7 @@ register_buildings(void) void write_building_reference(const struct building * b, struct storage * store) { - store->w_id(store, b?b->no:0); + store->w_id(store, (b && b->region)?b->no:0); } diff --git a/src/common/kernel/curse.h b/src/common/kernel/curse.h index 7e571afc3..5fd40385f 100644 --- a/src/common/kernel/curse.h +++ b/src/common/kernel/curse.h @@ -292,7 +292,6 @@ extern struct curse * get_curse(struct attrib *ap, const curse_type * ctype); * oder einen NULL-pointer * */ -struct unit * get_tracingunit(struct attrib **ap, const curse_type * ct); int find_cursebyname(const char *c); const curse_type * ct_find(const char *c); void ct_register(const curse_type *); diff --git a/src/common/kernel/magic.c b/src/common/kernel/magic.c index 820a730de..31d17a6e5 100644 --- a/src/common/kernel/magic.c +++ b/src/common/kernel/magic.c @@ -2108,7 +2108,7 @@ sm_familiar(const unit * u, const region * r, skill_t sk, int value) /* skillmod int mod; unit * familiar = get_familiar(u); if (familiar==NULL) { - log_error(("[sm_familiar] %s has a familiar-skillmod, but no familiar\n", unitname(u))); + /* the familiar is dead */ return value; } mod = eff_skill(familiar, sk, r)/2; @@ -2391,16 +2391,22 @@ attrib_type at_clone = { unit * get_familiar(const unit *u) { - attrib * a = a_find(u->attribs, &at_familiar); - if (a!=NULL) return (unit*)a->data.v; - return NULL; + attrib * a = a_find(u->attribs, &at_familiar); + if (a!=NULL) { + unit * u = (unit*)a->data.v; + if (u->number>0) return u; + } + return NULL; } unit * get_familiar_mage(const unit *u) { attrib * a = a_find(u->attribs, &at_familiarmage); - if (a!=NULL) return (unit*)a->data.v; + if (a!=NULL) { + unit * u = (unit*)a->data.v; + if (u->number>0) return u; + } return NULL; } @@ -2408,7 +2414,10 @@ unit * get_clone(const unit *u) { attrib * a = a_find(u->attribs, &at_clone); - if (a!=NULL) return (unit*)a->data.v; + if (a!=NULL) { + unit * u = (unit*)a->data.v; + if (u->number>0) return u; + } return NULL; } @@ -2416,7 +2425,10 @@ unit * get_clone_mage(const unit *u) { attrib * a = a_find(u->attribs, &at_clonemage); - if (a!=NULL) return (unit*)a->data.v; + if (a!=NULL) { + unit * u = (unit*)a->data.v; + if (u->number>0) return u; + } return NULL; } diff --git a/src/common/kernel/ship.c b/src/common/kernel/ship.c index 950b0b2cf..31991d461 100644 --- a/src/common/kernel/ship.c +++ b/src/common/kernel/ship.c @@ -307,7 +307,7 @@ shipowner(const ship * sh) void write_ship_reference(const struct ship * sh, struct storage * store) { - store->w_id(store, sh?sh->no:0); + store->w_id(store, (sh && sh->region)?sh->no:0); } void diff --git a/src/common/kernel/unit.c b/src/common/kernel/unit.c index d72b262c6..5b96829b2 100644 --- a/src/common/kernel/unit.c +++ b/src/common/kernel/unit.c @@ -624,7 +624,7 @@ free_units(void) void write_unit_reference(const unit * u, struct storage * store) { - store->w_id(store, u?u->no:0); + store->w_id(store, (u && u->region)?u->no:0); } int