From 0a57933e30949aa86acdb8e9261cda38a81656f7 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 12 Jul 2015 14:10:12 +0200 Subject: [PATCH] minor optimizations, error checking (static analysis) --- src/battle.c | 5 +++-- src/kernel/curse.h | 36 ++++++++++++++++++------------------ src/magic.c | 16 ++++++++++++---- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/battle.c b/src/battle.c index f97c39a2a..4719da5b7 100644 --- a/src/battle.c +++ b/src/battle.c @@ -2339,8 +2339,9 @@ void do_regenerate(fighter * af) ta.index = af->fighting; while (ta.index--) { - af->person[ta.index].hp += effskill(au, SK_STAMINA); - af->person[ta.index].hp = _min(unit_max_hp(au), af->person[ta.index].hp); + struct person *p = af->person + ta.index; + p->hp += effskill(au, SK_STAMINA); + p->hp = _min(unit_max_hp(au), p->hp); } } diff --git a/src/kernel/curse.h b/src/kernel/curse.h index 86d94c935..45665f5cb 100644 --- a/src/kernel/curse.h +++ b/src/kernel/curse.h @@ -179,18 +179,6 @@ extern "C" { /* ------------------------------------------------------------- */ /* Allgemeine Zauberwirkungen */ - typedef struct curse { - struct curse *nexthash; - int no; /* 'Einheitennummer' dieses Curse */ - const struct curse_type *type; /* Zeiger auf ein curse_type-struct */ - int flags; /* WARNING: these are XORed with type->flags! */ - int duration; /* Dauer der Verzauberung. Wird jede Runde vermindert */ - double vigour; /* Stärke der Verzauberung, Widerstand gegen Antimagie */ - struct unit *magician; /* Pointer auf den Magier, der den Spruch gewirkt hat */ - double effect; - variant data; /* pointer auf spezielle curse-unterstructs */ - } curse; - #define c_flags(c) ((c)->type->flags ^ (c)->flags) /* ------------------------------------------------------------- */ @@ -200,17 +188,29 @@ extern "C" { int typ; int flags; int mergeflags; - struct message *(*curseinfo) (const void *, objtype_t, const struct curse *, - int); - void(*change_vigour) (curse *, double); - int(*read) (struct storage * store, curse * c, void *target); - int(*write) (struct storage * store, const struct curse * c, + struct message *(*curseinfo) (const void *, objtype_t, + const struct curse *, int); + void(*change_vigour) (struct curse *, double); + int(*read) (struct storage * store, struct curse *, void *target); + int(*write) (struct storage * store, const struct curse *, const void *target); int(*cansee) (const struct faction *, const void *, objtype_t, const struct curse *, int); - int(*age) (curse *); + int(*age) (struct curse *); } curse_type; + typedef struct curse { + variant data; /* pointer auf spezielle curse-unterstructs */ + struct curse *nexthash; + const curse_type *type; /* Zeiger auf ein curse_type-struct */ + struct unit *magician; /* Pointer auf den Magier, der den Spruch gewirkt hat */ + double vigour; /* Stärke der Verzauberung, Widerstand gegen Antimagie */ + double effect; + int no; /* 'Einheitennummer' dieses Curse */ + int flags; /* WARNING: these are XORed with type->flags! */ + int duration; /* Dauer der Verzauberung. Wird jede Runde vermindert */ + } curse; + extern struct attrib_type at_curse; void curse_write(const struct attrib *a, const void *owner, struct storage *store); diff --git a/src/magic.c b/src/magic.c index f610f1d77..6d713ba63 100644 --- a/src/magic.c +++ b/src/magic.c @@ -2560,7 +2560,7 @@ static castorder *cast_cmd(unit * u, order * ord) } s = gettoken(token, sizeof(token)); } - if (!s || !s[0] || strlen(s) == 0) { + if (!s || !s[0]) { /* Fehler "Es wurde kein Zauber angegeben" */ cmistake(u, ord, 172, MSG_MAGIC); return 0; @@ -2571,7 +2571,7 @@ static castorder *cast_cmd(unit * u, order * ord) /* Vertraute können auch Zauber sprechen, die sie selbst nicht * können. unit_getspell findet aber nur jene Sprüche, die * die Einheit beherrscht. */ - if (!sp && is_familiar(u)) { + if (!sp && is_familiar(u)) { caster = get_familiar_mage(u); if (caster) { familiar = u; @@ -2695,8 +2695,16 @@ static castorder *cast_cmd(unit * u, order * ord) if (!s || *s == 0) break; if (p + 1 >= size) { - size *= 2; - params = (char**)realloc(params, sizeof(char *) * size); + char ** tmp; + tmp = (char**)realloc(params, sizeof(char *) * size * 2); + if (tmp) { + size *= 2; + params = tmp; + } + else { + log_error("error allocationg %d bytes: %s", size * 2, strerror(errno)); + break; + } } params[p++] = _strdup(s); }