From 033070663abbd639212a1f2d8a0046c4ebf8b278 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 15 Dec 2018 19:38:40 +0100 Subject: [PATCH] code analysis scan. add checks for malloc results. reduce stack size. --- src/chaos.c | 6 +++--- src/donations.c | 3 ++- src/economy.c | 18 +++++++++++++----- src/exparse.c | 35 +++++++++++++++++++++++------------ src/items.c | 2 +- src/jsonconf.c | 10 +++++----- src/kernel/building.c | 2 +- src/kernel/event.c | 6 ++++-- src/kernel/faction.c | 5 ++++- src/kernel/group.c | 1 + src/kernel/item.c | 9 +++++---- src/kernel/messages.c | 2 ++ src/kernel/order.c | 2 ++ src/kernel/pathfinder.c | 4 +++- src/kernel/plane.c | 2 +- src/kernel/pool.c | 6 ++++-- src/kernel/race.c | 10 ++++++++-- src/kernel/region.c | 17 ++++++++++++----- src/kernel/resources.c | 4 +++- src/kernel/save.c | 5 +++++ src/kernel/ship.c | 7 +++++-- src/kernel/skills.c | 2 +- src/kernel/spellbook.c | 3 +++ src/kernel/terrain.c | 2 +- src/laws.c | 2 +- src/magic.c | 4 ++-- src/renumber.c | 2 +- src/study.c | 2 +- src/summary.c | 2 +- src/triggers/changefaction.c | 2 +- src/triggers/changerace.c | 2 +- src/triggers/createcurse.c | 2 +- src/triggers/createunit.c | 2 +- src/triggers/gate.c | 2 +- src/triggers/giveitem.c | 2 +- src/triggers/timeout.c | 2 +- 36 files changed, 125 insertions(+), 64 deletions(-) diff --git a/src/chaos.c b/src/chaos.c index 925545369..7a2dd7c89 100644 --- a/src/chaos.c +++ b/src/chaos.c @@ -53,12 +53,12 @@ static const terrain_type *chaosterrain(void) } } if (numtypes > 0) { + int n = 0; types = malloc(sizeof(terrain_type *) * numtypes); if (!types) abort(); - numtypes = 0; - for (terrain = terrains(); terrain != NULL; terrain = terrain->next) { + for (terrain = terrains(); n != numtypes && terrain != NULL; terrain = terrain->next) { if ((terrain->flags & LAND_REGION) && terrain->herbs) { - types[numtypes++] = terrain; + types[n++] = terrain; } } } diff --git a/src/donations.c b/src/donations.c index b923df73b..eee9e6fda 100644 --- a/src/donations.c +++ b/src/donations.c @@ -48,7 +48,8 @@ void add_donation(faction * f1, faction * f2, int amount, region * r) tf->amount += amount; } else { - tf = malloc(sizeof(transfer)); + tf = (transfer *)malloc(sizeof(transfer)); + if (!tf) abort(); memcpy(tf, &tr, sizeof(transfer)); } selist_set_insert(&transfers, tf, cmp_transfer); diff --git a/src/economy.c b/src/economy.c index 7a13b4c34..8a638c067 100644 --- a/src/economy.c +++ b/src/economy.c @@ -166,7 +166,8 @@ int expand_production(region * r, econ_request * requests, econ_request ***resul if (norders > 0) { int i = 0; econ_request **split; - split = calloc(norders, sizeof(econ_request *)); + split = (econ_request **)calloc(norders, sizeof(econ_request *)); + if (!split) abort(); for (o = requests; o; o = o->next) { if (o->qty > 0) { unsigned int j; @@ -230,7 +231,8 @@ static recruitment *select_recruitment(econ_request ** rop, while (rec && rec->f != u->faction) rec = rec->next; if (rec == NULL) { - rec = malloc(sizeof(recruitment)); + rec = (recruitment *)malloc(sizeof(recruitment)); + if (!rec) abort(); rec->f = u->faction; rec->total = 0; rec->assigned = 0; @@ -563,6 +565,7 @@ static void recruit(unit * u, struct order *ord, econ_request ** recruitorders) u_setrace(u, rc); u->wants = n; o = (econ_request *)calloc(1, sizeof(econ_request)); + if (!o) abort(); o->qty = n; o->unit = u; o->type.recruit.ord = ord; @@ -940,7 +943,7 @@ typedef struct allocation { unsigned int flags; unit *unit; } allocation; -#define new_allocation() calloc(sizeof(allocation), 1) +#define new_allocation() (allocation *)calloc(1, sizeof(allocation)) #define free_allocation(a) free(a) typedef struct allocation_list { @@ -1054,12 +1057,13 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want) while (alist && alist->type != rtype) alist = alist->next; if (!alist) { - alist = calloc(sizeof(struct allocation_list), 1); + alist = calloc(1, sizeof(struct allocation_list)); alist->next = allocations; alist->type = rtype; allocations = alist; } al = new_allocation(); + if (!al) abort(); al->want = amount; al->save = save_mod; al->next = alist->data; @@ -1665,6 +1669,7 @@ static void buy(unit * u, econ_request ** buyorders, struct order *ord) return; } o = (econ_request *)calloc(1, sizeof(econ_request)); + if (!o) abort(); o->type.trade.ltype = ltype; /* sollte immer gleich sein */ o->unit = u; @@ -2013,6 +2018,7 @@ static bool sell(unit * u, econ_request ** sellorders, struct order *ord) /* die Menge der verkauften G�ter merken */ a->data.i += n; o = (econ_request *)calloc(1, sizeof(econ_request)); + if (!o) abort(); o->unit = u; o->qty = n; o->type.trade.ltype = ltype; @@ -2628,6 +2634,7 @@ void tax_cmd(unit * u, struct order *ord, econ_request ** taxorders) * einheiten aufgeteilt. */ o = (econ_request *)calloc(1, sizeof(econ_request)); + if (!o) abort(); o->qty = u->wants / TAXFRACTION; o->unit = u; addlist(taxorders, o); @@ -2693,6 +2700,7 @@ void loot_cmd(unit * u, struct order *ord, econ_request ** lootorders) } o = (econ_request *)calloc(1, sizeof(econ_request)); + if (!o) abort(); o->qty = u->wants / TAXFRACTION; o->unit = u; addlist(lootorders, o); @@ -2700,7 +2708,7 @@ void loot_cmd(unit * u, struct order *ord, econ_request ** lootorders) return; } -#define MAX_WORKERS 2048 +#define MAX_WORKERS 512 void auto_work(region * r) { econ_request workers[MAX_WORKERS]; diff --git a/src/exparse.c b/src/exparse.c index 602bdf4e9..0814f7cdc 100644 --- a/src/exparse.c +++ b/src/exparse.c @@ -753,8 +753,9 @@ static void handle_modifier(parseinfo *pi, const XML_Char *el, const XML_Char ** } static construction *parse_construction(parseinfo *pi, const XML_Char *el, const XML_Char **attr) { - construction *con = calloc(sizeof(construction), 1); int i; + construction *con = (construction *)calloc(1, sizeof(construction)); + if (!con) abort(); con->maxsize = -1; con->minskill = -1; con->reqsize = 1; @@ -1273,7 +1274,8 @@ static void start_buildings(parseinfo *pi, const XML_Char *el, const XML_Char ** } else if (xml_strequal(el, "construction")) { assert(stage == NULL); - stage = calloc(1, sizeof(building_stage)); + stage = (building_stage *)calloc(1, sizeof(building_stage)); + if (!stage) abort(); stage->construction = parse_construction(pi, el, attr); } else if (xml_strequal(el, "maintenance")) { @@ -1366,7 +1368,8 @@ static void end_spells(parseinfo *pi, const XML_Char *el) { else if (xml_strequal(el, "spell")) { spell *sp = (spell *)pi->object; if (ncomponents > 0) { - sp->components = calloc(sizeof(spell_component), ncomponents + 1); + sp->components = (spell_component *)calloc(ncomponents + 1, sizeof(spell_component)); + if (!sp->components) abort(); memcpy(sp->components, components, sizeof(spell_component) * ncomponents); ncomponents = 0; } @@ -1384,7 +1387,8 @@ static void end_weapon(parseinfo *pi, const XML_Char *el) { else if (xml_strequal(el, "modifier")) { if (nwmods > 0) { weapon_type *wtype = rtype->wtype; - wtype->modifiers = calloc(sizeof(weapon_mod), nwmods + 1); + wtype->modifiers = (weapon_mod *)calloc(nwmods + 1, sizeof(weapon_mod)); + if (!wtype->modifiers) abort(); memcpy(wtype->modifiers, wmods, sizeof(weapon_mod) * nwmods); nwmods = 0; } @@ -1395,7 +1399,8 @@ static void end_resources(parseinfo *pi, const XML_Char *el) { resource_type *rtype = (resource_type *)pi->object; if (xml_strequal(el, "resource")) { if (nrmods > 0) { - rtype->modifiers = calloc(sizeof(resource_mod), nrmods + 1); + rtype->modifiers = (resource_mod *)calloc(nrmods + 1, sizeof(resource_mod)); + if (!rtype->modifiers) abort(); memcpy(rtype->modifiers, rmods, sizeof(resource_mod) * nrmods); nrmods = 0; } @@ -1403,7 +1408,8 @@ static void end_resources(parseinfo *pi, const XML_Char *el) { else if (xml_strequal(el, "construction")) { if (nreqs > 0) { construction *con = rtype->itype->construction; - con->materials = calloc(sizeof(requirement), nreqs + 1); + con->materials = (requirement *)calloc(nreqs + 1, sizeof(requirement)); + if (!con->materials) abort(); memcpy(con->materials, reqs, sizeof(requirement) * nreqs); nreqs = 0; } @@ -1440,15 +1446,17 @@ static void end_ships(parseinfo *pi, const XML_Char *el) { assert(stype->construction); if (nreqs > 0) { construction *con = stype->construction; - con->materials = calloc(sizeof(requirement), nreqs + 1); + con->materials = (requirement *) calloc(nreqs + 1, sizeof(requirement)); + if (!con->materials) abort(); memcpy(con->materials, reqs, sizeof(requirement) * nreqs); nreqs = 0; } } else if (xml_strequal(el, "ship")) { if (ncoasts > 0) { - stype->coasts = calloc(sizeof(const terrain_type *), ncoasts + 1); - memcpy(stype->coasts, coasts, sizeof(const terrain_type *) * ncoasts); + stype->coasts = (terrain_type **) calloc(ncoasts + 1, sizeof(terrain_type *)); + if (!stype->coasts) abort(); + memcpy(stype->coasts, coasts, sizeof(terrain_type *) * ncoasts); ncoasts = 0; } pi->object = NULL; @@ -1468,7 +1476,8 @@ static void end_buildings(parseinfo *pi, const XML_Char *el) { if (stage) { if (nreqs > 0) { construction *con = stage->construction; - con->materials = calloc(sizeof(requirement), nreqs + 1); + con->materials = (requirement *)calloc(nreqs + 1, sizeof(requirement)); + if (!con->materials) abort(); memcpy(con->materials, reqs, sizeof(requirement) * nreqs); nreqs = 0; } @@ -1485,12 +1494,14 @@ static void end_buildings(parseinfo *pi, const XML_Char *el) { else if (xml_strequal(el, "building")) { stage_ptr = NULL; if (nupkeep > 0) { - btype->maintenance = calloc(sizeof(maintenance), nupkeep + 1); + btype->maintenance = (maintenance *)calloc(nupkeep + 1, sizeof(maintenance)); + if (!btype->maintenance) abort(); memcpy(btype->maintenance, upkeep, sizeof(maintenance) * nupkeep); nupkeep = 0; } if (nrmods > 0) { - btype->modifiers = calloc(sizeof(resource_mod), nrmods + 1); + btype->modifiers = calloc(nrmods + 1, sizeof(resource_mod)); + if (!btype->modifiers) abort(); memcpy(btype->modifiers, rmods, sizeof(resource_mod) * nrmods); nrmods = 0; } diff --git a/src/items.c b/src/items.c index 94865aba3..9a91a6037 100644 --- a/src/items.c +++ b/src/items.c @@ -142,7 +142,7 @@ struct order *ord) /* H�lt Spr�che bis zu einem summierten Gesamtlevel von power aus. * Jeder Zauber reduziert die 'Lebenskraft' (vigour) der Antimagiezone * um seine Stufe */ - force = effect * 20; /* Stufe 5 =~ 100 */ + force = effect * 20.0; /* Stufe 5 =~ 100 */ /* Regionszauber aufl�sen */ while (*ap && force > 0) { diff --git a/src/jsonconf.c b/src/jsonconf.c index 48be28b16..217ed0ebe 100644 --- a/src/jsonconf.c +++ b/src/jsonconf.c @@ -79,7 +79,7 @@ static int json_flags(cJSON *json, const char *flags[]) { static void json_requirements(cJSON *json, requirement **matp) { cJSON *child; int i; - requirement *mat = calloc(sizeof(requirement), 1 + cJSON_GetArraySize(json)); + requirement *mat = calloc(1 + cJSON_GetArraySize(json), sizeof(requirement)); 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); @@ -134,7 +134,7 @@ static void json_maintenance(cJSON *json, maintenance **mtp) { log_error("maintenance is not a json object or array (%d)", json->type); return; } - *mtp = mt = (struct maintenance *) calloc(sizeof(struct maintenance), size + 1); + *mtp = mt = (struct maintenance *) calloc(size + 1, sizeof(struct maintenance)); if (json->type == cJSON_Array) { int i; for (i = 0, child = json->child; child; child = child->next, ++i) { @@ -156,7 +156,7 @@ static void json_construction(cJSON *json, construction **consp) { log_error("construction %s is not a json object: %d", json->string, json->type); return; } - cons = (construction *)calloc(sizeof(construction), 1); + cons = (construction *)calloc(1, sizeof(construction)); for (child = json->child; child; child = child->next) { switch (child->type) { case cJSON_Object: @@ -332,7 +332,7 @@ static void json_stages(cJSON *json, building_type *bt) { for (child = json->child; child; child = child->next) { switch (child->type) { case cJSON_Object: - stage = calloc(sizeof(building_stage), 1); + stage = calloc(1, sizeof(building_stage)); json_stage(child, stage); if (stage->construction->maxsize > 0) { stage->construction->maxsize -= size; @@ -375,7 +375,7 @@ static void json_building(cJSON *json, building_type *bt) { if (strcmp(child->string, "construction") == 0) { /* simple, single-stage building */ if (!bt->stages) { - building_stage *stage = calloc(sizeof(building_stage), 1); + building_stage *stage = calloc(1, sizeof(building_stage)); json_construction(child, &stage->construction); bt->stages = stage; } diff --git a/src/kernel/building.c b/src/kernel/building.c index 456ad1704..b741c37af 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -285,7 +285,7 @@ static local_names *get_bnames(const struct locale *lang) selist *ql = buildingtypes; int qi; - bn = (local_names *)calloc(sizeof(local_names), 1); + bn = (local_names *)calloc(1, sizeof(local_names)); bn->next = bnames; bn->lang = lang; diff --git a/src/kernel/event.c b/src/kernel/event.c index f1ff825f5..9e04abb6a 100644 --- a/src/kernel/event.c +++ b/src/kernel/event.c @@ -79,7 +79,8 @@ int read_triggers(struct gamedata *data, trigger ** tp) trigger *t_new(trigger_type * ttype) { - trigger *t = calloc(sizeof(trigger), 1); + trigger *t = calloc(1, sizeof(trigger)); + if (!t) abort(); t->type = ttype; if (ttype->initialize) ttype->initialize(t); @@ -129,7 +130,7 @@ typedef struct handler_info { static void init_handler(variant *var) { - var->v = calloc(sizeof(handler_info), 1); + var->v = calloc(1, sizeof(handler_info)); } static void free_handler(variant *var) @@ -204,6 +205,7 @@ void add_trigger(struct attrib **ap, const char *eventname, struct trigger *t) td = (handler_info *)a->data.v; td->event = str_strdup(eventname); } + assert(td); tp = &td->triggers; while (*tp) tp = &(*tp)->next; diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 61e4a591e..dbf11359e 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -214,9 +214,10 @@ void faction_genpassword(faction *f) { faction *addfaction(const char *email, const char *password, const struct race * frace, const struct locale * loc) { - faction *f = calloc(sizeof(faction), 1); + faction *f = calloc(1, sizeof(faction)); char buf[128]; + if (!f) abort(); if (check_email(email) == 0) { faction_setemail(f, email); } else { @@ -697,6 +698,7 @@ void faction_getorigin(const faction * f, int id, int *x, int *y) static origin *new_origin(int id, int x, int y) { origin *ur = (origin *)calloc(1, sizeof(origin)); + if (!ur) abort(); ur->id = id; ur->x = x; ur->y = y; @@ -838,6 +840,7 @@ void free_factions(void) { faction *faction_create(int no) { faction *f = (faction *)calloc(1, sizeof(faction)); + if (!f) abort(); f->no = no; fhash(f); return f; diff --git a/src/kernel/group.c b/src/kernel/group.c index 210f6e0f2..b01e10a9a 100755 --- a/src/kernel/group.c +++ b/src/kernel/group.c @@ -55,6 +55,7 @@ group *new_group(faction * f, const char *name, int gid) int index = gid % GMAXHASH; group *g = calloc(1, sizeof(group)); + if (!g) abort(); while (*gp) gp = &(*gp)->next; *gp = g; diff --git a/src/kernel/item.c b/src/kernel/item.c index a58df472c..52ff72a84 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -244,7 +244,7 @@ item_type *it_get_or_create(resource_type *rtype) { assert(rtype); if (!rtype->itype) { item_type * itype; - itype = (item_type *)calloc(sizeof(item_type), 1); + itype = (item_type *)calloc(1, sizeof(item_type)); if (!itype) abort(); itype->rtype = rtype; rtype->uchange = res_changeitem; @@ -267,7 +267,7 @@ luxury_type *new_luxurytype(item_type * itype, int price) assert(resource2luxury(itype->rtype) == NULL); - ltype = calloc(sizeof(luxury_type), 1); + ltype = calloc(1, sizeof(luxury_type)); if (!ltype) abort(); ltype->itype = itype; ltype->price = price; @@ -284,7 +284,7 @@ weapon_type *new_weapontype(item_type * itype, assert(itype && (!itype->rtype || !resource2weapon(itype->rtype))); - wtype = calloc(sizeof(weapon_type), 1); + wtype = calloc(1, sizeof(weapon_type)); if (!wtype) abort(); if (damage) { wtype->damage[0] = str_strdup(damage[0]); @@ -297,6 +297,7 @@ weapon_type *new_weapontype(item_type * itype, wtype->offmod = offmod; wtype->reload = reload; wtype->skill = sk; + assert(itype->rtype); itype->rtype->wtype = wtype; return wtype; @@ -309,7 +310,7 @@ armor_type *new_armortype(item_type * itype, double penalty, variant magres, assert(itype->rtype->atype == NULL); - atype = calloc(sizeof(armor_type), 1); + atype = calloc(1, sizeof(armor_type)); if (!atype) abort(); atype->itype = itype; diff --git a/src/kernel/messages.c b/src/kernel/messages.c index 48ea13f1f..ea16044f8 100644 --- a/src/kernel/messages.c +++ b/src/kernel/messages.c @@ -312,8 +312,10 @@ message *add_message(message_list ** pm, message * m) assert(m && m->type); if (m != NULL) { struct mlist *mnew = malloc(sizeof(struct mlist)); + if (!mnew) abort(); if (*pm == NULL) { *pm = malloc(sizeof(message_list)); + if (*pm == NULL) abort(); (*pm)->end = &(*pm)->begin; } mnew->msg = msg_addref(m); diff --git a/src/kernel/order.c b/src/kernel/order.c index b4467fa5b..44332d630 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -61,6 +61,7 @@ void odata_create(order_data **pdata, size_t len, const char *str) assert(pdata); data = malloc(sizeof(order_data) + len + 1); + if (!data) abort(); data->_refcount = 1; result = (char *)(data + 1); data->_str = (len > 0) ? result : NULL; @@ -230,6 +231,7 @@ order *copy_order(const order * src) { if (src != NULL) { order *ord = (order *)malloc(sizeof(order)); + if (!ord) abort(); ord->next = NULL; ord->command = src->command; ord->id = src->id; diff --git a/src/kernel/pathfinder.c b/src/kernel/pathfinder.c index 61cead5fd..ba37d651d 100644 --- a/src/kernel/pathfinder.c +++ b/src/kernel/pathfinder.c @@ -69,8 +69,10 @@ static node *new_node(region * r, int distance, node * prev) n = node_garbage; node_garbage = n->next; } - else + else { n = malloc(sizeof(node)); + if (!n) abort(); + } n->next = NULL; n->prev = prev; n->r = r; diff --git a/src/kernel/plane.c b/src/kernel/plane.c index 89de42618..4fc3ff816 100644 --- a/src/kernel/plane.c +++ b/src/kernel/plane.c @@ -231,7 +231,7 @@ plane *create_new_plane(int id, const char *name, int minx, int maxx, int miny, if (pl) return pl; pl = calloc(1, sizeof(plane)); - + if (!pl) abort(); pl->next = NULL; pl->id = id; if (name) diff --git a/src/kernel/pool.c b/src/kernel/pool.c index 70e02cb96..6805e1c0e 100644 --- a/src/kernel/pool.c +++ b/src/kernel/pool.c @@ -113,7 +113,8 @@ int change_reservation(unit * u, const item_type * itype, int value) rp = &(*rp)->next; res = *rp; if (!res) { - *rp = res = calloc(sizeof(reservation), 1); + *rp = res = calloc(1, sizeof(reservation)); + if (!res) abort(); res->type = itype; res->value = value; } @@ -138,7 +139,8 @@ int set_resvalue(unit * u, const item_type * itype, int value) if (!res) { if (!value) return 0; - *rp = res = calloc(sizeof(reservation), 1); + *rp = res = calloc(1, sizeof(reservation)); + if (!res) abort(); res->type = itype; res->value = value; } diff --git a/src/kernel/race.c b/src/kernel/race.c index 4a0d0689f..debf368ef 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -101,6 +101,7 @@ static void rc_setoption(race *rc, int k, const char *value) { variant *v = NULL; if (!rc->options) { rc->options = malloc(sizeof(rcoption)); + if (!rc->options) abort(); rc->options->key[0] = key; rc->options->key[1] = RCO_NONE; v = rc->options->value; @@ -202,6 +203,7 @@ race_t old_race(const struct race * rc) int i; if (!xrefs) { xrefs = malloc(sizeof(rc_xref) * MAXRACES); + if (!xrefs) abort(); } for (i = 0; i != MAXRACES; ++i) { xrefs[i].rc = get_race(i); @@ -244,6 +246,7 @@ void racelist_insert(struct race_list **rl, const struct race *r) { race_list *rl2 = (race_list *)malloc(sizeof(race_list)); + if (!rl2) abort(); rl2->data = r; rl2->next = *rl; @@ -340,7 +343,8 @@ race *rc_create(const char *zName) char zText[64]; assert(zName); - rc = (race *)calloc(sizeof(race), 1); + rc = (race *)calloc(1, sizeof(race)); + if (!rc) abort(); rc->mask_item = 1 << race_mask; ++race_mask; @@ -446,8 +450,10 @@ int rc_herb_trade(const struct race *rc) } void set_study_speed(race *rc, skill_t sk, int modifier) { - if (!rc->study_speed) + if (!rc->study_speed) { rc->study_speed = calloc(1, MAXSKILLS); + if (!rc->study_speed) abort(); + } rc->study_speed[sk] = (char)modifier; } diff --git a/src/kernel/region.c b/src/kernel/region.c index 5d2de2308..f8ce68ffd 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -471,6 +471,7 @@ void add_regionlist(region_list ** rl, region * r) { region_list *rl2 = (region_list *)malloc(sizeof(region_list)); + if (!rl2) abort(); rl2->data = r; rl2->next = *rl; @@ -799,7 +800,7 @@ static region *deleted_regions; void remove_region(region ** rlist, region * r) { - + assert(r); while (r->units) { unit *u = r->units; i_freeall(&u->items); @@ -1040,6 +1041,7 @@ void setluxuries(region * r, const luxury_type * sale) for (ltype = luxurytypes; ltype; ltype = ltype->next) { struct demand *dmd = malloc(sizeof(struct demand)); + if (!dmd) abort(); dmd->type = ltype; if (ltype != sale) dmd->value = 1 + rng_int() % 5; @@ -1165,6 +1167,7 @@ void terraform_region(region * r, const terrain_type * terrain) int mnr = 0; r->land = calloc(1, sizeof(land_region)); + if (!r->land) abort(); r->land->ownership = NULL; region_set_morale(r, MORALE_DEFAULT, -1); region_setname(r, makename()); @@ -1186,6 +1189,7 @@ void terraform_region(region * r, const terrain_type * terrain) } else { sr = calloc(1, sizeof(struct surround)); + if (!sr) abort(); } sr->next = nb; sr->type = sale->type; @@ -1321,6 +1325,7 @@ struct message *msg) imsg = imsg->next; if (imsg == NULL) { imsg = malloc(sizeof(struct individual_message)); + if (!imsg) abort(); imsg->next = r->individual_messages; imsg->msgs = NULL; r->individual_messages = imsg; @@ -1372,11 +1377,13 @@ void region_set_owner(struct region *r, struct faction *owner, int turn) assert(rule_region_owners()); if (r->land) { if (!r->land->ownership) { - r->land->ownership = malloc(sizeof(region_owner)); + region_owner *ro = malloc(sizeof(region_owner)); + if (!ro) abort(); assert(region_get_morale(r) == MORALE_DEFAULT); - r->land->ownership->owner = NULL; - r->land->ownership->last_owner = NULL; - r->land->ownership->flags = 0; + ro->owner = NULL; + ro->last_owner = NULL; + ro->flags = 0; + r->land->ownership = ro; } r->land->ownership->since_turn = turn; r->land->ownership->morale_turn = turn; diff --git a/src/kernel/resources.c b/src/kernel/resources.c index b5741236b..ad2b67d90 100644 --- a/src/kernel/resources.c +++ b/src/kernel/resources.c @@ -70,8 +70,9 @@ struct rawmaterial * add_resource(region * r, int level, int base, int divisor, const resource_type * rtype) { - struct rawmaterial *rm = calloc(sizeof(struct rawmaterial), 1); + struct rawmaterial *rm = calloc(1, sizeof(struct rawmaterial)); + if (!rm) abort(); rm->next = r->resources; r->resources = rm; rm->flags = 0; @@ -183,6 +184,7 @@ struct rawmaterial_type *rmt_create(struct resource_type *rtype) { if (!rtype->raw) { rawmaterial_type *rmtype = rtype->raw = malloc(sizeof(rawmaterial_type)); + if (!rmtype) abort(); rmtype->rtype = rtype; rmtype->terraform = terraform_default; rmtype->update = NULL; diff --git a/src/kernel/save.c b/src/kernel/save.c index f6ce25327..4c8e02fe0 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -150,6 +150,7 @@ void read_planes(gamedata *data) { if (pl == NULL) { pl = calloc(1, sizeof(plane)); + if (!pl) abort(); } else { log_warning("the plane with id=%d already exists.", id); @@ -239,6 +240,7 @@ static void read_owner(gamedata *data, region_owner ** powner) READ_INT(data->store, &since_turn); if (since_turn >= 0) { region_owner *owner = malloc(sizeof(region_owner)); + if (!owner) abort(); owner->since_turn = since_turn; READ_INT(data->store, &owner->morale_turn); if (data->version >= MOURNING_VERSION) { @@ -696,6 +698,7 @@ static region *readregion(gamedata *data, int x, int y) if (strcmp(name, "end") == 0) break; res = malloc(sizeof(rawmaterial)); + if (!res) abort(); res->rtype = rt_find(name); if (!res->rtype && strncmp("rm_", name, 3) == 0) { res->rtype = rt_find(name + 3); @@ -1203,6 +1206,7 @@ struct building *read_building(gamedata *data) { storage * store = data->store; b = (building *)calloc(1, sizeof(building)); + if (!b) abort(); READ_INT(store, &b->no); bhash(b); READ_STR(store, name, sizeof(name)); @@ -1257,6 +1261,7 @@ ship *read_ship(gamedata *data) storage *store = data->store; sh = (ship *)calloc(1, sizeof(ship)); + if (!sh) abort(); READ_INT(store, &sh->no); shash(sh); READ_STR(store, name, sizeof(name)); diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 5c47e5347..27de232e4 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -71,7 +71,8 @@ const ship_type *findshiptype(const char *name, const struct locale *lang) selist *ql; int qi; - sn = (local_names *)calloc(sizeof(local_names), 1); + sn = (local_names *)calloc(1, sizeof(local_names)); + if (!sn) abort(); sn->next = snames; sn->lang = lang; @@ -124,7 +125,8 @@ ship_type *st_get_or_create(const char * name) { ship_type * st = st_find_i(name); assert(!snames); if (!st) { - st = (ship_type *)calloc(sizeof(ship_type), 1); + st = (ship_type *)calloc(1, sizeof(ship_type)); + if (!st) abort(); st->_name = str_strdup(name); st->storm = 1.0; st->tac_bonus = 1.0; @@ -189,6 +191,7 @@ ship *new_ship(const ship_type * stype, region * r, const struct locale *lang) ship *sh = (ship *)calloc(1, sizeof(ship)); const char *sname = 0; + if (!sh) abort(); assert(stype); sh->no = newcontainerid(); sh->coast = NODIRECTION; diff --git a/src/kernel/skills.c b/src/kernel/skills.c index ea2022872..4fba7284f 100644 --- a/src/kernel/skills.c +++ b/src/kernel/skills.c @@ -39,7 +39,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /** skillmod attribut **/ static void init_skillmod(variant *var) { - var->v = calloc(sizeof(skillmod_data), 1); + var->v = calloc(1, sizeof(skillmod_data)); } /** temporary skill modification (NOT SAVED!). */ diff --git a/src/kernel/spellbook.c b/src/kernel/spellbook.c index d230992cf..345cdd96d 100644 --- a/src/kernel/spellbook.c +++ b/src/kernel/spellbook.c @@ -19,6 +19,7 @@ spellbook * create_spellbook(const char * name) { spellbook *result = (spellbook *)malloc(sizeof(spellbook)); + if (!result) abort(); result->name = name ? str_strdup(name) : 0; result->spells = 0; return result; @@ -79,6 +80,7 @@ void spellbook_addref(spellbook *sb, const char *name, int level) { assert(sb && name && level > 0); sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry)); + if (!sbe) abort(); spellref_init(&sbe->spref, NULL, name); sbe->level = level; selist_push(&sb->spells, sbe); @@ -95,6 +97,7 @@ void spellbook_add(spellbook *sb, spell *sp, int level) } #endif sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry)); + if (!sbe) abort(); spellref_init(&sbe->spref, sp, NULL); sbe->level = level; selist_push(&sb->spells, sbe); diff --git a/src/kernel/terrain.c b/src/kernel/terrain.c index f5041dc03..3f2390254 100644 --- a/src/kernel/terrain.c +++ b/src/kernel/terrain.c @@ -125,7 +125,7 @@ terrain_type * get_or_create_terrain(const char *name) { terrain_type *terrain = terrain_find_i(name); if (!terrain) { ++terrain_changes; - terrain = (terrain_type *)calloc(sizeof(terrain_type), 1); + terrain = (terrain_type *)calloc(1, sizeof(terrain_type)); if (terrain) { terrain->_name = str_strdup(name); terrain->next = registered_terrains; diff --git a/src/laws.c b/src/laws.c index 0dff64b61..421d1af31 100644 --- a/src/laws.c +++ b/src/laws.c @@ -1408,7 +1408,7 @@ static void init_prefixnames(void) in = in->next; } if (in == NULL) { - in = calloc(sizeof(local_names), 1); + in = calloc(1, sizeof(local_names)); if (!in) abort(); } in->next = pnames; diff --git a/src/magic.c b/src/magic.c index a90f36882..036003e28 100644 --- a/src/magic.c +++ b/src/magic.c @@ -244,7 +244,7 @@ static int a_ageicastle(struct attrib *a, void *owner) static void a_initicastle(variant *var) { - var->v = calloc(sizeof(icastle_data), 1); + var->v = calloc(1, sizeof(icastle_data)); } attrib_type at_icastle = { @@ -295,7 +295,7 @@ int get_spell_level_mage(const spell * sp, void * cbdata) static void init_mage(variant *var) { - var->v = calloc(sizeof(sc_mage), 1); + var->v = calloc(1, sizeof(sc_mage)); } static void free_mage(variant *var) diff --git a/src/renumber.c b/src/renumber.c index e562a0285..bfb43309a 100644 --- a/src/renumber.c +++ b/src/renumber.c @@ -56,7 +56,7 @@ void renumber_factions(void) ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); } else { - struct renum *r = calloc(sizeof(struct renum), 1); + struct renum *r = calloc(1, sizeof(struct renum)); r->next = *rn; r->attrib = a; r->faction = f; diff --git a/src/study.c b/src/study.c index a53942a39..3fb2d0a7d 100644 --- a/src/study.c +++ b/src/study.c @@ -154,7 +154,7 @@ int study_cost(struct unit *u, skill_t sk) static void init_learning(variant *var) { - var->v = calloc(sizeof(teaching_info), 1); + var->v = calloc(1, sizeof(teaching_info)); } static void done_learning(variant *var) diff --git a/src/summary.c b/src/summary.c index e01679d56..67e956401 100644 --- a/src/summary.c +++ b/src/summary.c @@ -404,7 +404,7 @@ summary *make_summary(void) while (plang && plang->locale != lang) plang = plang->next; if (!plang) { - plang = calloc(sizeof(struct language), 1); + plang = calloc(1, sizeof(struct language)); plang->next = s->languages; s->languages = plang; plang->locale = lang; diff --git a/src/triggers/changefaction.c b/src/triggers/changefaction.c index 8b1ead479..c29304992 100644 --- a/src/triggers/changefaction.c +++ b/src/triggers/changefaction.c @@ -51,7 +51,7 @@ typedef struct changefaction_data { static void changefaction_init(trigger * t) { - t->data.v = calloc(sizeof(changefaction_data), 1); + t->data.v = calloc(1, sizeof(changefaction_data)); } static void changefaction_free(trigger * t) diff --git a/src/triggers/changerace.c b/src/triggers/changerace.c index 5a834314a..19a84eea2 100644 --- a/src/triggers/changerace.c +++ b/src/triggers/changerace.c @@ -52,7 +52,7 @@ typedef struct changerace_data { static void changerace_init(trigger * t) { - t->data.v = calloc(sizeof(changerace_data), 1); + t->data.v = calloc(1, sizeof(changerace_data)); } static void changerace_free(trigger * t) diff --git a/src/triggers/createcurse.c b/src/triggers/createcurse.c index 544b9aafe..83f12b3be 100644 --- a/src/triggers/createcurse.c +++ b/src/triggers/createcurse.c @@ -56,7 +56,7 @@ typedef struct createcurse_data { static void createcurse_init(trigger * t) { - t->data.v = calloc(sizeof(createcurse_data), 1); + t->data.v = calloc(1, sizeof(createcurse_data)); } static void createcurse_free(trigger * t) diff --git a/src/triggers/createunit.c b/src/triggers/createunit.c index 82155de55..ec1521bee 100644 --- a/src/triggers/createunit.c +++ b/src/triggers/createunit.c @@ -55,7 +55,7 @@ typedef struct createunit_data { static void createunit_init(trigger * t) { - t->data.v = calloc(sizeof(createunit_data), 1); + t->data.v = calloc(1, sizeof(createunit_data)); } static void createunit_free(trigger * t) diff --git a/src/triggers/gate.c b/src/triggers/gate.c index ac3e44b88..0218f6a3c 100644 --- a/src/triggers/gate.c +++ b/src/triggers/gate.c @@ -86,7 +86,7 @@ static int gate_read(trigger * t, gamedata *data) static void gate_init(trigger * t) { - t->data.v = calloc(sizeof(gate_data), 1); + t->data.v = calloc(1, sizeof(gate_data)); } static void gate_done(trigger * t) diff --git a/src/triggers/giveitem.c b/src/triggers/giveitem.c index 118497c86..3e1047f2c 100644 --- a/src/triggers/giveitem.c +++ b/src/triggers/giveitem.c @@ -51,7 +51,7 @@ typedef struct giveitem_data { static void giveitem_init(trigger * t) { - t->data.v = calloc(sizeof(giveitem_data), 1); + t->data.v = calloc(1, sizeof(giveitem_data)); } static void giveitem_free(trigger * t) diff --git a/src/triggers/timeout.c b/src/triggers/timeout.c index 8e8a6b9bf..20c0b79fe 100644 --- a/src/triggers/timeout.c +++ b/src/triggers/timeout.c @@ -42,7 +42,7 @@ typedef struct timeout_data { static void timeout_init(trigger * t) { - t->data.v = calloc(sizeof(timeout_data), 1); + t->data.v = calloc(1, sizeof(timeout_data)); } static void timeout_free(trigger * t)