Commit 28681e0a authored by Rusty Russell's avatar Rusty Russell

tal/str: fix error in tal_strndup()

It used strlen() on the source, which might not be valid.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 6e5c2f3f
...@@ -26,11 +26,9 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n) ...@@ -26,11 +26,9 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
char *ret; char *ret;
/* We have to let through NULL for take(). */ /* We have to let through NULL for take(). */
if (likely(p)) { if (likely(p))
len = strlen(p); len = strnlen(p, n);
if (len > n) else
len = n;
} else
len = n; len = n;
ret = tal_dup_(ctx, p, 1, len, 1, false, TAL_LABEL(char, "[]")); ret = tal_dup_(ctx, p, 1, len, 1, false, TAL_LABEL(char, "[]"));
......
#include <ccan/tal/str/str.h>
#include <stdlib.h>
#include <stdio.h>
#include <ccan/tal/str/str.c>
#include <ccan/tap/tap.h>
#include "helper.h"
int main(int argc, char *argv[])
{
char *str, *copy;
plan_tests(1);
str = malloc(5);
memcpy(str, "hello", 5);
/* We should be fine to strndup src without nul terminator. */
copy = tal_strndup(NULL, str, 5);
ok1(!strcmp(copy, "hello"));
tal_free(copy);
free(str);
return exit_status();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment