Browse Source

rfc822: switch to ccan/tal.

We use TAL_USE_TALLOC to use libtalloc as the backend: you can test that with
ccanlint --compiler="cc -DTAL_USE_TALLOC".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 13 years ago
parent
commit
e4b6a26282

+ 10 - 1
ccan/rfc822/_info

@@ -20,6 +20,10 @@
  *   RFC822 compliant, don't SEGV and try to return as much useful
  *   data as possible.
  *
+ * Define TAL_USE_TALLOC to use libtalloc as the allocator, otherwise
+ * it will use ccan/tal (usually done on the cmdline, as tal/str will need
+ * it too).
+ *
  * Example:
  *	// Given '' outputs 'body'
  *	// Given 'From' outputs ' <from@example.com>'
@@ -53,7 +57,11 @@ int main(int argc, char *argv[])
 		return 1;
 
 	if (strcmp(argv[1], "depends") == 0) {
-		printf("ccan/talloc\n");
+#ifdef TAL_USE_TALLOC
+		printf("ccan/tal/talloc\n");
+#else
+		printf("ccan/tal\n");
+#endif
 		printf("ccan/list\n");
 		printf("ccan/str\n");
 		printf("ccan/bytestring\n");
@@ -64,6 +72,7 @@ int main(int argc, char *argv[])
 		printf("ccan/failtest\n");
 		printf("ccan/foreach\n");
 		printf("ccan/array_size\n");
+		printf("ccan/tal/str\n");
 		return 0;
 	}
 

+ 14 - 6
ccan/rfc822/rfc822.c

@@ -5,11 +5,17 @@
 #include <string.h>
 
 #include <ccan/str/str.h>
-#include <ccan/talloc/talloc.h>
 #include <ccan/list/list.h>
+#include <stdio.h>
 
 #include <ccan/rfc822/rfc822.h>
 
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
+#include <ccan/tal/tal.h>
+#endif
+
 #if !HAVE_MEMMEM
 void *memmem(const void *haystack, size_t haystacklen,
 	     const void *needle, size_t needlelen)
@@ -92,6 +98,8 @@ struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
 	assert(msg);
 	if (!list_check(&msg->headers, abortstr))
                 return NULL;
+	if (!tal_check(msg, abortstr))
+		return NULL;
         return (struct rfc822_msg *)msg;
 }
 
@@ -106,7 +114,7 @@ struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
 	struct rfc822_msg *msg;
 	int i;
 
-	msg = talloc(ctx, struct rfc822_msg);
+	msg = tal(ctx, struct rfc822_msg);
 	ALLOC_CHECK(msg, NULL);
 
 	msg->data = p;
@@ -128,7 +136,7 @@ struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
 void rfc822_free(struct rfc822_msg *msg)
 {
 	CHECK(msg, ">rfc822_free");
-	talloc_free(msg);
+	tal_free(msg);
 }
 
 static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
@@ -200,7 +208,7 @@ static struct rfc822_header *next_header_parse(struct rfc822_msg *msg)
 		msg->remainder = eh;
 
 
-	hi = talloc_zero(msg, struct rfc822_header);
+	hi = talz(msg, struct rfc822_header);
 	ALLOC_CHECK(hi, NULL);
 
 	hi->all = bytestring(h, eh - h);
