add an assert_alloc macro for allocations that cannot fail.

This commit is contained in:
Enno Rehling 2016-08-29 09:20:08 +01:00
parent 1f3413a9b4
commit fd273848ae
9 changed files with 31 additions and 19 deletions

View File

@ -56,6 +56,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <attributes/moved.h> #include <attributes/moved.h>
/* util includes */ /* util includes */
#include <util/assert.h>
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/bsdstring.h> #include <util/bsdstring.h>
@ -67,7 +68,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/rng.h> #include <util/rng.h>
/* libc includes */ /* libc includes */
#include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
@ -218,7 +218,7 @@ static void message_faction(battle * b, faction * f, struct message *m)
assert(f); assert(f);
if (f->battles == NULL || f->battles->r != r) { if (f->battles == NULL || f->battles->r != r) {
struct bmsg *bm = (struct bmsg *)calloc(1, sizeof(struct bmsg)); struct bmsg *bm = (struct bmsg *)calloc(1, sizeof(struct bmsg));
assert(bm || !"out of memory"); assert_alloc(bm);
bm->next = f->battles; bm->next = f->battles;
f->battles = bm; f->battles = bm;
bm->r = r; bm->r = r;

View File

@ -57,6 +57,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "skill.h" #include "skill.h"
/* util includes */ /* util includes */
#include <util/assert.h>
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/bsdstring.h> #include <util/bsdstring.h>
@ -77,7 +78,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <attributes/targetregion.h> #include <attributes/targetregion.h>
/* libc includes */ /* libc includes */
#include <assert.h>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -127,7 +127,7 @@ get_followers(unit * target, region * r, const region_list * route_end,
const attrib *a = a_find(uf->attribs, &at_follow); const attrib *a = a_find(uf->attribs, &at_follow);
if (a && a->data.v == target) { if (a && a->data.v == target) {
follower *fnew = (follower *)malloc(sizeof(follower)); follower *fnew = (follower *)malloc(sizeof(follower));
assert(fnew || !"out of memory"); assert_alloc(fnew);
fnew->uf = uf; fnew->uf = uf;
fnew->ut = target; fnew->ut = target;
fnew->route_end = route_end; fnew->route_end = route_end;
@ -1545,7 +1545,7 @@ static arg_regions *var_copy_regions(const region_list * begin, int size)
assert(size > 0); assert(size > 0);
arg_regions *dst = arg_regions *dst =
(arg_regions *)malloc(sizeof(arg_regions) + sizeof(region *) * (size_t)size); (arg_regions *)malloc(sizeof(arg_regions) + sizeof(region *) * (size_t)size);
assert(dst || !"out of memory"); assert_alloc(dst);
dst->nregions = size; dst->nregions = size;
dst->regions = (region **)(dst + 1); dst->regions = (region **)(dst + 1);
for (rsrc = begin; i != size; rsrc = rsrc->next) { for (rsrc = begin; i != size; rsrc = rsrc->next) {

View File

@ -58,6 +58,7 @@
#include <races/races.h> #include <races/races.h>
/* util includes */ /* util includes */
#include <util/assert.h>
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/event.h> #include <util/event.h>
@ -80,7 +81,6 @@
#include <storage.h> #include <storage.h>
/* libc includes */ /* libc includes */
#include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
@ -1263,7 +1263,7 @@ add_ironweapon(const struct item_type *type, const struct item_type *rusty,
float chance) float chance)
{ {
iron_weapon *iweapon = malloc(sizeof(iron_weapon)); iron_weapon *iweapon = malloc(sizeof(iron_weapon));
assert(iweapon || !"out of memory"); assert_alloc(iweapon);
iweapon->type = type; iweapon->type = type;
iweapon->rusty = rusty; iweapon->rusty = rusty;
iweapon->chance = chance; iweapon->chance = chance;

View File

@ -27,10 +27,10 @@
#include <util/message.h> #include <util/message.h>
#include <util/log.h> #include <util/log.h>
#include <util/rand.h> #include <util/rand.h>
#include <util/assert.h>
#include <CuTest.h> #include <CuTest.h>
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -191,7 +191,7 @@ ship_type * test_create_shiptype(const char * name)
stype->damage = 1; stype->damage = 1;
if (!stype->construction) { if (!stype->construction) {
stype->construction = calloc(1, sizeof(construction)); stype->construction = calloc(1, sizeof(construction));
assert(stype->construction || !"out of memory"); assert_alloc(stype->construction);
stype->construction->maxsize = 5; stype->construction->maxsize = 5;
stype->construction->minskill = 1; stype->construction->minskill = 1;
stype->construction->reqsize = 1; stype->construction->reqsize = 1;
@ -260,7 +260,7 @@ spell * test_create_spell(void)
sp = create_spell("testspell", 0); sp = create_spell("testspell", 0);
sp->components = (spell_component *)calloc(4, sizeof(spell_component)); 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].amount = 1;
sp->components[0].type = get_resourcetype(R_SILVER); sp->components[0].type = get_resourcetype(R_SILVER);
sp->components[0].cost = SPC_FIX; sp->components[0].cost = SPC_FIX;

7
src/util/assert.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef UTIL_ASSERT_H
#define UTIL_ASSERT_H
#include <assert.h>
#define assert_alloc(expr) assert((expr) || !"out of memory")
#endif

View File

@ -18,6 +18,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h> #include <platform.h>
#include "assert.h"
/* libc includes */ /* libc includes */
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -34,7 +36,9 @@ char *set_string(char **s, const char *neu)
strcpy(*s, neu); strcpy(*s, neu);
} }
else { else {
*s = realloc(*s, strlen(neu) + 1); char *rs = realloc(*s, strlen(neu) + 1);
assert_alloc(rs);
*s = rs;
strcpy(*s, neu); strcpy(*s, neu);
} }
return *s; return *s;

View File

@ -16,9 +16,9 @@
#include "bsdstring.h" #include "bsdstring.h"
#include "critbit.h" #include "critbit.h"
#include "log.h" #include "log.h"
#include "assert.h"
/* libc includes */ /* libc includes */
#include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -48,6 +48,7 @@ void opstack_push(opstack ** stackp, variant data)
opstack *stack = *stackp; opstack *stack = *stackp;
if (stack == NULL) { if (stack == NULL) {
stack = (opstack *)malloc(sizeof(opstack)); stack = (opstack *)malloc(sizeof(opstack));
assert_alloc(stack);
stack->size = 2; stack->size = 2;
stack->begin = malloc(sizeof(variant) * stack->size); stack->begin = malloc(sizeof(variant) * stack->size);
stack->top = stack->begin; stack->top = stack->begin;
@ -58,10 +59,7 @@ void opstack_push(opstack ** stackp, variant data)
void *tmp; void *tmp;
stack->size += stack->size; stack->size += stack->size;
tmp = realloc(stack->begin, sizeof(variant) * stack->size); tmp = realloc(stack->begin, sizeof(variant) * stack->size);
if (!tmp) { assert_alloc(tmp);
log_error("realloc out of memory");
abort();
}
stack->begin = (variant *)tmp; stack->begin = (variant *)tmp;
stack->top = stack->begin + pos; stack->top = stack->begin + pos;
} }

View File

@ -19,6 +19,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h> #include <platform.h>
#include "umlaut.h" #include "umlaut.h"
#include "assert.h"
#include "log.h" #include "log.h"
#include "unicode.h" #include "unicode.h"
@ -183,6 +184,7 @@ void addtoken(void ** root, const char *str, variant id)
index = lcs % NODEHASHSIZE; index = lcs % NODEHASHSIZE;
#endif #endif
ref = (tref *)malloc(sizeof(tref)); ref = (tref *)malloc(sizeof(tref));
assert_alloc(ref);
ref->ucs = lcs; ref->ucs = lcs;
ref->node = node; ref->node = node;
ref->nexthash = tk->next[index]; ref->nexthash = tk->next[index];

View File

@ -14,6 +14,7 @@
/* util includes */ /* util includes */
#include "log.h" #include "log.h"
#include "assert.h"
#ifdef USE_LIBXML2 #ifdef USE_LIBXML2
#include <libxml/catalog.h> #include <libxml/catalog.h>
@ -21,7 +22,6 @@
#endif #endif
/* libc includes */ /* libc includes */
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -93,6 +93,7 @@ void xml_register_callback(xml_callback callback)
{ {
xml_reader *reader = (xml_reader *)malloc(sizeof(xml_reader)); xml_reader *reader = (xml_reader *)malloc(sizeof(xml_reader));
xml_reader **insert = &xmlReaders; xml_reader **insert = &xmlReaders;
assert_alloc(reader);
reader->callback = callback; reader->callback = callback;
reader->next = NULL; reader->next = NULL;