Merge branch 'develop' into c99

This commit is contained in:
Enno Rehling 2017-01-11 09:30:50 +01:00 committed by GitHub
commit 05b6c1c418
10 changed files with 29 additions and 59 deletions

View File

@ -58,34 +58,6 @@ static int tolua_spawn_undead(lua_State * L)
return 0; return 0;
} }
static int fix_familiars(struct lua_State *L)
{
faction *f;
for (f = factions; f; f = f->next) {
unit *u;
for (u = f->units; u; u = u->nextF) {
struct sc_mage *mage = get_mage(u);
if (mage && is_familiar(u)) {
if (mage->spellbook && mage->magietyp == M_GRAY) {
equipment *eq;
char buffer[64];
spellbook_clear(mage->spellbook);
free(mage->spellbook);
mage->spellbook = 0;
snprintf(buffer, sizeof(buffer), "%s_familiar", u_race(u)->_name);
eq = get_equipment(buffer);
if (eq) {
equip_unit_mask(u, eq, EQUIP_SPELLS);
}
}
}
}
}
return 0;
}
void bind_monsters(struct lua_State *L) void bind_monsters(struct lua_State *L)
{ {
tolua_module(L, NULL, 0); tolua_module(L, NULL, 0);
@ -95,7 +67,6 @@ void bind_monsters(struct lua_State *L)
tolua_function(L, TOLUA_CAST "plan_monsters", tolua_planmonsters); tolua_function(L, TOLUA_CAST "plan_monsters", tolua_planmonsters);
tolua_function(L, TOLUA_CAST "spawn_undead", tolua_spawn_undead); tolua_function(L, TOLUA_CAST "spawn_undead", tolua_spawn_undead);
tolua_function(L, TOLUA_CAST "spawn_dragons", tolua_spawn_dragons); tolua_function(L, TOLUA_CAST "spawn_dragons", tolua_spawn_dragons);
tolua_function(L, TOLUA_CAST "fix_familiars", fix_familiars);
tolua_function(L, TOLUA_CAST "get_monsters", tolua_get_monsters); tolua_function(L, TOLUA_CAST "get_monsters", tolua_get_monsters);
} }
tolua_endmodule(L); tolua_endmodule(L);

View File

@ -100,7 +100,9 @@ static int tolua_storage_tostring(lua_State * L)
{ {
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0); gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
char name[64]; char name[64];
snprintf(name, sizeof(name), "<gamedata %p ver=%d>", (void *)data, data->version); // safe to use sprintf here, because:
// %p is at most 16 characters, %d 20, text is 16, comes to 53 with \0
sprintf(name, "<gamedata %p ver=%d>", (void *)data, data->version);
lua_pushstring(L, name); lua_pushstring(L, name);
return 1; return 1;
} }

View File

