fix read/write of regioninfo.

This commit is contained in:
Enno Rehling 2017-09-16 09:03:42 +02:00
parent 1b19e8d19c
commit 0a3eb9ac7b
3 changed files with 9 additions and 5 deletions

View File

@ -1444,7 +1444,7 @@ void region_setinfo(struct region *r, const char *info)
{ {
assert(r->land); assert(r->land);
free(r->land->display); free(r->land->display);
r->land->display = info ? strdup(info) : 0; r->land->display = (info && info[0]) ? strdup(info) : 0;
} }
const char *region_getinfo(const region * r) const char *region_getinfo(const region * r)

View File

@ -885,12 +885,12 @@ void write_unit(gamedata *data, const unit * u)
WRITE_SECTION(data->store); WRITE_SECTION(data->store);
} }
static void read_regioninfo(gamedata *data, const region *r, char *info) { static void read_regioninfo(gamedata *data, const region *r, char *info, size_t len) {
if (lomem) { if (lomem) {
READ_STR(data->store, NULL, 0); READ_STR(data->store, NULL, 0);
} }
else { else {
READ_STR(data->store, info, sizeof(info)); READ_STR(data->store, info, len);
if (unicode_utf8_trim(info) != 0) { if (unicode_utf8_trim(info) != 0) {
log_warning("trim region %d info to '%s'", r->uid, info); log_warning("trim region %d info to '%s'", r->uid, info);
} }
@ -929,7 +929,7 @@ static region *readregion(gamedata *data, int x, int y)
r->land = 0; r->land = 0;
} }
if (data->version < LANDDISPLAY_VERSION) { if (data->version < LANDDISPLAY_VERSION) {
read_regioninfo(data, r, info); read_regioninfo(data, r, info, sizeof(info));
} }
READ_STR(data->store, name, sizeof(name)); READ_STR(data->store, name, sizeof(name));
@ -956,7 +956,7 @@ static region *readregion(gamedata *data, int x, int y)
rawmaterial **pres = &r->resources; rawmaterial **pres = &r->resources;
if (data->version >= LANDDISPLAY_VERSION) { if (data->version >= LANDDISPLAY_VERSION) {
read_regioninfo(data, r, info); read_regioninfo(data, r, info, sizeof(info));
} }
region_setinfo(r, info); region_setinfo(r, info);
READ_INT(data->store, &i); READ_INT(data->store, &i);

View File

@ -116,12 +116,15 @@ static void test_readwrite_region(CuTest * tc)
gamedata data; gamedata data;
storage store; storage store;
region *r; region *r;
const char * lipsum = "Lorem ipsum dolor sit amet";
test_setup(); test_setup();
r = test_create_region(0, 0, 0); r = test_create_region(0, 0, 0);
free(r->land->name); free(r->land->name);
r->land->name = strdup(" Hodor "); r->land->name = strdup(" Hodor ");
CuAssertStrEquals(tc, " Hodor ", r->land->name); CuAssertStrEquals(tc, " Hodor ", r->land->name);
region_setinfo(r, lipsum);
CuAssertStrEquals(tc, lipsum, r->land->display);
mstream_init(&data.strm); mstream_init(&data.strm);
gamedata_init(&data, &store, RELEASE_VERSION); gamedata_init(&data, &store, RELEASE_VERSION);
write_region(&data, r); write_region(&data, r);
@ -132,6 +135,7 @@ static void test_readwrite_region(CuTest * tc)
r = read_region(&data); r = read_region(&data);
CuAssertPtrNotNull(tc, r); CuAssertPtrNotNull(tc, r);
CuAssertStrEquals(tc, "Hodor", r->land->name); CuAssertStrEquals(tc, "Hodor", r->land->name);
CuAssertStrEquals(tc, lipsum, r->land->display);
regions = r; regions = r;
mstream_done(&data.strm); mstream_done(&data.strm);