more casual leak removal.

This commit is contained in:
Enno Rehling 2014-12-31 01:00:10 +01:00
parent 80be0a8e89
commit c39be8d599
10 changed files with 1979 additions and 1931 deletions

View File

@ -1047,23 +1047,24 @@ int get_param_int(const struct param *p, const char *key, int def)
int check_param(const struct param *p, const char *key, const char *searchvalue)
{
int result = 0;
const char *value = get_param(p, key);
if (!value) {
return 0;
}
char *p_value = malloc(sizeof(char)* (strlen(value) + 1));
strcpy(p_value, value);
char *p_value = _strdup(value);
const char *delimiter = " ,;";
char *v = strtok(p_value, delimiter);
while (v != NULL) {
if (strcmp(v, searchvalue) == 0)
{
return 1;
if (strcmp(v, searchvalue) == 0) {
result = 1;
break;
}
v = strtok(NULL, delimiter);
}
return 0;
free(p_value);
return result;
}
static const char *g_datadir;

View File

@ -304,7 +304,7 @@ static void json_ship(cJSON *json, ship_type *st) {
}
break;
case cJSON_Array:
st->coasts = (const terrain_type **)
st->coasts = (terrain_type **)
malloc(sizeof(terrain_type *) * (1+cJSON_GetArraySize(child)));
for (i=0,iter=child->child;iter;iter=iter->next) {
if (iter->type==cJSON_String) {

View File

@ -9,7 +9,6 @@
#include <tests.h>
#include <CuTest.h>
#include <stdlib.h>
#include <string.h>
static void test_create_order(CuTest *tc) {
char cmd[32];
@ -127,8 +126,7 @@ static void test_init_order(CuTest *tc) {
}
static void test_getstrtoken(CuTest *tc) {
char *cmd = _strdup("hurr \"durr\" \"\" \'\'");
init_tokens_str(cmd);
init_tokens_str("hurr \"durr\" \"\" \'\'");
CuAssertStrEquals(tc, "hurr", getstrtoken());
CuAssertStrEquals(tc, "durr", getstrtoken());
CuAssertStrEquals(tc, "", getstrtoken());
@ -139,8 +137,7 @@ static void test_getstrtoken(CuTest *tc) {
}
static void test_skip_token(CuTest *tc) {
char *cmd = _strdup("hurr \"durr\"");
init_tokens_str(cmd);
init_tokens_str("hurr \"durr\"");
skip_token();
CuAssertStrEquals(tc, "durr", getstrtoken());
CuAssertStrEquals(tc, 0, getstrtoken());

View File

@ -241,6 +241,7 @@ void free_ship(ship * s)
static void free_shiptype(void *ptr) {
ship_type *stype = (ship_type *)ptr;
free(stype->_name);
free(stype->coasts);
free(stype);
}

View File

@ -1,7 +1,7 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -27,7 +27,7 @@ extern "C" {
#define DAMAGE_SCALE 100 /* multiplier for sh->damage */
/* ship_type::flags */
/* ship_type::flags */
#define SFL_OPENSEA 0x01
#define SFL_FLY 0x02
#define SFL_NOCOAST 0x04
@ -55,14 +55,14 @@ extern "C" {
int df_bonus; /* Verändert den Verteidigungskill (default: 0) */
float tac_bonus;
const struct terrain_type **coasts; /* coast that this ship can land on */
struct terrain_type ** coasts; /* coast that this ship can land on */
struct construction *construction; /* how to build a ship */
} ship_type;
extern struct quicklist *shiptypes;
/* Alte Schiffstypen: */
/* Alte Schiffstypen: */
const ship_type *st_find(const char *name);
ship_type *st_get_or_create(const char *name);

View File

@ -27,6 +27,8 @@ void test_named_spellbooks(CuTest * tc)
sb = create_spellbook(0);
CuAssertPtrNotNull(tc, sb);
CuAssertPtrEquals(tc, 0, sb->name);
spellbook_clear(sb);
free(sb);
sb = create_spellbook("spells");
CuAssertPtrNotNull(tc, sb);

View File

@ -135,7 +135,7 @@ xml_readrequirements(xmlNodePtr * nodeTab, int nodeNr, requirement ** reqArray)
void
xml_readconstruction(xmlXPathContextPtr xpath, xmlNodeSetPtr nodeSet,
construction ** consPtr)
construction ** consPtr)
{
xmlNodePtr pushNode = xpath->node;
int k;
@ -191,7 +191,7 @@ xml_readconstruction(xmlXPathContextPtr xpath, xmlNodeSetPtr nodeSet,
if (propValue != NULL) {
pf_generic foo = get_function((const char *)propValue);
a_add(&con->attribs, make_skillmod(NOSKILL, SMF_PRODUCTION,
(skillmod_fun) foo, 1.0, 0));
(skillmod_fun)foo, 1.0, 0));
xmlFree(propValue);
}
@ -289,17 +289,23 @@ static int parse_buildings(xmlDocPtr doc)
btype->name =
(const char *(*)(const struct building_type *,
const struct building *, int))fun;
} else if (strcmp((const char *)propValue, "init") == 0) {
btype->init = (void (*)(struct building_type *))fun;
} else if (strcmp((const char *)propValue, "age") == 0) {
btype->age = (void (*)(struct building *))fun;
} else if (strcmp((const char *)propValue, "protection") == 0) {
btype->protection = (int (*)(struct building *, struct unit *))fun;
} else if (strcmp((const char *)propValue, "taxes") == 0) {
btype->taxes = (double (*)(const struct building *, int))fun;
} else if (strcmp((const char *)propValue, "age") == 0) {
btype->age = (void (*)(struct building *))fun;
} else {
}
else if (strcmp((const char *)propValue, "init") == 0) {
btype->init = (void(*)(struct building_type *))fun;
}
else if (strcmp((const char *)propValue, "age") == 0) {
btype->age = (void(*)(struct building *))fun;
}
else if (strcmp((const char *)propValue, "protection") == 0) {
btype->protection = (int(*)(struct building *, struct unit *))fun;
}
else if (strcmp((const char *)propValue, "taxes") == 0) {
btype->taxes = (double(*)(const struct building *, int))fun;
}
else if (strcmp((const char *)propValue, "age") == 0) {
btype->age = (void(*)(struct building *))fun;
}
else {
log_error("unknown function type '%s' for building %s\n", (const char *)propValue, btype->_name);
}
xmlFree(propValue);
@ -357,7 +363,8 @@ static int parse_calendar(xmlDocPtr doc)
months_per_year = 0;
if (nsetCalendars == NULL || nsetCalendars->nodeNr == 0) {
rv = -1;
} else
}
else
for (c = 0; c != nsetCalendars->nodeNr; ++c) {
xmlNodePtr calendar = nsetCalendars->nodeTab[c];
xmlXPathObjectPtr xpathWeeks, xpathMonths, xpathSeasons;
@ -528,14 +535,14 @@ static int parse_ships(xmlDocPtr doc)
if (k == 0) {
assert(st->coasts == NULL);
st->coasts =
(const terrain_type **)malloc(sizeof(const terrain_type *) *
(terrain_type **)malloc(sizeof(terrain_type *) *
(result->nodesetval->nodeNr + 1));
st->coasts[result->nodesetval->nodeNr] = NULL;
}
propValue = xmlGetProp(node, BAD_CAST "terrain");
assert(propValue != NULL);
st->coasts[c] = get_terrain((const char *)propValue);
st->coasts[c] = get_or_create_terrain((const char *)propValue);
if (st->coasts[c] != NULL)
++c;
else {
@ -723,7 +730,8 @@ static weapon_type *xml_readweapon(xmlXPathContextPtr xpath, item_type * itype)
wtype->attack =
(bool(*)(const struct troop *, const struct weapon_type *,
int *))fun;
} else {
}
else {
log_error("unknown function type '%s' for item '%s'\n", (const char *)propValue, itype->rtype->_name);
}
xmlFree(propValue);
@ -825,20 +833,24 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype)
assert(propValue != NULL);
if (strcmp((const char *)propValue, "give") == 0) {
itype->give =
(int (*)(struct unit *, struct unit *, const struct item_type *, int,
(int(*)(struct unit *, struct unit *, const struct item_type *, int,
struct order *))fun;
} else if (strcmp((const char *)propValue, "use") == 0) {
}
else if (strcmp((const char *)propValue, "use") == 0) {
itype->use =
(int (*)(struct unit *, const struct item_type *, int,
(int(*)(struct unit *, const struct item_type *, int,
struct order *))fun;
} else if (strcmp((const char *)propValue, "canuse") == 0) {
}
else if (strcmp((const char *)propValue, "canuse") == 0) {
itype->canuse =
(bool(*)(const struct unit *, const struct item_type *))fun;
} else if (strcmp((const char *)propValue, "useonother") == 0) {
}
else if (strcmp((const char *)propValue, "useonother") == 0) {
itype->useonother =
(int (*)(struct unit *, int, const struct item_type *, int,
(int(*)(struct unit *, int, const struct item_type *, int,
struct order *))fun;
} else {
}
else {
log_error("unknown function type '%s' for item '%s'\n", (const char *)propValue, rtype->_name);
}
xmlFree(propValue);
@ -873,11 +885,13 @@ static int parse_rules(xmlDocPtr doc)
assert(propValue != NULL);
if (strcmp((const char *)propValue, "wage") == 0) {
global.functions.wage =
(int (*)(const struct region *, const struct faction *,
(int(*)(const struct region *, const struct faction *,
const struct race *, int))fun;
} else if (strcmp((const char *)propValue, "maintenance") == 0) {
global.functions.maintenance = (int (*)(const struct unit *))fun;
} else {
}
else if (strcmp((const char *)propValue, "maintenance") == 0) {
global.functions.maintenance = (int(*)(const struct unit *))fun;
}
else {
log_error("unknown function for rule '%s'\n", (const char *)propValue);
}
xmlFree(propValue);
@ -942,12 +956,15 @@ static int parse_resources(xmlDocPtr doc)
assert(propValue != NULL);
if (strcmp((const char *)propValue, "change") == 0) {
rtype->uchange = (rtype_uchange) fun;
} else if (strcmp((const char *)propValue, "get") == 0) {
rtype->uget = (rtype_uget) fun;
} else if (strcmp((const char *)propValue, "name") == 0) {
rtype->name = (rtype_name) fun;
} else {
rtype->uchange = (rtype_uchange)fun;
}
else if (strcmp((const char *)propValue, "get") == 0) {
rtype->uget = (rtype_uget)fun;
}
else if (strcmp((const char *)propValue, "name") == 0) {
rtype->name = (rtype_name)fun;
}
else {
log_error("unknown function type '%s' for resource %s\n", (const char *)propValue, rtype->_name);
}
xmlFree(propValue);
@ -965,7 +982,7 @@ static int parse_resources(xmlDocPtr doc)
if (a == NULL)
a = a_add(&rtype->attribs, a_new(&at_resourcelimit));
rdata = (resource_limit *) a->data.v;
rdata = (resource_limit *)a->data.v;
rtype->flags |= RTF_LIMITED;
xpath->node = limit;
xmlXPathFreeObject(result);
@ -1000,13 +1017,16 @@ static int parse_resources(xmlDocPtr doc)
if (strcmp((const char *)propValue, "skill") == 0) {
rdata->modifiers[k].value.i = xml_ivalue(node, "value", 0);
rdata->modifiers[k].flags = RMF_SKILL;
} else if (strcmp((const char *)propValue, "material") == 0) {
}
else if (strcmp((const char *)propValue, "material") == 0) {
rdata->modifiers[k].value.f = (float)xml_fvalue(node, "value", 0);
rdata->modifiers[k].flags = RMF_SAVEMATERIAL;
} else if (strcmp((const char *)propValue, "resource") == 0) {
}
else if (strcmp((const char *)propValue, "resource") == 0) {
rdata->modifiers[k].value.f = (float)xml_fvalue(node, "value", 0);
rdata->modifiers[k].flags = RMF_SAVERESOURCE;
} else if (strcmp((const char *)propValue, "require") == 0) {
}
else if (strcmp((const char *)propValue, "require") == 0) {
xmlChar *propBldg = xmlGetProp(node, BAD_CAST "building");
if (propBldg != NULL) {
btype = bt_get_or_create((const char *)propBldg);
@ -1014,7 +1034,8 @@ static int parse_resources(xmlDocPtr doc)
rdata->modifiers[k].flags = RMF_REQUIREDBUILDING;
xmlFree(propBldg);
}
} else {
}
else {
log_error("unknown type '%s' for resourcelimit-modifier '%s'\n", (const char *)propValue, rtype->_name);
}
xmlFree(propValue);
@ -1031,7 +1052,8 @@ static int parse_resources(xmlDocPtr doc)
if (propFlag != NULL) {
if (strcmp((const char *)propFlag, "logging") == 0) {
rdata->guard |= GUARD_TREES;
} else if (strcmp((const char *)propFlag, "mining") == 0) {
}
else if (strcmp((const char *)propFlag, "mining") == 0) {
rdata->guard |= GUARD_MINING;
}
xmlFree(propFlag);
@ -1059,10 +1081,12 @@ static int parse_resources(xmlDocPtr doc)
propValue = xmlGetProp(node, BAD_CAST "name");
assert(propValue != NULL);
if (strcmp((const char *)propValue, "produce") == 0) {
rdata->produce = (rlimit_produce) fun;
} else if (strcmp((const char *)propValue, "limit") == 0) {
rdata->limit = (rlimit_limit) fun;
} else {
rdata->produce = (rlimit_produce)fun;
}
else if (strcmp((const char *)propValue, "limit") == 0) {
rdata->limit = (rlimit_limit)fun;
}
else {
log_error("unknown limit '%s' for resource %s\n", (const char *)propValue, rtype->_name);
}
xmlFree(propValue);
@ -1139,7 +1163,7 @@ static void add_callbacks(equipment * eq, xmlNodeSetPtr nsetItems)
if (propValue != NULL) {
fun = get_function((const char *)propValue);
if (fun) {
equipment_setcallback(eq, (void (*)(const struct equipment *,
equipment_setcallback(eq, (void(*)(const struct equipment *,
struct unit *))fun);
}
xmlFree(propValue);
@ -1162,11 +1186,13 @@ static void add_spells(equipment * eq, xmlNodeSetPtr nsetItems)
sp = find_spell((const char *)propValue);
if (!sp) {
log_error("no spell '%s' for equipment-set '%s'\n", (const char *)propValue, eq->name);
} else {
}
else {
int level = xml_ivalue(node, "level", 0);
if (level>0) {
if (level > 0) {
equipment_addspell(eq, sp, level);
} else {
}
else {
log_error("spell '%s' for equipment-set '%s' has no level\n", sp->sname, eq->name);
}
}
@ -1190,7 +1216,8 @@ static void add_skills(equipment * eq, xmlNodeSetPtr nsetSkills)
if (sk == NOSKILL) {
log_error("unknown skill '%s' in equipment-set %s\n", (const char *)propValue, eq->name);
xmlFree(propValue);
} else {
}
else {
xmlFree(propValue);
propValue = xmlGetProp(node, BAD_CAST "level");
if (propValue != NULL) {
@ -1338,7 +1365,8 @@ static int parse_spellbooks(xmlDocPtr doc)
if (propValue) {
sb = get_spellbook((const char *)propValue);
xmlFree(propValue);
} else {
}
else {
log_error("spellbook at index '%d' has n name\n", i);
continue;
}
@ -1357,9 +1385,10 @@ static int parse_spellbooks(xmlDocPtr doc)
sp = find_spell((const char *)propValue);
xmlFree(propValue);
}
if (sp && level>0) {
if (sp && level > 0) {
spellbook_add(sb, sp, level);
} else {
}
else {
log_error("invalid entry at index '%d' in spellbook '%s'\n", k, sb->name);
}
}
@ -1457,9 +1486,10 @@ static int parse_spells(xmlDocPtr doc)
if (!cast) {
log_error("no spell cast function registered for '%s'\n", sp->sname);
}
strlcpy(zText+7, sp->sname, sizeof(zText)-7);
strlcpy(zText + 7, sp->sname, sizeof(zText) - 7);
fumble = get_function(zText);
} else {
}
else {
for (k = 0; k != result->nodesetval->nodeNr; ++k) {
xmlNodePtr node = result->nodesetval->nodeTab[k];
pf_generic fun;
@ -1469,12 +1499,15 @@ static int parse_spells(xmlDocPtr doc)
if (strcmp((const char *)propValue, "cast") == 0) {
if (fun) {
cast = fun;
} else {
}
else {
log_error("unknown function name '%s' for spell '%s'\n", (const char *)propValue, sp->sname);
}
} else if (fun && strcmp((const char *)propValue, "fumble") == 0) {
}
else if (fun && strcmp((const char *)propValue, "fumble") == 0) {
fumble = fun;
} else {
}
else {
log_error("unknown function type '%s' for spell '%s'\n", (const char *)propValue, sp->sname);
}
xmlFree(propValue);
@ -1489,7 +1522,7 @@ static int parse_spells(xmlDocPtr doc)
result = xmlXPathEvalExpression(BAD_CAST "resource", xpath);
if (result->nodesetval->nodeNr) {
sp->components =
(spell_component *) malloc(sizeof(spell_component) *
(spell_component *)malloc(sizeof(spell_component) *
(result->nodesetval->nodeNr + 1));
sp->components[result->nodesetval->nodeNr].type = 0;
}
@ -1512,12 +1545,13 @@ static int parse_spells(xmlDocPtr doc)
propValue = xmlGetProp(node, BAD_CAST "cost");
if (propValue != NULL) {
if (strcmp((const char *)propValue, "linear") == 0) {
if ((sp->sptyp&SPELLLEVEL)==0) {
if ((sp->sptyp&SPELLLEVEL) == 0) {
log_error("spell '%s' has linear cost but fixed level\n", sp->sname);
}
component->cost = SPC_LINEAR;
} else if (strcmp((const char *)propValue, "level") == 0) {
if ((sp->sptyp&SPELLLEVEL)==0) {
}
else if (strcmp((const char *)propValue, "level") == 0) {
if ((sp->sptyp&SPELLLEVEL) == 0) {
log_error("spell '%s' has levelled cost but fixed level\n", sp->sname);
}
component->cost = SPC_LEVEL;
@ -1692,7 +1726,8 @@ static int parse_races(xmlDocPtr doc)
for (child = node->children; child; child = child->next) {
if (strcmp((const char *)child->name, "ai") == 0) {
parse_ai(rc, child);
} else if (strcmp((const char *)child->name, "param") == 0) {
}
else if (strcmp((const char *)child->name, "param") == 0) {
parse_param(&rc->parameters, child);
}
}
@ -1718,7 +1753,8 @@ static int parse_races(xmlDocPtr doc)
rc->study_speed = calloc(1, MAXSKILLS);
rc->study_speed[sk] = (char)speed;
}
} else {
}
else {
log_error("unknown skill '%s' in race '%s'\n", (const char *)propValue, rc->_name);
}
xmlFree(propValue);
@ -1741,19 +1777,25 @@ static int parse_races(xmlDocPtr doc)
assert(propValue != NULL);
if (strcmp((const char *)propValue, "name") == 0) {
rc->generate_name = (const char *(*)(const struct unit *))fun;
} else if (strcmp((const char *)propValue, "describe") == 0) {
}
else if (strcmp((const char *)propValue, "describe") == 0) {
rc->describe =
(const char *(*)(const struct unit *, const struct locale *))fun;
} else if (strcmp((const char *)propValue, "age") == 0) {
rc->age = (void (*)(struct unit *))fun;
} else if (strcmp((const char *)propValue, "move") == 0) {
}
else if (strcmp((const char *)propValue, "age") == 0) {
rc->age = (void(*)(struct unit *))fun;
}
else if (strcmp((const char *)propValue, "move") == 0) {
rc->move_allowed =
(bool(*)(const struct region *, const struct region *))fun;
} else if (strcmp((const char *)propValue, "itemdrop") == 0) {
}
else if (strcmp((const char *)propValue, "itemdrop") == 0) {
rc->itemdrop = (struct item * (*)(const struct race *, int))fun;
} else if (strcmp((const char *)propValue, "initfamiliar") == 0) {
rc->init_familiar = (void (*)(struct unit *))fun;
} else {
}
else if (strcmp((const char *)propValue, "initfamiliar") == 0) {
rc->init_familiar = (void(*)(struct unit *))fun;
}
else {
log_error("unknown function type '%s' for race %s\n", (const char *)propValue, rc->_name);
}
xmlFree(propValue);
@ -1773,7 +1815,8 @@ static int parse_races(xmlDocPtr doc)
if (xml_bvalue(node, "default", false)) {
rc->familiars[k] = rc->familiars[0];
rc->familiars[0] = frc;
} else {
}
else {
rc->familiars[k] = frc;
}
xmlFree(propValue);
@ -1804,7 +1847,8 @@ static int parse_races(xmlDocPtr doc)
if (propValue != NULL) {
attack->data.dice = _strdup((const char *)propValue);
xmlFree(propValue);
} else {
}
else {
attack->data.sp = xml_spell(node, "spell");
if (attack->data.sp) {
attack->level = xml_ivalue(node, "level", 0);
@ -2000,7 +2044,8 @@ static int parse_messages(xmlDocPtr doc)
mtype = mt_find((const char *)propValue);
if (mtype == NULL) {
mtype = mt_register(mt_new((const char *)propValue, (const char **)argv));
} else {
}
else {
assert(argv != NULL || !"cannot redefine arguments of message now");
}
xmlFree(propValue);
@ -2050,7 +2095,7 @@ static int parse_messages(xmlDocPtr doc)
static void
xml_readstrings(xmlXPathContextPtr xpath, xmlNodePtr * nodeTab, int nodeNr,
bool names)
bool names)
{
int i;
@ -2087,7 +2132,8 @@ xml_readstrings(xmlXPathContextPtr xpath, xmlNodePtr * nodeTab, int nodeNr,
locale_setstring(lang, zName, (const char *)propText);
}
xmlFree(propText);
} else {
}
else {
log_warning("string %s has no text in locale %s\n", zName, locale_name(lang));
}
}
@ -2118,7 +2164,7 @@ static int parse_strings(xmlDocPtr doc)
static void
xml_readprefixes(xmlXPathContextPtr xpath, xmlNodePtr * nodeTab, int nodeNr,
bool names)
bool names)
{
int i;
@ -2214,7 +2260,7 @@ static int parse_main(xmlDocPtr doc)
xmlNodePtr node = nodes->nodeTab[i];
xmlChar *propName = xmlGetProp(node, BAD_CAST "name");
skill_t sk = findskill((const char *)propName);
if (sk!=NOSKILL) {
if (sk != NOSKILL) {
bool enable = xml_bvalue(node, "enable", true);
enable_skill(sk, enable);
}

View File

@ -18,7 +18,7 @@ extern "C" {
extern void register_xmlreader(void);
/* game-specific callbacks */
extern void (*set_spelldata_cb) (struct spell * sp);
extern void(*set_spelldata_cb) (struct spell * sp);
#ifdef __cplusplus
}
#endif

View File

@ -28,7 +28,7 @@ static void test_ship_not_allowed_in_coast(CuTest * tc)
ttype = test_create_terrain("glacier", LAND_REGION | ARCTIC_REGION | WALK_INTO | SAIL_INTO);
otype = test_create_terrain("ocean", SEA_REGION | SAIL_INTO);
stype = test_create_shiptype("derp");
stype->coasts = (const struct terrain_type **)calloc(2, sizeof(const struct terrain_type *));
stype->coasts = (struct terrain_type **)calloc(2, sizeof(struct terrain_type *));
r1 = test_create_region(0, 0, ttype);
r2 = test_create_region(1, 0, otype);

View File

@ -86,6 +86,7 @@ int RunAllTests(void)
log_flags = flags;
fail_count = suite->failCount;
CuSuiteDelete(suite);
kernel_done();
return fail_count;
}