abort on OOM

This commit is contained in:
Enno Rehling 2017-11-20 14:44:02 +01:00
parent e6c11e2223
commit e0bd5c2c7b
6 changed files with 29 additions and 7 deletions

View File

@ -135,7 +135,13 @@ static void dict_upgrade(attrib **alist, attrib *abegin) {
assert(!"invalid input");
}
if (i == 4) {
keys = realloc(keys, sizeof(int) * (n + i + 1));
int *k;
k = realloc(keys, sizeof(int) * (n + i + 1));
if (!k) {
free(keys);
abort();
}
keys = k;
memcpy(keys + n + 1, val, sizeof(val));
n += i;
i = 0;

View File

@ -123,8 +123,14 @@ static int a_readkeys(attrib * a, void *owner, gamedata *data) {
if (e != n) {
int sz = keys_size(n);
if (e > sz) {
int *k;
sz = keys_size(e);
keys = realloc(keys, sizeof(int)*(2 * sz + 1));
k = realloc(keys, sizeof(int)*(2 * sz + 1));
if (!k) {
free(keys);
abort();
}
keys = k;
keys[0] = e;
}
}

View File

@ -89,7 +89,8 @@ alliance *new_alliance(int id, const char *name) {
al->flags |= ALF_NON_ALLIED;
}
al->next = alliances;
return alliances = al;
alliances = al;
return al;
}
alliance *findalliance(int id)

View File

@ -340,8 +340,10 @@ void it_set_appearance(item_type *itype, const char *appearance) {
assert(itype);
assert(itype->rtype);
if (appearance) {
char plural[32];
itype->_appearance[0] = strdup(appearance);
itype->_appearance[1] = strcat(strcpy((char *)malloc(strlen((char *)appearance) + 3), (char *)appearance), "_p");
snprintf(plural, "%29s_p", appearance);
itype->_appearance[1] = strdup(plural);
} else {
itype->_appearance[0] = 0;
itype->_appearance[1] = 0;

View File

@ -119,7 +119,12 @@ int a_readstring(attrib * a, void *owner, struct gamedata *data)
do {
e = READ_STR(data->store, buf, sizeof(buf));
if (result) {
result = realloc(result, len + DISPLAYSIZE - 1);
char *tmp = realloc(result, len + DISPLAYSIZE - 1);
if (!tmp) {
free(result);
abort();
}
result = tmp;
strcpy(result + len, buf);
len += DISPLAYSIZE - 1;
}

View File

@ -61,7 +61,7 @@ arg_type *find_argtype(const char *name)
message_type *mt_new(const char *name, const char *args[])
{
int i, nparameters = 0;
message_type *mtype = (message_type *)malloc(sizeof(message_type));
message_type *mtype;
assert(name != NULL);
if (name == NULL) {
@ -72,6 +72,7 @@ message_type *mt_new(const char *name, const char *args[])
/* count the number of parameters */
while (args[nparameters]) ++nparameters;
}
mtype = (message_type *)malloc(sizeof(message_type));
mtype->key = 0;
mtype->name = strdup(name);
mtype->nparameters = nparameters;
@ -143,13 +144,14 @@ static void free_arg(const arg_type * atype, variant data)
message *msg_create(const struct message_type *mtype, variant args[])
{
int i;
message *msg = (message *)malloc(sizeof(message));
message *msg;
assert(mtype != NULL);
if (mtype == NULL) {
log_error("Trying to create message with type=0x0\n");
return NULL;
}
msg = (message *)malloc(sizeof(message));
msg->type = mtype;
msg->parameters = (variant *)(mtype->nparameters ? calloc(mtype->nparameters, sizeof(variant)) : NULL);
msg->refcount = 1;