@@ -354,7 +362,7 @@ struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg,
 		if (lines <= 1) {
 			hdr->unfolded = bytestring(raw.ptr, len);
 		} else {
-			char *unfold = talloc_array(msg, char, len);
+			char *unfold = tal_arr(msg, char, len);
 			char *p = unfold;
 
 			ALLOC_CHECK(unfold, bytestring_NULL);
@@ -447,7 +455,7 @@ static struct rfc822_header *index_header(struct rfc822_msg *msg,
 	if (!hn) {
 		unsigned hash = headerhash(hname);
 
-		hn = talloc_zero(msg, struct rfc822_headers_of_name);
+		hn = talz(msg, struct rfc822_headers_of_name);
 		ALLOC_CHECK(hn, NULL);
 
 		hn->name = hname;

+ 6 - 3
ccan/rfc822/rfc822.h

@@ -22,6 +22,9 @@ struct rfc822_msg;
  * callback is called with a string describing where the failure
  * occurred, which can be used to log a more useful error message.
  *
+ * Note that tal also has a default function which calls abort() on allocation
+ * failure: see tal_set_backend().
+ *
  * Example:
  *	static void my_handler(const char *str)
  *	{
@@ -53,15 +56,15 @@ static inline bool rfc822_iswsp(char c)
  * inconsistent, and the function will abort.  If the state of the
  * structure is valid it returns it unchanged.
  *
- * Returns the list head if the list is consistent, NULL if not (it
- * can never return NULL if @abortstr is set).
+ * Returns the @msg if the message is consistent, NULL if not (it can
+ * never return NULL if @abortstr is set).
  */
 struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
 				const char *abortstr);
 
 /**
  * rfc822_start - start parsing a new rfc822 message
- * @ctx: talloc context to make allocations in
+ * @ctx: tal context to make allocations in (or talloc #ifdef TAL_USE_TALLOC)
  * @p: pointer to a buffer containing the message text
  * @len: length of the message text
  *

+ 21 - 2
ccan/rfc822/test/helper.c

@@ -1,7 +1,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#include <ccan/talloc/talloc.h>
 #include <ccan/failtest/failtest_override.h>
 #include <ccan/failtest/failtest.h>
 
@@ -11,6 +10,7 @@
 
 /* failtest limitations mean we need these wrappers to test talloc
  * failure paths. */
+#ifndef TAL_USE_TALLOC
 static void *malloc_wrapper(size_t size)
 {
 	return malloc(size);
@@ -25,6 +25,7 @@ static void *realloc_wrapper(void *ptr, size_t size)
 {
 	return realloc(ptr, size);
 }
+#endif
 
 #if 0
 static void allocation_failure_exit(const char *s)
@@ -50,11 +51,29 @@ void allocation_failure_check(void)
 	}
 }
 
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
+#include <ccan/tal/tal.h>
+#endif
+
+/* Don't abort on allocation failures! */
+static void noabort_wrapper(const char *why)
+{
+	return;
+}
+
 void failtest_setup(int argc, char *argv[])
 {
 	failtest_init(argc, argv);
 	rfc822_set_allocation_failure_handler(allocation_failure_continue);
-	talloc_set_allocator(malloc_wrapper, free_wrapper, realloc_wrapper);
+#ifdef TAL_USE_TALLOC
+	/* FIXME: we can't inject allocation failures in talloc! */
+	tal_set_backend(NULL, NULL, NULL, noabort_wrapper);
+#else
+	tal_set_backend(malloc_wrapper, realloc_wrapper, free_wrapper,
+			noabort_wrapper);
+#endif
 }
 
 void check_header(struct rfc822_msg *msg,

+ 12 - 8
ccan/rfc822/test/run-default-alloc-failure.c

@@ -10,7 +10,11 @@
 
 #include <ccan/rfc822/rfc822.h>
 
-#include <ccan/talloc/talloc.h>
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
+#include <ccan/tal/tal.h>
+#endif
 
 static bool should_fail = false;
 
@@ -18,16 +22,16 @@ static void *mayfail_alloc(const void *ctx, size_t size)
 {
 	if (should_fail)
 		return NULL;
-	return talloc_zero_size(ctx, size);
+	return tal_arrz(ctx, char, size);
 }
 
 /* Override various tallocation functions. */
-#undef talloc
-#undef talloc_zero
-#undef talloc_array
-#define talloc(ctx, type) mayfail_alloc((ctx), sizeof(type))
-#define talloc_zero(ctx, type) mayfail_alloc((ctx), sizeof(type))
-#define talloc_array(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num))
+#undef tal
+#undef talz
+#undef tal_arr
+#define tal(ctx, type) mayfail_alloc((ctx), sizeof(type))
+#define talz(ctx, type) mayfail_alloc((ctx), sizeof(type))
+#define tal_arr(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num))
 
 #include <ccan/rfc822/rfc822.c>
 

+ 1 - 1
ccan/rfc822/test/run-hdr-and-body.c

@@ -137,7 +137,7 @@ int main(int argc, char *argv[])
 			test_hdrbody(e, buf, len, exname, crlf);
 			test_hdrhdr(e, buf, len, exname, crlf);
 
-			talloc_free(buf);
+			tal_free(buf);
 		}
 	}
 

+ 1 - 1
ccan/rfc822/test/run-hdr-of-name.c

@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
 
 			test_hdrbyname(e, buf, len, exname, crlf);
 
-			talloc_free(buf);
+			tal_free(buf);
 		}
 	}
 

