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

@ -55,7 +55,7 @@ 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;

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

@ -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) {
}
else if (strcmp((const char *)propValue, "init") == 0) {
btype->init = (void(*)(struct building_type *))fun;
} else if (strcmp((const char *)propValue, "age") == 0) {
}
else if (strcmp((const char *)propValue, "age") == 0) {
btype->age = (void(*)(struct building *))fun;
} else if (strcmp((const char *)propValue, "protection") == 0) {
}
else if (strcmp((const char *)propValue, "protection") == 0) {
btype->protection = (int(*)(struct building *, struct unit *))fun;
} else if (strcmp((const char *)propValue, "taxes") == 0) {
}
else if (strcmp((const char *)propValue, "taxes") == 0) {
btype->taxes = (double(*)(const struct building *, int))fun;
} else if (strcmp((const char *)propValue, "age") == 0) {
}
else if (strcmp((const char *)propValue, "age") == 0) {
btype->age = (void(*)(struct building *))fun;
} else {
}
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);
@ -827,18 +835,22 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype)
itype->give =
(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,
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,
struct order *))fun;
} else {
}
else {
log_error("unknown function type '%s' for item '%s'\n", (const char *)propValue, rtype->_name);
}
xmlFree(propValue);
@ -875,9 +887,11 @@ static int parse_rules(xmlDocPtr doc)
global.functions.wage =
(int(*)(const struct region *, const struct faction *,
const struct race *, int))fun;
} else if (strcmp((const char *)propValue, "maintenance") == 0) {
}
else if (strcmp((const char *)propValue, "maintenance") == 0) {
global.functions.maintenance = (int(*)(const struct unit *))fun;
} else {
}
else {
log_error("unknown function for rule '%s'\n", (const char *)propValue);
}
xmlFree(propValue);
@ -943,11 +957,14 @@ 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) {
}
else if (strcmp((const char *)propValue, "get") == 0) {
rtype->uget = (rtype_uget)fun;
} else if (strcmp((const char *)propValue, "name") == 0) {
}
else if (strcmp((const char *)propValue, "name") == 0) {
rtype->name = (rtype_name)fun;
} else {
}
else {
log_error("unknown function type '%s' for resource %s\n", (const char *)propValue, rtype->_name);
}
xmlFree(propValue);
@ -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);
@ -1060,9 +1082,11 @@ static int parse_resources(xmlDocPtr doc)
assert(propValue != NULL);
if (strcmp((const char *)propValue, "produce") == 0) {
rdata->produce = (rlimit_produce)fun;
} else if (strcmp((const char *)propValue, "limit") == 0) {
}
else if (strcmp((const char *)propValue, "limit") == 0) {
rdata->limit = (rlimit_limit)fun;
} else {
}
else {
log_error("unknown limit '%s' for resource %s\n", (const char *)propValue, rtype->_name);
}
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) {
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;
}
@ -1359,7 +1387,8 @@ static int parse_spellbooks(xmlDocPtr doc)
}
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);
}
}
@ -1459,7 +1488,8 @@ static int parse_spells(xmlDocPtr doc)
}
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);
@ -1516,7 +1549,8 @@ static int parse_spells(xmlDocPtr doc)
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) {
}
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);
}
@ -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) {
}
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, "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) {
}
else if (strcmp((const char *)propValue, "initfamiliar") == 0) {
rc->init_familiar = (void(*)(struct unit *))fun;
} else {
}
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);
@ -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));
}
}

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