From 64e9e7145aac9502655c5799ab711b9766c1da57 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 19 Jan 2016 17:06:55 +1030 Subject: [PATCH] tal/str: fix infinite loop of tal_fmt() with empty string. Signed-off-by: Rusty Russell --- ccan/tal/str/str.c | 2 +- ccan/tal/str/test/run-fmt-terminate.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 ccan/tal/str/test/run-fmt-terminate.c diff --git a/ccan/tal/str/str.c b/ccan/tal/str/str.c index 83dac663..7adb9ef5 100644 --- a/ccan/tal/str/str.c +++ b/ccan/tal/str/str.c @@ -52,7 +52,7 @@ char *tal_fmt(const tal_t *ctx, const char *fmt, ...) static bool do_vfmt(char **buf, size_t off, const char *fmt, va_list ap) { /* A decent guess to start. */ - size_t max = strlen(fmt) * 2; + size_t max = strlen(fmt) * 2 + 1; bool ok; for (;;) { diff --git a/ccan/tal/str/test/run-fmt-terminate.c b/ccan/tal/str/test/run-fmt-terminate.c new file mode 100644 index 00000000..9dfd0015 --- /dev/null +++ b/ccan/tal/str/test/run-fmt-terminate.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include +#include "helper.h" + +/* Empty format string: should still terminate! */ +int main(int argc, char *argv[]) +{ + char *str; + const char *fmt = ""; + + plan_tests(1); + /* GCC complains about empty format string, complains about non-literal + * with no args... */ + str = tal_fmt(NULL, fmt, ""); + ok1(!strcmp(str, "")); + tal_free(str); + + return exit_status(); +} -- 2.39.2