@ -233,7 +233,6 @@ static void test_give_men_requires_contact(CuTest * tc) {
struct give env = { 0 }; struct give env = { 0 };
message * msg; message * msg;
order *ord; order *ord;
char cmd[32];
test_setup_ex(tc); test_setup_ex(tc);
env.f1 = test_create_faction(0); env.f1 = test_create_faction(0);
@ -244,8 +243,7 @@ static void test_give_men_requires_contact(CuTest * tc) {
CuAssertIntEquals(tc, 1, env.dst->number); CuAssertIntEquals(tc, 1, env.dst->number);
CuAssertIntEquals(tc, 1, env.src->number); CuAssertIntEquals(tc, 1, env.src->number);
snprintf(cmd, sizeof(cmd), "%s ALLES PERSONEN", itoa36(env.dst->no)); ord = create_order(K_GIVE, env.f1->locale, "%s ALLES PERSONEN", itoa36(env.dst->no));
ord = create_order(K_GIVE, env.f1->locale, cmd);
test_clear_messages(env.f1); test_clear_messages(env.f1);
give_cmd(env.src, ord); give_cmd(env.src, ord);
CuAssertPtrEquals(tc, 0, test_find_messagetype(env.f1->msgs, "give_person")); CuAssertPtrEquals(tc, 0, test_find_messagetype(env.f1->msgs, "give_person"));
@ -307,7 +305,6 @@ static void test_give(CuTest * tc) {
static void test_give_cmd(CuTest * tc) { static void test_give_cmd(CuTest * tc) {
struct give env = { 0 }; struct give env = { 0 };
struct order *ord; struct order *ord;
char cmd[32];
test_setup_ex(tc); test_setup_ex(tc);
env.lang = test_create_locale(); env.lang = test_create_locale();
@ -316,8 +313,7 @@ static void test_give_cmd(CuTest * tc) {
i_change(&env.src->items, env.itype, 10); i_change(&env.src->items, env.itype, 10);
snprintf(cmd, sizeof(cmd), "%s 5 %s", itoa36(env.dst->no), LOC(env.f1->locale, env.itype->rtype->_name)); ord = create_order(K_GIVE, env.f1->locale, "%s 5 %s", itoa36(env.dst->no), LOC(env.f1->locale, env.itype->rtype->_name));
ord = create_order(K_GIVE, env.f1->locale, cmd);
assert(ord); assert(ord);
give_cmd(env.src, ord); give_cmd(env.src, ord);
CuAssertIntEquals(tc, 5, i_get(env.src->items, env.itype)); CuAssertIntEquals(tc, 5, i_get(env.src->items, env.itype));
@ -330,7 +326,6 @@ static void test_give_cmd(CuTest * tc) {
static void test_give_herbs(CuTest * tc) { static void test_give_herbs(CuTest * tc) {
struct give env = { 0 }; struct give env = { 0 };
struct order *ord; struct order *ord;
char cmd[32];
test_setup_ex(tc); test_setup_ex(tc);
test_create_world(); test_create_world();
@ -338,8 +333,7 @@ static void test_give_herbs(CuTest * tc) {
setup_give(&env); setup_give(&env);
i_change(&env.src->items, env.itype, 10); i_change(&env.src->items, env.itype, 10);
snprintf(cmd, sizeof(cmd), "%s %s", itoa36(env.dst->no), LOC(env.f1->locale, parameters[P_HERBS])); ord = create_order(K_GIVE, env.f1->locale, "%s %s", itoa36(env.dst->no), LOC(env.f1->locale, parameters[P_HERBS]));
ord = create_order(K_GIVE, env.f1->locale, cmd);
assert(ord); assert(ord);
give_cmd(env.src, ord); give_cmd(env.src, ord);

View File

@ -1167,7 +1167,7 @@ static void handlekey(state * st, int c)
region *first = (mr && mr->r && mr->r->next) ? mr->r->next : regions; region *first = (mr && mr->r && mr->r->next) ? mr->r->next : regions;
if (findmode == 'f') { if (findmode == 'f') {
snprintf(sbuffer, sizeof(sbuffer), "find-faction: %s", locate); slprintf(sbuffer, sizeof(sbuffer), "find-faction: %s", locate);
statusline(st->wnd_status->handle, sbuffer); statusline(st->wnd_status->handle, sbuffer);
f = findfaction(atoi36(locate)); f = findfaction(atoi36(locate));
if (f == NULL) { if (f == NULL) {

View File

@ -67,13 +67,13 @@ int json_export(stream * out, int flags) {
cJSON *json, *root = cJSON_CreateObject(); cJSON *json, *root = cJSON_CreateObject();
assert(out && out->api); assert(out && out->api);
if (regions && (flags & EXPORT_REGIONS)) { if (regions && (flags & EXPORT_REGIONS)) {
char id[32]; char id[32]; // TODO: static_assert(INT_MAX < 10^32)
region * r; region * r;
plane * p; plane * p;
cJSON_AddItemToObject(root, "planes", json = cJSON_CreateObject()); cJSON_AddItemToObject(root, "planes", json = cJSON_CreateObject());
for (p = planes; p; p = p->next) { for (p = planes; p; p = p->next) {
cJSON *data; cJSON *data;
snprintf(id, sizeof(id), "%d", p->id); sprintf(id, "%d", p->id); // safe, unless int is bigger than 64 bit
cJSON_AddItemToObject(json, id, data = cJSON_CreateObject()); cJSON_AddItemToObject(json, id, data = cJSON_CreateObject());
cJSON_AddNumberToObject(data, "x", p->minx); cJSON_AddNumberToObject(data, "x", p->minx);
cJSON_AddNumberToObject(data, "y", p->miny); cJSON_AddNumberToObject(data, "y", p->miny);
@ -85,7 +85,7 @@ int json_export(stream * out, int flags) {
cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject()); cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject());
for (r = regions; r; r = r->next) { for (r = regions; r; r = r->next) {
cJSON *data; cJSON *data;
snprintf(id, sizeof(id), "%d", r->uid); sprintf(id, "%d", r->uid); // safe, unless int is bigger than 64 bit
cJSON_AddItemToObject(json, id, data = cJSON_CreateObject()); cJSON_AddItemToObject(json, id, data = cJSON_CreateObject());
cJSON_AddNumberToObject(data, "x", r->x); cJSON_AddNumberToObject(data, "x", r->x);
cJSON_AddNumberToObject(data, "y", r->y); cJSON_AddNumberToObject(data, "y", r->y);

View File

@ -156,7 +156,10 @@ const char *resourcename(const resource_type * rtype, int flags)
} }
if (flags & NMF_PLURAL) { if (flags & NMF_PLURAL) {
static char name[64]; // FIXME: static return value static char name[64]; // FIXME: static return value
snprintf(name, sizeof(name), "%s_p", rtype->_name); size_t len = strlen(rtype->_name);
assert(len <= sizeof(name) - 3);
memcpy(name, rtype->_name, len);
strcpy(name + len, "_p");
return name; return name;
} }
return rtype->_name; return rtype->_name;

View File

@ -523,6 +523,8 @@ static void disable_feature(const char *str) {
char name[32]; char name[32];
int k; int k;
skill_t sk; skill_t sk;
size_t len;
sk = findskill(str); sk = findskill(str);
if (sk != NOSKILL) { if (sk != NOSKILL) {
enable_skill(sk, false); enable_skill(sk, false);
@ -534,7 +536,10 @@ static void disable_feature(const char *str) {
enable_keyword(k, false); enable_keyword(k, false);
return; return;
} }
snprintf(name, sizeof(name), "%s.enabled", str); len = strlen(str);
assert(len <= sizeof(name) - 9);
memcpy(name, str, len);
strcpy(name+len, ".enabled");
log_info("disable feature %s\n", name); log_info("disable feature %s\n", name);
config_set(name, "0"); config_set(name, "0");
} }
@ -796,10 +801,10 @@ static void json_settings(cJSON *json) {
else { else {
char value[32]; char value[32];
if (child->type == cJSON_Number && child->valuedouble && child->valueint<child->valuedouble) { if (child->type == cJSON_Number && child->valuedouble && child->valueint<child->valuedouble) {
snprintf(value, sizeof(value), "%f", child->valuedouble); sprintf(value, "%f", child->valuedouble);
} }
else { else {
snprintf(value, sizeof(value), "%d", child->valueint); sprintf(value, "%d", child->valueint);
} }
config_set(child->string, value); config_set(child->string, value);
} }

View File

@ -140,37 +140,34 @@ static void test_scale_number(CuTest *tc) {
static void test_unit_name(CuTest *tc) { static void test_unit_name(CuTest *tc) {
unit *u; unit *u;
char name[32];
test_cleanup(); test_cleanup();
test_create_world(); test_create_world();
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0)); u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
renumber_unit(u, 666);
unit_setname(u, "Hodor"); unit_setname(u, "Hodor");
snprintf(name, sizeof(name), "Hodor (%s)", itoa36(u->no)); CuAssertStrEquals(tc, "Hodor (ii)", unitname(u));
CuAssertStrEquals(tc, name, unitname(u));
test_cleanup(); test_cleanup();
} }
static void test_unit_name_from_race(CuTest *tc) { static void test_unit_name_from_race(CuTest *tc) {
unit *u; unit *u;
char name[32];
struct locale *lang; struct locale *lang;
test_cleanup(); test_cleanup();
test_create_world(); test_create_world();
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0)); u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
renumber_unit(u, 666);
unit_setname(u, NULL); unit_setname(u, NULL);
lang = get_or_create_locale("de"); lang = get_or_create_locale("de");
locale_setstring(lang, rc_name_s(u->_race, NAME_SINGULAR), "Mensch"); locale_setstring(lang, rc_name_s(u->_race, NAME_SINGULAR), "Mensch");
locale_setstring(lang, rc_name_s(u->_race, NAME_PLURAL), "Menschen"); locale_setstring(lang, rc_name_s(u->_race, NAME_PLURAL), "Menschen");
snprintf(name, sizeof(name), "Mensch (%s)", itoa36(u->no)); CuAssertStrEquals(tc, "Mensch (ii)", unitname(u));
CuAssertStrEquals(tc, name, unitname(u));
CuAssertStrEquals(tc, "Mensch", unit_getname(u)); CuAssertStrEquals(tc, "Mensch", unit_getname(u));
u->number = 2; u->number = 2;
snprintf(name, sizeof(name), "Menschen (%s)", itoa36(u->no)); CuAssertStrEquals(tc, "Menschen (ii)", unitname(u));
CuAssertStrEquals(tc, name, unitname(u));
CuAssertStrEquals(tc, "Menschen", unit_getname(u)); CuAssertStrEquals(tc, "Menschen", unit_getname(u));
test_cleanup(); test_cleanup();

View File

@ -521,7 +521,6 @@ static void test_pay_cmd_other_building(CuTest *tc) {
order *ord; order *ord;
faction *f; faction *f;
building *b; building *b;
char cmd[32];
test_setup(); test_setup();
setup_pay_cmd(&fix); setup_pay_cmd(&fix);
@ -531,8 +530,7 @@ static void test_pay_cmd_other_building(CuTest *tc) {
config_set("rules.region_owner_pay_building", "lighthouse"); config_set("rules.region_owner_pay_building", "lighthouse");
update_owners(b->region); update_owners(b->region);
snprintf(cmd, sizeof(cmd), "NOT %s", itoa36(b->no)); ord = create_order(K_PAY, f->locale, "NOT %s", itoa36(b->no));
ord = create_order(K_PAY, f->locale, cmd);
assert(ord); assert(ord);
CuAssertPtrEquals(tc, fix.u1, building_owner(b)); CuAssertPtrEquals(tc, fix.u1, building_owner(b));
CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord)); CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord));

View File

@ -13,7 +13,7 @@ int wrptr(char **ptr, size_t * size, int result)
{ {
size_t bytes = (size_t)result; size_t bytes = (size_t)result;
if (result < 0) { if (result < 0) {
// _snprintf buffer was too small // buffer was too small
if (*size > 0) { if (*size > 0) {
**ptr = 0; **ptr = 0;
*size = 0; *size = 0;