Commit e4b6a262 authored by Rusty Russell's avatar Rusty Russell

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: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 97d99004
...@@ -20,6 +20,10 @@ ...@@ -20,6 +20,10 @@
* RFC822 compliant, don't SEGV and try to return as much useful * RFC822 compliant, don't SEGV and try to return as much useful
* data as possible. * 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: * Example:
* // Given '' outputs 'body' * // Given '' outputs 'body'
* // Given 'From' outputs ' <from@example.com>' * // Given 'From' outputs ' <from@example.com>'
...@@ -53,7 +57,11 @@ int main(int argc, char *argv[]) ...@@ -53,7 +57,11 @@ int main(int argc, char *argv[])
return 1; return 1;
if (strcmp(argv[1], "depends") == 0) { 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/list\n");
printf("ccan/str\n"); printf("ccan/str\n");
printf("ccan/bytestring\n"); printf("ccan/bytestring\n");
...@@ -64,6 +72,7 @@ int main(int argc, char *argv[]) ...@@ -64,6 +72,7 @@ int main(int argc, char *argv[])
printf("ccan/failtest\n"); printf("ccan/failtest\n");
printf("ccan/foreach\n"); printf("ccan/foreach\n");
printf("ccan/array_size\n"); printf("ccan/array_size\n");
printf("ccan/tal/str\n");
return 0; return 0;
} }
......
...@@ -5,11 +5,17 @@ ...@@ -5,11 +5,17 @@
#include <string.h> #include <string.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/talloc/talloc.h>
#include <ccan/list/list.h> #include <ccan/list/list.h>
#include <stdio.h>
#include <ccan/rfc822/rfc822.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 #if !HAVE_MEMMEM
void *memmem(const void *haystack, size_t haystacklen, void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen) const void *needle, size_t needlelen)
...@@ -92,6 +98,8 @@ struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg, ...@@ -92,6 +98,8 @@ struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
assert(msg); assert(msg);
if (!list_check(&msg->headers, abortstr)) if (!list_check(&msg->headers, abortstr))
return NULL; return NULL;
if (!tal_check(msg, abortstr))
return NULL;
return (struct rfc822_msg *)msg; return (struct rfc822_msg *)msg;
} }
...@@ -106,7 +114,7 @@ struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len) ...@@ -106,7 +114,7 @@ struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
struct rfc822_msg *msg; struct rfc822_msg *msg;
int i; int i;
msg = talloc(ctx, struct rfc822_msg); msg = tal(ctx, struct rfc822_msg);
ALLOC_CHECK(msg, NULL); ALLOC_CHECK(msg, NULL);
msg->data = p; msg->data = p;
...@@ -128,7 +136,7 @@ struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len) ...@@ -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) void rfc822_free(struct rfc822_msg *msg)
{ {
CHECK(msg, ">rfc822_free"); CHECK(msg, ">rfc822_free");
talloc_free(msg); tal_free(msg);
} }
static struct rfc822_header *next_header_cached(struct rfc822_msg *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) ...@@ -200,7 +208,7 @@ static struct rfc822_header *next_header_parse(struct rfc822_msg *msg)
msg->remainder = eh; msg->remainder = eh;
hi = talloc_zero(msg, struct rfc822_header); hi = talz(msg, struct rfc822_header);
ALLOC_CHECK(hi, NULL); ALLOC_CHECK(hi, NULL);
hi->all = bytestring(h, eh - h); hi->all = bytestring(h, eh - h);
...@@ -354,7 +362,7 @@ struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg, ...@@ -354,7 +362,7 @@ struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg,
if (lines <= 1) { if (lines <= 1) {
hdr->unfolded = bytestring(raw.ptr, len); hdr->unfolded = bytestring(raw.ptr, len);
} else { } else {
char *unfold = talloc_array(msg, char, len); char *unfold = tal_arr(msg, char, len);
char *p = unfold; char *p = unfold;
ALLOC_CHECK(unfold, bytestring_NULL); ALLOC_CHECK(unfold, bytestring_NULL);
...@@ -447,7 +455,7 @@ static struct rfc822_header *index_header(struct rfc822_msg *msg, ...@@ -447,7 +455,7 @@ static struct rfc822_header *index_header(struct rfc822_msg *msg,
if (!hn) { if (!hn) {
unsigned hash = headerhash(hname); 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); ALLOC_CHECK(hn, NULL);
hn->name = hname; hn->name = hname;
......
...@@ -22,6 +22,9 @@ struct rfc822_msg; ...@@ -22,6 +22,9 @@ struct rfc822_msg;
* callback is called with a string describing where the failure * callback is called with a string describing where the failure
* occurred, which can be used to log a more useful error message. * 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: * Example:
* static void my_handler(const char *str) * static void my_handler(const char *str)
* { * {
...@@ -53,15 +56,15 @@ static inline bool rfc822_iswsp(char c) ...@@ -53,15 +56,15 @@ static inline bool rfc822_iswsp(char c)
* inconsistent, and the function will abort. If the state of the * inconsistent, and the function will abort. If the state of the
* structure is valid it returns it unchanged. * structure is valid it returns it unchanged.
* *
* Returns the list head if the list is consistent, NULL if not (it * Returns the @msg if the message is consistent, NULL if not (it can
* can never return NULL if @abortstr is set). * never return NULL if @abortstr is set).
*/ */
struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg, struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
const char *abortstr); const char *abortstr);
/** /**
* rfc822_start - start parsing a new rfc822 message * 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 * @p: pointer to a buffer containing the message text
* @len: length of the message text * @len: length of the message text
* *
......
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <ccan/talloc/talloc.h>
#include <ccan/failtest/failtest_override.h> #include <ccan/failtest/failtest_override.h>
#include <ccan/failtest/failtest.h> #include <ccan/failtest/failtest.h>
...@@ -11,6 +10,7 @@ ...@@ -11,6 +10,7 @@
/* failtest limitations mean we need these wrappers to test talloc /* failtest limitations mean we need these wrappers to test talloc
* failure paths. */ * failure paths. */
#ifndef TAL_USE_TALLOC
static void *malloc_wrapper(size_t size) static void *malloc_wrapper(size_t size)
{ {
return malloc(size); return malloc(size);
...@@ -25,6 +25,7 @@ static void *realloc_wrapper(void *ptr, size_t size) ...@@ -25,6 +25,7 @@ static void *realloc_wrapper(void *ptr, size_t size)
{ {
return realloc(ptr, size); return realloc(ptr, size);
} }
#endif
#if 0 #if 0
static void allocation_failure_exit(const char *s) static void allocation_failure_exit(const char *s)
...@@ -50,11 +51,29 @@ void allocation_failure_check(void) ...@@ -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[]) void failtest_setup(int argc, char *argv[])
{ {
failtest_init(argc, argv); failtest_init(argc, argv);
rfc822_set_allocation_failure_handler(allocation_failure_continue); 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, void check_header(struct rfc822_msg *msg,
......
...@@ -10,7 +10,11 @@ ...@@ -10,7 +10,11 @@
#include <ccan/rfc822/rfc822.h> #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; static bool should_fail = false;
...@@ -18,16 +22,16 @@ static void *mayfail_alloc(const void *ctx, size_t size) ...@@ -18,16 +22,16 @@ static void *mayfail_alloc(const void *ctx, size_t size)
{ {
if (should_fail) if (should_fail)
return NULL; return NULL;
return talloc_zero_size(ctx, size); return tal_arrz(ctx, char, size);
} }
/* Override various tallocation functions. */ /* Override various tallocation functions. */
#undef talloc #undef tal
#undef talloc_zero #undef talz
#undef talloc_array #undef tal_arr
#define talloc(ctx, type) mayfail_alloc((ctx), sizeof(type)) #define tal(ctx, type) mayfail_alloc((ctx), sizeof(type))
#define talloc_zero(ctx, type) mayfail_alloc((ctx), sizeof(type)) #define talz(ctx, type) mayfail_alloc((ctx), sizeof(type))
#define talloc_array(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num)) #define tal_arr(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num))
#include <ccan/rfc822/rfc822.c> #include <ccan/rfc822/rfc822.c>
......
...@@ -137,7 +137,7 @@ int main(int argc, char *argv[]) ...@@ -137,7 +137,7 @@ int main(int argc, char *argv[])
test_hdrbody(e, buf, len, exname, crlf); test_hdrbody(e, buf, len, exname, crlf);
test_hdrhdr(e, buf, len, exname, crlf); test_hdrhdr(e, buf, len, exname, crlf);
talloc_free(buf); tal_free(buf);
} }
} }
......
...@@ -75,7 +75,7 @@ int main(int argc, char *argv[]) ...@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
test_hdrbyname(e, buf, len, exname, crlf); test_hdrbyname(e, buf, len, exname, crlf);
talloc_free(buf); tal_free(buf);
} }
} }
......
...@@ -39,7 +39,7 @@ static void test_assemble(const struct aexample *e, int crlf, ...@@ -39,7 +39,7 @@ static void test_assemble(const struct aexample *e, int crlf,
len, cmplen); len, cmplen);
ok1(len == cmplen); ok1(len == cmplen);
ok1(memcmp(msg, cmp, cmplen) == 0); ok1(memcmp(msg, cmp, cmplen) == 0);
talloc_free(msg); tal_free(msg);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
......
...@@ -31,7 +31,7 @@ static struct bytestring fold_and_assemble(int foldat, int crlf, int truncated) ...@@ -31,7 +31,7 @@ static struct bytestring fold_and_assemble(int foldat, int crlf, int truncated)
char *buf, *p; char *buf, *p;
int i, n = 0; 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) if (!buf)
exit(0); exit(0);
...@@ -111,7 +111,7 @@ int main(int argc, char *argv[]) ...@@ -111,7 +111,7 @@ int main(int argc, char *argv[])
for (i = -1; i <= FOLD_POINTS; i++) { for (i = -1; i <= FOLD_POINTS; i++) {
msgbuf = fold_and_assemble(i, crlf, truncated); msgbuf = fold_and_assemble(i, crlf, truncated);
check_folded_header(msgbuf.ptr, msgbuf.len); check_folded_header(msgbuf.ptr, msgbuf.len);
talloc_free(msgbuf.ptr); tal_free(msgbuf.ptr);
} }
} }
} }
......
#ifndef RFC822_TESTDATA_H #ifndef RFC822_TESTDATA_H
#define 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/array_size/array_size.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
...@@ -127,30 +127,26 @@ static inline const char *assemble_msg(const struct aexample *e, ...@@ -127,30 +127,26 @@ static inline const char *assemble_msg(const struct aexample *e,
{ {
const char *nl = crlf ? "\r\n" : "\n"; const char *nl = crlf ? "\r\n" : "\n";
int nln = crlf ? 2 : 1; int nln = crlf ? 2 : 1;
char *msg, *amsg; char *msg;
size_t n = 0; size_t n = 0;
int i; int i;
msg = talloc_strdup(NULL, ""); msg = tal_strdup(NULL, "");
if (!msg) if (!msg)
return NULL; return NULL;
for (i = 0; i < e->nhdrs; i++) { for (i = 0; i < e->nhdrs; i++) {
amsg = talloc_asprintf_append(msg, "%s:%s%s", e->hdrs[i].name, if (!tal_append_fmt(&msg, "%s:%s%s", e->hdrs[i].name,
e->hdrs[i].val, nl); e->hdrs[i].val, nl)) {
if (!amsg) { tal_free(msg);
talloc_free(msg);
return NULL; return NULL;
} }
msg = amsg;
n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln; n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
} }
amsg = talloc_asprintf_append(msg, "%s%s", nl, e->body); if (!tal_append_fmt(&msg, "%s%s", nl, e->body)) {
if (!amsg) { tal_free(msg);
talloc_free(msg);
return NULL; return NULL;
} }
msg = amsg;
n += strlen(e->body) + nln; n += strlen(e->body) + nln;
*len = n; *len = n;
return msg; return msg;
......
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