tal/path: handle weird case of path_join("")
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 21 Dec 2017 23:41:09 +0000 (10:11 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 21 Dec 2017 23:42:31 +0000 (10:12 +1030)
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 <rusty@rustcorp.com.au>
ccan/tal/path/path.c
ccan/tal/path/test/run-join.c

index 362152d29cb261747f60a46d14db60addd6b928b..c7a134e3c9d9981699f9c95e4ad10fde71b9fa7d 100644 (file)
@@ -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);
 
index 3961cbb132401bd6a68f5ae4d5c4df5fb3922e12..05d3d6e1ea7312720e32469849f100dad04c21b7 100644 (file)
@@ -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();