more allocation checks.

This commit is contained in:
Enno Rehling 2018-12-15 20:01:51 +01:00
parent 7ea64be314
commit 7b8bc8af0f
22 changed files with 68 additions and 13 deletions

View File

@ -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));

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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];

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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 ? */

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;