From 73f16d5bb61cf2ac16a24c61a1d9ffc122c2811e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 29 Jan 2016 19:11:48 +0100 Subject: [PATCH] adding a join_path function to try and narrow down misbehavior. --- src/kernel/config.c | 25 ++++++++++++++++++------- src/kernel/config.h | 1 + src/kernel/save.test.c | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 21907ecc3..6cc31c3b0 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -653,15 +653,26 @@ void set_basepath(const char *path) g_basedir = path; } +#ifdef WIN32 +#define PATH_DELIM '\\' +#else +#define PATH_DELIM '/' +#endif + + +char * join_path(const char *p1, const char *p2, char *dst, size_t len) { + size_t sz; + assert(p1 && p2); + sz = strlcpy(dst, p1, len); + assert(sz < len); + dst[sz++] = PATH_DELIM; + strlcpy(dst + sz, p2, len - sz); + return dst; +} + static const char * relpath(char *buf, size_t sz, const char *path) { if (g_basedir) { - strlcpy(buf, g_basedir, sz); -#ifdef WIN32 - strlcat(buf, "\\", sz); -#else - strlcat(buf, "/", sz); -#endif - strlcat(buf, path, sz); + join_path(g_basedir, path, buf, sz); } else { strlcpy(buf, path, sz); diff --git a/src/kernel/config.h b/src/kernel/config.h index 257de940d..30aa25549 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -185,6 +185,7 @@ struct param; double config_get_flt(const char *key, double def); bool config_token(const char *key, const char *tok); + char * join_path(const char *p1, const char *p2, char *dst, size_t len); bool ExpensiveMigrants(void); int NMRTimeout(void); int LongHunger(const struct unit *u); diff --git a/src/kernel/save.test.c b/src/kernel/save.test.c index 5395344a2..1492750ab 100644 --- a/src/kernel/save.test.c +++ b/src/kernel/save.test.c @@ -40,7 +40,7 @@ static void test_readwrite_unit(CuTest * tc) fno = f->no; u = test_create_unit(f, r); _mkdir(datapath()); - sprintf(path, "%s/%s", datapath(), filename); + join_path(datapath(), filename, path, sizeof(path)); data = gamedata_open(path, "wb"); CuAssertPtrNotNull(tc, data); // TODO: intermittent test