fixing memory bug

This commit is contained in:
Steffen Mecke 2015-11-13 15:35:17 +01:00
parent 477d69152f
commit f9c2994de1
2 changed files with 12 additions and 8 deletions

View File

@ -347,9 +347,7 @@ static void test_spell_syntax(CuTest *tc, char *msg, spell_fixture *spell, char
} }
static void set_parameter(spell_fixture spell, char *value) { static void set_parameter(spell_fixture spell, char *value) {
if (spell.sp->parameter) free(spell.sp->parameter);
strcpy(spell.sp->parameter, value);
else
spell.sp->parameter = _strdup(value); spell.sp->parameter = _strdup(value);
} }
@ -391,15 +389,19 @@ static void test_write_spell_syntax(CuTest *tc) {
test_spell_syntax(tc, "r", &spell, " ZAUBERE \"Testzauber\" <x> <y>"); test_spell_syntax(tc, "r", &spell, " ZAUBERE \"Testzauber\" <x> <y>");
set_parameter(spell, "bc"); set_parameter(spell, "bc");
free(spell.sp->syntax);
spell.sp->syntax = _strdup("hodor"); spell.sp->syntax = _strdup("hodor");
test_spell_syntax(tc, "bc hodor", &spell, " ZAUBERE \"Testzauber\" <bnr> <Hodor>"); test_spell_syntax(tc, "bc hodor", &spell, " ZAUBERE \"Testzauber\" <bnr> <Hodor>");
free(spell.sp->syntax); free(spell.sp->syntax);
spell.sp->syntax = 0;
/* no idea what ? is supposed to mean, optional parameter maybe? /* There are no spells with optional parameters, so we don't force this, for now
set_parameter(spell, "kcc?"); set_parameter(spell, "c?");
spell.sp->syntax = _strdup("hodor");
test_spell_syntax(tc, "kcc?", &spell, " ZAUBERE \"Testzauber\" <bnr>");
free(spell.sp->syntax); free(spell.sp->syntax);
spell.sp->syntax = _strdup("hodor");
test_spell_syntax(tc, "c?", &spell, " ZAUBERE \"Testzauber\" [<Hodor>]");
free(spell.sp->syntax);
spell.sp->syntax = 0;
*/ */
set_parameter(spell, "kc+"); set_parameter(spell, "kc+");

View File

@ -205,6 +205,8 @@ spell * test_create_spell(void)
sp->components[2].amount = 1; sp->components[2].amount = 1;
sp->components[2].type = get_resourcetype(R_HORSE); sp->components[2].type = get_resourcetype(R_HORSE);
sp->components[2].cost = SPC_LINEAR; sp->components[2].cost = SPC_LINEAR;
sp->syntax = 0;
sp->parameter = 0;
return sp; return sp;
} }