+ 1 - 1
ccan/rfc822/test/run-testdata.c

@@ -39,7 +39,7 @@ static void test_assemble(const struct aexample *e, int crlf,
 		len, cmplen);
 	ok1(len == cmplen);
 	ok1(memcmp(msg, cmp, cmplen) == 0);
-	talloc_free(msg);
+	tal_free(msg);
 }
 
 int main(int argc, char *argv[])

+ 2 - 2
ccan/rfc822/test/run-unfold.c

@@ -31,7 +31,7 @@ static struct bytestring fold_and_assemble(int foldat, int crlf, int truncated)
 	char *buf, *p;
 	int i, n = 0;
 
-	buf = talloc_array(NULL, char, strlen(BEFORE) + strlen(AFTER) + 3*strlen(UNFOLDED) + 2);
+	buf = tal_arr(NULL, char, strlen(BEFORE) + strlen(AFTER) + 3*strlen(UNFOLDED) + 2);
 	if (!buf)
 		exit(0);
 
@@ -111,7 +111,7 @@ int main(int argc, char *argv[])
 			for (i = -1; i <= FOLD_POINTS; i++) {
 				msgbuf = fold_and_assemble(i, crlf, truncated);
 				check_folded_header(msgbuf.ptr, msgbuf.len);
-				talloc_free(msgbuf.ptr);
+				tal_free(msgbuf.ptr);
 			}
 		}
 	}

+ 8 - 12
ccan/rfc822/test/testdata.h

@@ -1,7 +1,7 @@
 #ifndef RFC822_TESTDATA_H
 #define RFC822_TESTDATA_H
 
-#include <ccan/talloc/talloc.h>
+#include <ccan/tal/str/str.h>
 #include <ccan/array_size/array_size.h>
 #include <ccan/foreach/foreach.h>
 
@@ -127,30 +127,26 @@ static inline const char *assemble_msg(const struct aexample *e,
 {
 	const char *nl = crlf ? "\r\n" : "\n";
 	int nln = crlf ? 2 : 1;
-	char *msg, *amsg;
+	char *msg;
 	size_t n = 0;
 	int i;
 
-	msg = talloc_strdup(NULL, "");
+	msg = tal_strdup(NULL, "");
 	if (!msg)
 		return NULL;
 
 	for (i = 0; i < e->nhdrs; i++) {
-		amsg = talloc_asprintf_append(msg, "%s:%s%s", e->hdrs[i].name,
-					      e->hdrs[i].val, nl);
-		if (!amsg) {
-			talloc_free(msg);
+		if (!tal_append_fmt(&msg, "%s:%s%s", e->hdrs[i].name,
+				    e->hdrs[i].val, nl)) {
+			tal_free(msg);
 			return NULL;
 		}
-		msg = amsg;
 		n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
 	}
-	amsg = talloc_asprintf_append(msg, "%s%s", nl, e->body);
-	if (!amsg) {
-		talloc_free(msg);
+	if (!tal_append_fmt(&msg, "%s%s", nl, e->body)) {
+		tal_free(msg);
 		return NULL;
 	}
-	msg = amsg;
 	n += strlen(e->body) + nln;
 	*len = n;
 	return msg;