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

View File

@ -885,12 +885,12 @@ void write_unit(gamedata *data, const unit * u)
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) {
READ_STR(data->store, NULL, 0);
}
else {
READ_STR(data->store, info, sizeof(info));
READ_STR(data->store, info, len);
if (unicode_utf8_trim(info) != 0) {
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;
}
if (data->version < LANDDISPLAY_VERSION) {
read_regioninfo(data, r, info);
read_regioninfo(data, r, info, sizeof(info));
}
READ_STR(data->store, name, sizeof(name));
@ -956,7 +956,7 @@ static region *readregion(gamedata *data, int x, int y)
rawmaterial **pres = &r->resources;
if (data->version >= LANDDISPLAY_VERSION) {
read_regioninfo(data, r, info);
read_regioninfo(data, r, info, sizeof(info));
}
region_setinfo(r, info);
READ_INT(data->store, &i);

View File

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