From 0a3eb9ac7bd04962b4f45b9a3f916407411f1e65 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 16 Sep 2017 09:03:42 +0200 Subject: [PATCH] fix read/write of regioninfo. --- src/kernel/region.c | 2 +- src/kernel/save.c | 8 ++++---- src/kernel/save.test.c | 4 ++++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/kernel/region.c b/src/kernel/region.c index f770ec089..2d0a601b6 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -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) diff --git a/src/kernel/save.c b/src/kernel/save.c index 94a25c07e..629a2f45e 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -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); diff --git a/src/kernel/save.test.c b/src/kernel/save.test.c index 9501df896..81d5a507d 100644 --- a/src/kernel/save.test.c +++ b/src/kernel/save.test.c @@ -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);