From 118708edd46530fa0848d01b8920ddd673eb72c0 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Dec 2017 10:11:09 +1030 Subject: [PATCH] tal/path: handle weird case of path_join("") It seems most sensible to make it a noop, but it definitely shouldn't access out of bounds as it does. Reported-by: Russ Dill Fixes: #61 Signed-off-by: Rusty Russell --- ccan/tal/path/path.c | 2 +- ccan/tal/path/test/run-join.c | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ccan/tal/path/path.c b/ccan/tal/path/path.c index 362152d2..c7a134e3 100644 --- a/ccan/tal/path/path.c +++ b/ccan/tal/path/path.c @@ -52,7 +52,7 @@ char *path_join(const tal_t *ctx, const char *base, const char *a) ret = tal_dup_arr(ctx, char, base, len, 1 + strlen(a) + 1); if (!ret) goto out; - if (ret[len-1] != PATH_SEP) + if (len != 0 && ret[len-1] != PATH_SEP) ret[len++] = PATH_SEP; strcpy(ret + len, a); diff --git a/ccan/tal/path/test/run-join.c b/ccan/tal/path/test/run-join.c index 3961cbb1..05d3d6e1 100644 --- a/ccan/tal/path/test/run-join.c +++ b/ccan/tal/path/test/run-join.c @@ -6,7 +6,7 @@ int main(void) { char *path, *ctx = tal_strdup(NULL, "ctx"); - plan_tests(34); + plan_tests(36); path = path_join(ctx, "foo", "bar"); ok1(streq(path, "foo/bar")); @@ -85,6 +85,11 @@ int main(void) ok1(!path); ok1(!tal_first(ctx)); + path = path_join(ctx, "", "bar"); + ok1(streq(path, "bar")); + ok1(tal_parent(path) == ctx); + tal_free(path); + tal_free(ctx); return exit_status(); -- 2.39.2