From 7b8bc8af0f064fba0022e8d192d3d71b36f2d35f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 15 Dec 2018 20:01:51 +0100 Subject: [PATCH] more allocation checks. --- src/attributes/dict.c | 5 ++++- src/attributes/key.c | 7 +++++-- src/economy.c | 1 + src/jsonconf.c | 12 ++++++++++++ src/kernel/alliance.c | 2 ++ src/kernel/ally.c | 1 + src/kernel/attrib.c | 1 + src/kernel/build.c | 1 + src/kernel/building.c | 3 +++ src/kernel/command.c | 2 ++ src/kernel/connection.c | 1 + src/kernel/spell.c | 2 ++ src/magic.c | 14 +++++++++++++- src/modules/autoseed.c | 3 +++ src/move.c | 11 ++++++----- src/prefix.c | 6 ++---- src/renumber.c | 1 + src/reports.c | 4 ++++ src/spells/borders.c | 1 + src/spells/combatspells.c | 1 + src/steal.c | 1 + src/summary.c | 1 + 22 files changed, 68 insertions(+), 13 deletions(-) diff --git a/src/attributes/dict.c b/src/attributes/dict.c index 70e88219d..130dfffa4 100644 --- a/src/attributes/dict.c +++ b/src/attributes/dict.c @@ -86,6 +86,7 @@ static void dict_init(variant *var) { dict_data *dd; var->v = malloc(sizeof(dict_data)); + if (!var->v) abort(); dd = (dict_data *)var->v; dd->type = TNONE; } @@ -143,7 +144,9 @@ static void dict_upgrade(attrib **alist, attrib *abegin) { } } if (i > 0) { - keys = realloc(keys, sizeof(int) * (2 * (n + i) + 1)); + int *tmp = realloc(keys, sizeof(int) * (2 * (n + i) + 1)); + if (!tmp) abort(); + keys = tmp; memcpy(keys + n*2 + 1, val, sizeof(int)*i*2); if (!ak) { ak = a_add(alist, a_new(&at_keys)); diff --git a/src/attributes/key.c b/src/attributes/key.c index ac4dbb4d5..8d563024d 100644 --- a/src/attributes/key.c +++ b/src/attributes/key.c @@ -257,10 +257,12 @@ static int *keys_update(int *base, int key, int val) int sz = keys_size(n); assert(kv[0] > key); if (n + 1 > sz) { + int *tmp; ptrdiff_t diff = kv - base; sz = keys_size(n + 1); - base = realloc(base, (sz * 2 + 1) * sizeof(int)); - if (!base) abort(); + tmp = realloc(base, (sz * 2 + 1) * sizeof(int)); + if (!tmp) abort(); + base = tmp; kv = base + diff; } base[0] = n + 1; @@ -299,6 +301,7 @@ void key_set(attrib ** alist, int key, int val) if (!keys) { int sz = keys_size(1); a->data.v = keys = malloc((2 * sz + 1) * sizeof(int)); + if (!keys) abort(); keys[0] = 1; keys[1] = key; keys[2] = val; diff --git a/src/economy.c b/src/economy.c index e00a8eb90..b236569be 100644 --- a/src/economy.c +++ b/src/economy.c @@ -1058,6 +1058,7 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want) alist = alist->next; if (!alist) { alist = calloc(1, sizeof(struct allocation_list)); + if (!alist) abort(); alist->next = allocations; alist->type = rtype; allocations = alist; diff --git a/src/jsonconf.c b/src/jsonconf.c index 217ed0ebe..06ea2c786 100644 --- a/src/jsonconf.c +++ b/src/jsonconf.c @@ -80,6 +80,7 @@ static void json_requirements(cJSON *json, requirement **matp) { cJSON *child; int i; requirement *mat = calloc(1 + cJSON_GetArraySize(json), sizeof(requirement)); + if (!mat) abort(); for (i = 0, child = json->child; child; child = child->next, ++i) { mat[i].number = child->valueint; mat[i].rtype = rt_get_or_create(child->string); @@ -157,6 +158,7 @@ static void json_construction(cJSON *json, construction **consp) { return; } cons = (construction *)calloc(1, sizeof(construction)); + if (!cons) abort(); for (child = json->child; child; child = child->next) { switch (child->type) { case cJSON_Object: @@ -236,6 +238,7 @@ static void json_terrain(cJSON *json, terrain_type *ter) { if (size > 0) { int n; ter->production = (terrain_production *)calloc(size + 1, sizeof(terrain_production)); + if (!ter->production) abort(); ter->production[size].type = 0; for (n = 0, entry = child->child; entry; entry = entry->next, ++n) { ter->production[n].type = rt_get_or_create(entry->string); @@ -266,6 +269,7 @@ static void json_terrain(cJSON *json, terrain_type *ter) { int n; free(ter->herbs); ter->herbs = malloc(sizeof(const item_type *) * (size + 1)); + if (!ter->herbs) abort(); ter->herbs[size] = 0; for (n = 0, entry = child->child; entry; entry = entry->next) { ter->herbs[n++] = it_get_or_create(rt_get_or_create(entry->valuestring)); @@ -333,6 +337,7 @@ static void json_stages(cJSON *json, building_type *bt) { switch (child->type) { case cJSON_Object: stage = calloc(1, sizeof(building_stage)); + if (!stage) abort(); json_stage(child, stage); if (stage->construction->maxsize > 0) { stage->construction->maxsize -= size; @@ -376,6 +381,7 @@ static void json_building(cJSON *json, building_type *bt) { /* simple, single-stage building */ if (!bt->stages) { building_stage *stage = calloc(1, sizeof(building_stage)); + if (!stage) abort(); json_construction(child, &stage->construction); bt->stages = stage; } @@ -448,6 +454,7 @@ static void json_ship(cJSON *json, ship_type *st) { case cJSON_Array: st->coasts = (terrain_type **) malloc(sizeof(terrain_type *) * (1 + cJSON_GetArraySize(child))); + if (!st->coasts) abort(); for (i = 0, iter = child->child; iter; iter = iter->next) { if (iter->type == cJSON_String) { terrain_type *ter = get_or_create_terrain(iter->valuestring); @@ -746,6 +753,7 @@ static void json_calendar(cJSON *json) { weeks_per_month = cJSON_GetArraySize(child); free(weeknames); weeknames = malloc(sizeof(char *) * weeks_per_month); + if (!weeknames) abort(); for (i = 0, entry = child->child; entry; entry = entry->next, ++i) { if (entry->type == cJSON_String) { weeknames[i] = str_strdup(entry->valuestring); @@ -760,6 +768,7 @@ static void json_calendar(cJSON *json) { assert(i == weeks_per_month); free(weeknames2); weeknames2 = malloc(sizeof(char *) * weeks_per_month); + if (!weeknames2) abort(); for (i = 0; i != weeks_per_month; ++i) { weeknames2[i] = malloc(strlen(weeknames[i]) + 3); sprintf(weeknames2[i], "%s_d", weeknames[i]); @@ -777,7 +786,9 @@ static void json_calendar(cJSON *json) { free(storms); months_per_year = cJSON_GetArraySize(child); storms = malloc(sizeof(int) * months_per_year); + if (!storms) abort(); month_season = malloc(sizeof(int) * months_per_year); + if (!month_season) abort(); for (i = 0, jmonth = child->child; jmonth; jmonth = jmonth->next, ++i) { if (jmonth->type == cJSON_Object) { storms[i] = cJSON_GetObjectItem(jmonth, "storm")->valueint; @@ -991,6 +1002,7 @@ static int include_json(const char *uri) { size_t sz; data = malloc(pos + 1); + if (!data) abort(); sz = fread(data, 1, (size_t)pos, F); data[sz] = 0; config = cJSON_Parse(data); diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index 1996c5aad..34775b4bc 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -83,6 +83,7 @@ alliance *new_alliance(int id, const char *name) { assert(id>0); al = calloc(1, sizeof(alliance)); + if (!al) abort(); al->id = id; if (name) { al->name = str_strdup(name); @@ -129,6 +130,7 @@ static void create_transaction(int type, unit * u, order * ord) { alliance_transaction *tr = (alliance_transaction *)calloc(1, sizeof(alliance_transaction)); + if (!tr) abort(); tr->ord = ord; tr->u = u; tr->next = transactions[type]; diff --git a/src/kernel/ally.c b/src/kernel/ally.c index f4ab35c8e..1999b34b3 100644 --- a/src/kernel/ally.c +++ b/src/kernel/ally.c @@ -150,6 +150,7 @@ allies *allies_clone(const allies *al) { for (; al; al = al->next) { allies *al_new = calloc(1, sizeof(allies)); + if (!al_new) abort(); memcpy(al_new, al, sizeof(allies)); *al_end = al_new; al_end = &al_new->next; diff --git a/src/kernel/attrib.c b/src/kernel/attrib.c index 997a04151..f6a857d90 100644 --- a/src/kernel/attrib.c +++ b/src/kernel/attrib.c @@ -377,6 +377,7 @@ attrib *a_new(const attrib_type * at) { attrib *a = (attrib *)calloc(1, sizeof(attrib)); assert(at != NULL); + if (!a) abort(); a->type = at; if (at->initialize) at->initialize(&a->data); diff --git a/src/kernel/build.c b/src/kernel/build.c index c78cf5212..2eb236c1e 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -642,6 +642,7 @@ message *msg_materials_required(unit * u, order * ord, multi = 1; for (c = 0; ctype && ctype->materials[c].number; ++c) { resource *res = malloc(sizeof(resource)); + if (!res) abort(); res->number = multi * ctype->materials[c].number / ctype->reqsize; res->type = ctype->materials[c].rtype; res->next = reslist; diff --git a/src/kernel/building.c b/src/kernel/building.c index b741c37af..09a515fc3 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -136,6 +136,7 @@ building_type *bt_get_or_create(const char *name) building_type *btype = bt_find_i(name); if (btype == NULL) { btype = (building_type *)calloc(1, sizeof(building_type)); + if (!btype) abort(); btype->_name = str_strdup(name); btype->flags = BTF_DEFAULT; btype->auraregen = 1.0; @@ -286,6 +287,7 @@ static local_names *get_bnames(const struct locale *lang) int qi; bn = (local_names *)calloc(1, sizeof(local_names)); + if (!bn) abort(); bn->next = bnames; bn->lang = lang; @@ -378,6 +380,7 @@ int read_building_reference(gamedata * data, building **bp) building *building_create(int id) { building *b = (building *)calloc(1, sizeof(building)); + if (!b) abort(); b->no = id; bhash(b); return b; diff --git a/src/kernel/command.c b/src/kernel/command.c index 8b2a7e5e8..71abc4e4e 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -64,6 +64,7 @@ syntaxtree *stree_create(void) const struct locale *lang = locales; while (lang) { syntaxtree *stree = (syntaxtree *)malloc(sizeof(syntaxtree)); + if (!stree) abort(); stree->lang = lang; stree->next = sroot; stree->root = 0; @@ -79,6 +80,7 @@ void stree_add(struct syntaxtree *stree, const char *str, parser fun) { variant var; assert(str); + if (!cmd) abort(); cmd->fun = fun; var.v = cmd; cmd->next = stree->cmds; diff --git a/src/kernel/connection.c b/src/kernel/connection.c index 995c276bb..61c96d9d4 100644 --- a/src/kernel/connection.c +++ b/src/kernel/connection.c @@ -134,6 +134,7 @@ connection *new_border(border_type * type, region * from, region * to) bp = &(*bp)->next; } *bp = b = calloc(1, sizeof(connection)); + if (!b) abort(); b->type = type; b->from = from; b->to = to; diff --git a/src/kernel/spell.c b/src/kernel/spell.c index 41e56babd..8cc56d863 100644 --- a/src/kernel/spell.c +++ b/src/kernel/spell.c @@ -124,6 +124,7 @@ spell * create_spell(const char * name) return 0; } sp = (spell *)calloc(1, sizeof(spell)); + if (!sp) abort(); len = cb_new_kv(name, len, &sp, sizeof(sp), buffer); if (cb_insert(&cb_spells, buffer, len) == CB_SUCCESS) { sp->sname = str_strdup(name); @@ -177,6 +178,7 @@ spell *find_spell(const char *name) struct spellref *spellref_create(spell *sp, const char *name) { spellref *spref = malloc(sizeof(spellref)); + if (!spref) abort(); if (sp) { spref->sp = sp; diff --git a/src/magic.c b/src/magic.c index 258d04bbb..e9e9cdd18 100644 --- a/src/magic.c +++ b/src/magic.c @@ -437,7 +437,7 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells for (qi = 0, ql = book->spells; ql; selist_advance(&ql, &qi, 1)) { spellbook_entry * sbe = (spellbook_entry *)selist_get(ql, qi); - if (sbe->level <= level) { + if (sbe && sbe->level <= level) { commonspells[numspells++] = sbe; } } @@ -1753,11 +1753,13 @@ verify_targets(castorder * co, int *invalid, int *resist, int *success) if ((sp->sptyp & REGIONSPELL)) { /* Zielobjekt Region anlegen */ spllprm *spobj = (spllprm *)malloc(sizeof(spllprm)); + if (!spobj) abort(); spobj->flag = 0; spobj->typ = SPP_REGION; spobj->data.r = target_r; sa = calloc(1, sizeof(spellparameter)); + if (!sa) abort(); sa->length = 1; sa->param = calloc(sa->length, sizeof(spllprm *)); sa->param[0] = spobj; @@ -1815,6 +1817,7 @@ static int addparam_string(const char *const param[], spllprm ** spobjp) spllprm *spobj = *spobjp = malloc(sizeof(spllprm)); assert(param[0]); + if (!spobj) abort(); spobj->flag = 0; spobj->typ = SPP_STRING; spobj->data.xs = str_strdup(param[0]); @@ -1826,6 +1829,7 @@ static int addparam_int(const char *const param[], spllprm ** spobjp) spllprm *spobj = *spobjp = malloc(sizeof(spllprm)); assert(param[0]); + if (!spobj) abort(); spobj->flag = 0; spobj->typ = SPP_INT; spobj->data.i = atoi((char *)param[0]); @@ -1837,6 +1841,7 @@ static int addparam_ship(const char *const param[], spllprm ** spobjp) spllprm *spobj = *spobjp = malloc(sizeof(spllprm)); int id = atoi36((const char *)param[0]); + if (!spobj) abort(); spobj->flag = 0; spobj->typ = SPP_SHIP; spobj->data.i = id; @@ -1848,6 +1853,7 @@ static int addparam_building(const char *const param[], spllprm ** spobjp) spllprm *spobj = *spobjp = malloc(sizeof(spllprm)); int id = atoi36((const char *)param[0]); + if (!spobj) abort(); spobj->flag = 0; spobj->typ = SPP_BUILDING; spobj->data.i = id; @@ -1877,6 +1883,7 @@ addparam_region(const char *const param[], spllprm ** spobjp, const unit * u, if (rt != NULL) { spllprm *spobj = *spobjp = (spllprm *)malloc(sizeof(spllprm)); + if (!spobj) abort(); spobj->flag = 0; spobj->typ = SPP_REGION; spobj->data.r = rt; @@ -1910,6 +1917,7 @@ addparam_unit(const char *const param[], spllprm ** spobjp, const unit * u, } spobj = *spobjp = malloc(sizeof(spllprm)); + if (!spobj) abort(); spobj->flag = 0; spobj->typ = otype; spobj->data.i = atoi36((const char *)param[i]); @@ -1949,12 +1957,14 @@ static spellparameter *add_spellparameter(region * target_r, unit * u, } par = malloc(sizeof(spellparameter)); + if (!par) abort(); par->length = size; if (!size) { par->param = NULL; return par; } par->param = malloc(size * sizeof(spllprm *)); + if (!par->param) abort(); while (!err && *c && i < size && param[i] != NULL) { spllprm *spobj = NULL; @@ -2008,6 +2018,7 @@ static spellparameter *add_spellparameter(region * target_r, unit * u, switch (findparam_ex(param[i++], u->faction->locale)) { case P_REGION: spobj = (spllprm *)malloc(sizeof(spllprm)); + if (!spobj) abort(); spobj->flag = 0; spobj->typ = SPP_REGION; spobj->data.r = target_r; @@ -2084,6 +2095,7 @@ castorder *create_castorder(castorder * co, unit *caster, unit * familiar, const int lev, double force, int range, struct order * ord, spellparameter * p) { if (!co) co = (castorder*)calloc(1, sizeof(castorder)); + if (!co) abort(); co->magician.u = caster; co->_familiar = familiar; diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index c16bb7d79..65d4b1dfc 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -676,6 +676,7 @@ int autoseed(newfaction ** players, int nsize, int max_agediff) region_list *regionqueue_push(region_list ** rlist, region * r) { region_list *rnew = malloc(sizeof(region_list)); + if (!rnew) abort(); rnew->data = r; rnew->next = 0; while (*rlist) { @@ -722,7 +723,9 @@ const terrain_type *random_terrain_e3(direction_t dir) int n = 0; terrainarr = malloc(GEOMAX * sizeof(const terrain_type *)); + if (!terrainarr) abort(); distribution = malloc(GEOMAX * sizeof(int)); + if (!distribution) abort(); for (n = 0; n != GEOMAX; ++n) { terrainarr[n] = newterrain(geography_e3[n].type); distribution[n] = geography_e3[n].distribution; diff --git a/src/move.c b/src/move.c index 683908065..d3a7f9ac6 100644 --- a/src/move.c +++ b/src/move.c @@ -779,9 +779,9 @@ static void msg_to_ship_inmates(ship *sh, unit **firstu, unit **lastu, message * } if (shipfirst) { *firstu = shipfirst; - } - for (u = *firstu; u != *lastu; u = u->next) { - freset(u->faction, FFL_MARK); + for (u = *firstu; u != *lastu; u = u->next) { + freset(u->faction, FFL_MARK); + } } msg_release(msg); } @@ -1724,8 +1724,9 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting) int lighthouse_div = config_get_int("rules.storm.lighthouse.divisor", 0); const char *token = getstrtoken(); - if (routep) + if (routep) { *routep = NULL; + } error = movewhere(u, token, starting_point, &next_point); if (error) { @@ -1966,7 +1967,7 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting) if (fval(u, UFL_FOLLOWING)) caught_target(current_point, u); - move_ship(sh, starting_point, current_point, *routep); + move_ship(sh, starting_point, current_point, routep ? *routep : NULL); /* Hafengebühren ? */ diff --git a/src/prefix.c b/src/prefix.c index 84c026625..d6589c849 100644 --- a/src/prefix.c +++ b/src/prefix.c @@ -20,14 +20,12 @@ int add_raceprefix(const char *prefix) next = 0; size = 4; race_prefixes = malloc(size * sizeof(char *)); + if (!race_prefixes) abort(); } if (next + 1 == size) { char **tmp; tmp = realloc(race_prefixes, 2 * size * sizeof(char *)); - if (!tmp) { - log_fatal("allocation failure"); - return 1; - } + if (!tmp) abort(); race_prefixes = tmp; size *= 2; } diff --git a/src/renumber.c b/src/renumber.c index bfb43309a..820828339 100644 --- a/src/renumber.c +++ b/src/renumber.c @@ -57,6 +57,7 @@ void renumber_factions(void) } else { struct renum *r = calloc(1, sizeof(struct renum)); + if (!r) abort(); r->next = *rn; r->attrib = a; r->faction = f; diff --git a/src/reports.c b/src/reports.c index addc78dd7..e120c1c5a 100644 --- a/src/reports.c +++ b/src/reports.c @@ -975,6 +975,7 @@ void lparagraph(struct strlist **SP, char *s, unsigned int indent, char mark) * Vgl. spunit (). */ char *buflocal = calloc(strlen(s) + indent + 1, sizeof(char)); + if (!buflocal) abort(); if (indent) { memset(buflocal, ' ', indent); @@ -1174,6 +1175,7 @@ static report_type *report_types; void register_reporttype(const char *extension, report_fun write, int flag) { report_type *type = (report_type *)malloc(sizeof(report_type)); + if (!type) abort(); type->extension = extension; type->write = write; type->flag = flag; @@ -1738,6 +1740,7 @@ static variant var_copy_items(variant x) for (isrc = (item *)x.v; isrc != NULL; isrc = isrc->next) { resource *res = malloc(sizeof(resource)); + if (!res) abort(); res->number = isrc->number; res->type = isrc->type->rtype; *rptr = res; @@ -1755,6 +1758,7 @@ static variant var_copy_resources(variant x) for (rsrc = (resource *)x.v; rsrc != NULL; rsrc = rsrc->next) { resource *res = malloc(sizeof(resource)); + if (!res) abort(); res->number = rsrc->number; res->type = rsrc->type; *rptr = res; diff --git a/src/spells/borders.c b/src/spells/borders.c index c0e51a9b1..067ff6114 100644 --- a/src/spells/borders.c +++ b/src/spells/borders.c @@ -80,6 +80,7 @@ const curse_type ct_firewall = { static void wall_init(connection * b) { wall_data *fd = (wall_data *)calloc(1, sizeof(wall_data)); + if (!fd) abort(); fd->countdown = -1; /* infinite */ b->data.v = fd; } diff --git a/src/spells/combatspells.c b/src/spells/combatspells.c index 296d88563..fd87a2f17 100644 --- a/src/spells/combatspells.c +++ b/src/spells/combatspells.c @@ -1220,6 +1220,7 @@ static void do_meffect(fighter * af, int typ, int effect, int duration) { battle *b = af->side->battle; meffect *me = (meffect *)malloc(sizeof(struct meffect)); + if (!me) abort(); selist_push(&b->meffects, me); me->magician = af; me->typ = typ; diff --git a/src/steal.c b/src/steal.c index 8a390c8fb..926d1ca8f 100644 --- a/src/steal.c +++ b/src/steal.c @@ -230,6 +230,7 @@ void steal_cmd(unit * u, struct order *ord, econ_request ** stealorders) * guter dieb sein, schliesslich macht man immer noch sehr viel laerm */ o = (econ_request *)calloc(1, sizeof(econ_request)); + if (!o) abort(); o->unit = u; o->qty = 1; /* Betrag steht in u->wants */ o->type.steal.no = u2->no; diff --git a/src/summary.c b/src/summary.c index 67e956401..06dabba90 100644 --- a/src/summary.c +++ b/src/summary.c @@ -85,6 +85,7 @@ int update_nmrs(void) int i; if (nmrs == NULL) { nmrs = malloc(sizeof(int) * (timeout + 1)); + if (!nmrs) abort(); } for (i = 0; i <= timeout; ++i) { nmrs[i] = 0;