Browse Source

tal/str: fix infinite loop of tal_fmt() with empty string.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 10 years ago
parent
commit
64e9e7145a
2 changed files with 23 additions and 1 deletions
  1. 1 1
      ccan/tal/str/str.c
  2. 22 0
      ccan/tal/str/test/run-fmt-terminate.c

+ 1 - 1
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 (;;) {

+ 22 - 0
ccan/tal/str/test/run-fmt-terminate.c

@@ -0,0 +1,22 @@
+#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"
+
+/* 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();
+}