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 check_param(const struct param *p, const char *key, const char *searchvalue)
{ {
int result = 0;
const char *value = get_param(p, key); const char *value = get_param(p, key);
if (!value) { if (!value) {
return 0; return 0;
} }
char *p_value = malloc(sizeof(char)* (strlen(value) + 1)); char *p_value = _strdup(value);
strcpy(p_value, value);
const char *delimiter = " ,;"; const char *delimiter = " ,;";
char *v = strtok(p_value, delimiter); char *v = strtok(p_value, delimiter);
while (v != NULL) { while (v != NULL) {
if (strcmp(v, searchvalue) == 0) if (strcmp(v, searchvalue) == 0) {
{ result = 1;
return 1; break;
} }
v = strtok(NULL, delimiter); v = strtok(NULL, delimiter);
} }
return 0; free(p_value);
return result;
} }
static const char *g_datadir; static const char *g_datadir;

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
/* /*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de> Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
@ -27,46 +27,46 @@ extern "C" {
#define DAMAGE_SCALE 100 /* multiplier for sh->damage */ #define DAMAGE_SCALE 100 /* multiplier for sh->damage */
/* ship_type::flags */ /* ship_type::flags */
#define SFL_OPENSEA 0x01 #define SFL_OPENSEA 0x01
#define SFL_FLY 0x02 #define SFL_FLY 0x02
#define SFL_NOCOAST 0x04 #define SFL_NOCOAST 0x04
typedef struct ship_type { typedef struct ship_type {
char *_name; char *_name;
int range; /* range in regions */ int range; /* range in regions */
int flags; /* flags */ int flags; /* flags */
int combat; /* modifier for combat */ int combat; /* modifier for combat */
double storm; /* multiplier for chance to drift in storm */ double storm; /* multiplier for chance to drift in storm */
double damage; /* multiplier for damage taken by the ship */ double damage; /* multiplier for damage taken by the ship */
int cabins; /* max. cabins (weight) */ int cabins; /* max. cabins (weight) */
int cargo; /* max. cargo (weight) */ int cargo; /* max. cargo (weight) */
int cptskill; /* min. skill of captain */ int cptskill; /* min. skill of captain */
int minskill; /* min. skill to sail this (crew) */ int minskill; /* min. skill to sail this (crew) */
int sumskill; /* min. sum of crew+captain */ int sumskill; /* min. sum of crew+captain */
int fishing; /* weekly income from fishing */ int fishing; /* weekly income from fishing */
int at_bonus; /* Verändert den Angriffsskill (default: 0) */ int at_bonus; /* Verändert den Angriffsskill (default: 0) */
int df_bonus; /* Verändert den Verteidigungskill (default: 0) */ int df_bonus; /* Verändert den Verteidigungskill (default: 0) */
float tac_bonus; 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 */ struct construction *construction; /* how to build a ship */
} ship_type; } ship_type;
extern struct quicklist *shiptypes; extern struct quicklist *shiptypes;
/* Alte Schiffstypen: */ /* Alte Schiffstypen: */
const ship_type *st_find(const char *name); const ship_type *st_find(const char *name);
ship_type *st_get_or_create(const char *name); ship_type *st_get_or_create(const char *name);
void free_shiptypes(void); void free_shiptypes(void);
#define NOSHIP NULL #define NOSHIP NULL
@ -80,51 +80,51 @@ extern "C" {
#define SFL_SAVEMASK (SF_FLYING) #define SFL_SAVEMASK (SF_FLYING)
#define INCOME_FISHING 10 #define INCOME_FISHING 10
typedef struct ship { typedef struct ship {
struct ship *next; struct ship *next;
struct ship *nexthash; struct ship *nexthash;
struct unit * _owner; /* never use directly, always use ship_owner() */ struct unit * _owner; /* never use directly, always use ship_owner() */
int no; int no;
struct region *region; struct region *region;
char *name; char *name;
char *display; char *display;
struct attrib *attribs; struct attrib *attribs;
int size; int size;
int damage; /* damage in 100th of a point of size */ int damage; /* damage in 100th of a point of size */
int flags; int flags;
const struct ship_type *type; const struct ship_type *type;
direction_t coast; direction_t coast;
} ship; } ship;
void damage_ship(struct ship * sh, double percent); void damage_ship(struct ship * sh, double percent);
void ship_set_owner(struct unit * u); void ship_set_owner(struct unit * u);
struct unit *ship_owner(const struct ship *sh); struct unit *ship_owner(const struct ship *sh);
void ship_update_owner(struct ship * sh); void ship_update_owner(struct ship * sh);
extern const char *shipname(const struct ship *self); extern const char *shipname(const struct ship *self);
extern int shipcapacity(const struct ship *sh); extern int shipcapacity(const struct ship *sh);
extern void getshipweight(const struct ship *sh, int *weight, int *cabins); extern void getshipweight(const struct ship *sh, int *weight, int *cabins);
extern ship *new_ship(const struct ship_type *stype, struct region *r, extern ship *new_ship(const struct ship_type *stype, struct region *r,
const struct locale *lang); const struct locale *lang);
extern const char *write_shipname(const struct ship *sh, char *buffer, extern const char *write_shipname(const struct ship *sh, char *buffer,
size_t size); size_t size);
extern struct ship *findship(int n); extern struct ship *findship(int n);
extern struct ship *findshipr(const struct region *r, int n); extern struct ship *findshipr(const struct region *r, int n);
extern const struct ship_type *findshiptype(const char *s, extern const struct ship_type *findshiptype(const char *s,
const struct locale *lang); const struct locale *lang);
extern void write_ship_reference(const struct ship *sh, extern void write_ship_reference(const struct ship *sh,
struct storage *store); struct storage *store);
extern void remove_ship(struct ship **slist, struct ship *s); extern void remove_ship(struct ship **slist, struct ship *s);
extern void free_ship(struct ship *s); extern void free_ship(struct ship *s);
extern void free_ships(void); extern void free_ships(void);
extern const char *ship_getname(const struct ship *self); extern const char *ship_getname(const struct ship *self);
extern void ship_setname(struct ship *self, const char *name); extern void ship_setname(struct ship *self, const char *name);
int shipspeed(const struct ship *sh, const struct unit *u); int shipspeed(const struct ship *sh, const struct unit *u);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -15,10 +15,10 @@ without prior permission by the authors of Eressea.
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern void register_xmlreader(void); extern void register_xmlreader(void);
/* game-specific callbacks */ /* game-specific callbacks */
extern void (*set_spelldata_cb) (struct spell * sp); extern void(*set_spelldata_cb) (struct spell * sp);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #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); ttype = test_create_terrain("glacier", LAND_REGION | ARCTIC_REGION | WALK_INTO | SAIL_INTO);
otype = test_create_terrain("ocean", SEA_REGION | SAIL_INTO); otype = test_create_terrain("ocean", SEA_REGION | SAIL_INTO);
stype = test_create_shiptype("derp"); 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); r1 = test_create_region(0, 0, ttype);
r2 = test_create_region(1, 0, otype); r2 = test_create_region(1, 0, otype);

View File

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