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>
* RFC822 compliant, don't SEGV and try to return as much useful
* data as possible.
*
* 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>'
* Example:
* // Given '' outputs 'body'
* // Given 'From' outputs ' <from@example.com>'
return 1;
if (strcmp(argv[1], "depends") == 0) {
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");
printf("ccan/list\n");
printf("ccan/str\n");
printf("ccan/bytestring\n");
printf("ccan/failtest\n");
printf("ccan/foreach\n");
printf("ccan/array_size\n");
printf("ccan/failtest\n");
printf("ccan/foreach\n");
printf("ccan/array_size\n");
+ printf("ccan/tal/str\n");
#include <string.h>
#include <ccan/str/str.h>
#include <string.h>
#include <ccan/str/str.h>
-#include <ccan/talloc/talloc.h>
#include <ccan/list/list.h>
#include <ccan/list/list.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
void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen)
#if !HAVE_MEMMEM
void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen)
assert(msg);
if (!list_check(&msg->headers, abortstr))
return NULL;
assert(msg);
if (!list_check(&msg->headers, abortstr))
return NULL;
+ if (!tal_check(msg, abortstr))
+ return NULL;
return (struct rfc822_msg *)msg;
}
return (struct rfc822_msg *)msg;
}
struct rfc822_msg *msg;
int i;
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;
ALLOC_CHECK(msg, NULL);
msg->data = p;
void rfc822_free(struct rfc822_msg *msg)
{
CHECK(msg, ">rfc822_free");
void rfc822_free(struct rfc822_msg *msg)
{
CHECK(msg, ">rfc822_free");
}
static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
}
static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
- hi = talloc_zero(msg, struct rfc822_header);
+ hi = talz(msg, struct rfc822_header);
ALLOC_CHECK(hi, NULL);
hi->all = bytestring(h, eh - h);
ALLOC_CHECK(hi, NULL);
hi->all = bytestring(h, eh - h);
if (lines <= 1) {
hdr->unfolded = bytestring(raw.ptr, len);
} else {
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);
char *p = unfold;
ALLOC_CHECK(unfold, bytestring_NULL);
if (!hn) {
unsigned hash = headerhash(hname);
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;
ALLOC_CHECK(hn, NULL);
hn->name = hname;
* callback is called with a string describing where the failure
* occurred, which can be used to log a more useful error message.
*
* 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)
* {
* Example:
* static void my_handler(const char *str)
* {
* inconsistent, and the function will abort. If the state of the
* structure is valid it returns it unchanged.
*
* 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
*/
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
*
* @p: pointer to a buffer containing the message text
* @len: length of the message text
*
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
-#include <ccan/talloc/talloc.h>
#include <ccan/failtest/failtest_override.h>
#include <ccan/failtest/failtest.h>
#include <ccan/failtest/failtest_override.h>
#include <ccan/failtest/failtest.h>
/* failtest limitations mean we need these wrappers to test talloc
* failure paths. */
/* failtest limitations mean we need these wrappers to test talloc
* failure paths. */
static void *malloc_wrapper(size_t size)
{
return malloc(size);
static void *malloc_wrapper(size_t size)
{
return malloc(size);
{
return realloc(ptr, size);
}
{
return realloc(ptr, size);
}
#if 0
static void allocation_failure_exit(const char *s)
#if 0
static void allocation_failure_exit(const char *s)
+#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);
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,
}
void check_header(struct rfc822_msg *msg,
#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;
{
if (should_fail)
return NULL;
{
if (should_fail)
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 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>
#include <ccan/rfc822/rfc822.c>
test_hdrbody(e, buf, len, exname, crlf);
test_hdrhdr(e, buf, len, exname, crlf);
test_hdrbody(e, buf, len, exname, crlf);
test_hdrhdr(e, buf, len, exname, crlf);
test_hdrbyname(e, buf, len, exname, crlf);
test_hdrbyname(e, buf, len, exname, crlf);
len, cmplen);
ok1(len == cmplen);
ok1(memcmp(msg, cmp, cmplen) == 0);
len, cmplen);
ok1(len == cmplen);
ok1(memcmp(msg, cmp, cmplen) == 0);
}
int main(int argc, char *argv[])
}
int main(int argc, char *argv[])
char *buf, *p;
int i, n = 0;
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);
for (i = -1; i <= FOLD_POINTS; i++) {
msgbuf = fold_and_assemble(i, crlf, truncated);
check_folded_header(msgbuf.ptr, msgbuf.len);
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);
#ifndef RFC822_TESTDATA_H
#define RFC822_TESTDATA_H
#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>
#include <ccan/array_size/array_size.h>
#include <ccan/foreach/foreach.h>
{
const char *nl = crlf ? "\r\n" : "\n";
int nln = crlf ? 2 : 1;
{
const char *nl = crlf ? "\r\n" : "\n";
int nln = crlf ? 2 : 1;
- msg = talloc_strdup(NULL, "");
+ msg = tal_strdup(NULL, "");
if (!msg)
return NULL;
for (i = 0; i < e->nhdrs; i++) {
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);
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 (!amsg) {
- talloc_free(msg);
+ if (!tal_append_fmt(&msg, "%s%s", nl, e->body)) {
+ tal_free(msg);
n += strlen(e->body) + nln;
*len = n;
return msg;
n += strlen(e->body) + nln;
*len = n;
return msg;