From fd273848ae87d8538bf5c637255b5aad01812ffc Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 29 Aug 2016 09:20:08 +0100 Subject: [PATCH] add an assert_alloc macro for allocations that cannot fail. --- src/battle.c | 4 ++-- src/move.c | 6 +++--- src/spells.c | 4 ++-- src/tests.c | 6 +++--- src/util/assert.h | 7 +++++++ src/util/strings.c | 6 +++++- src/util/translation.c | 12 +++++------- src/util/umlaut.c | 2 ++ src/util/xml.c | 3 ++- 9 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 src/util/assert.h diff --git a/src/battle.c b/src/battle.c index c061f1ca5..a35026983 100644 --- a/src/battle.c +++ b/src/battle.c @@ -56,6 +56,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* util includes */ +#include #include #include #include @@ -67,7 +68,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* libc includes */ -#include #include #include #include @@ -218,7 +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"); + assert_alloc(bm); bm->next = f->battles; f->battles = bm; bm->r = r; diff --git a/src/move.c b/src/move.c index 6d398d2f9..156c3c5d2 100644 --- a/src/move.c +++ b/src/move.c @@ -57,6 +57,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "skill.h" /* util includes */ +#include #include #include #include @@ -77,7 +78,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* libc includes */ -#include #include #include #include @@ -127,7 +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"); + assert_alloc(fnew); fnew->uf = uf; fnew->ut = target; fnew->route_end = route_end; @@ -1545,7 +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"); + assert_alloc(dst); 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 9dd7ef5bf..5329e85d3 100644 --- a/src/spells.c +++ b/src/spells.c @@ -58,6 +58,7 @@ #include /* util includes */ +#include #include #include #include @@ -80,7 +81,6 @@ #include /* libc includes */ -#include #include #include #include @@ -1263,7 +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"); + assert_alloc(iweapon); iweapon->type = type; iweapon->rusty = rusty; iweapon->chance = chance; diff --git a/src/tests.c b/src/tests.c index 6954c03d3..53ebe8a28 100644 --- a/src/tests.c +++ b/src/tests.c @@ -27,10 +27,10 @@ #include #include #include +#include #include -#include #include #include #include @@ -191,7 +191,7 @@ ship_type * test_create_shiptype(const char * name) stype->damage = 1; if (!stype->construction) { stype->construction = calloc(1, sizeof(construction)); - assert(stype->construction || !"out of memory"); + assert_alloc(stype->construction); stype->construction->maxsize = 5; stype->construction->minskill = 1; stype->construction->reqsize = 1; @@ -260,7 +260,7 @@ spell * test_create_spell(void) sp = create_spell("testspell", 0); sp->components = (spell_component *)calloc(4, sizeof(spell_component)); - assert(sp->components || !"out of memory"); + assert_alloc(sp->components); sp->components[0].amount = 1; sp->components[0].type = get_resourcetype(R_SILVER); sp->components[0].cost = SPC_FIX; diff --git a/src/util/assert.h b/src/util/assert.h new file mode 100644 index 000000000..79abce73e --- /dev/null +++ b/src/util/assert.h @@ -0,0 +1,7 @@ +#ifndef UTIL_ASSERT_H +#define UTIL_ASSERT_H + +#include + +#define assert_alloc(expr) assert((expr) || !"out of memory") +#endif diff --git a/src/util/strings.c b/src/util/strings.c index b3801d52f..5c8a7ba71 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -18,6 +18,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include +#include "assert.h" + /* libc includes */ #include #include @@ -34,7 +36,9 @@ char *set_string(char **s, const char *neu) strcpy(*s, neu); } else { - *s = realloc(*s, strlen(neu) + 1); + char *rs = realloc(*s, strlen(neu) + 1); + assert_alloc(rs); + *s = rs; strcpy(*s, neu); } return *s; diff --git a/src/util/translation.c b/src/util/translation.c index 16d3e684f..829a1fe05 100644 --- a/src/util/translation.c +++ b/src/util/translation.c @@ -1,4 +1,4 @@ -/* +/* +-------------------+ Christian Schlittchen | | Enno Rehling | Eressea PBEM host | Katja Zedel @@ -16,9 +16,9 @@ #include "bsdstring.h" #include "critbit.h" #include "log.h" +#include "assert.h" -/* libc includes */ -#include + /* libc includes */ #include #include #include @@ -48,6 +48,7 @@ void opstack_push(opstack ** stackp, variant data) opstack *stack = *stackp; if (stack == NULL) { stack = (opstack *)malloc(sizeof(opstack)); + assert_alloc(stack); stack->size = 2; stack->begin = malloc(sizeof(variant) * stack->size); stack->top = stack->begin; @@ -58,10 +59,7 @@ void opstack_push(opstack ** stackp, variant data) void *tmp; stack->size += stack->size; tmp = realloc(stack->begin, sizeof(variant) * stack->size); - if (!tmp) { - log_error("realloc out of memory"); - abort(); - } + assert_alloc(tmp); stack->begin = (variant *)tmp; stack->top = stack->begin + pos; } diff --git a/src/util/umlaut.c b/src/util/umlaut.c index abbd59372..616596a01 100644 --- a/src/util/umlaut.c +++ b/src/util/umlaut.c @@ -19,6 +19,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "umlaut.h" +#include "assert.h" #include "log.h" #include "unicode.h" @@ -183,6 +184,7 @@ void addtoken(void ** root, const char *str, variant id) index = lcs % NODEHASHSIZE; #endif ref = (tref *)malloc(sizeof(tref)); + assert_alloc(ref); ref->ucs = lcs; ref->node = node; ref->nexthash = tk->next[index]; diff --git a/src/util/xml.c b/src/util/xml.c index 80cec1092..2d49f2a7d 100644 --- a/src/util/xml.c +++ b/src/util/xml.c @@ -14,6 +14,7 @@ /* util includes */ #include "log.h" +#include "assert.h" #ifdef USE_LIBXML2 #include @@ -21,7 +22,6 @@ #endif /* libc includes */ -#include #include #include #include @@ -93,6 +93,7 @@ void xml_register_callback(xml_callback callback) { xml_reader *reader = (xml_reader *)malloc(sizeof(xml_reader)); xml_reader **insert = &xmlReaders; + assert_alloc(reader); reader->callback = callback; reader->next = NULL;