]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/test/run-default-alloc-failure.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / rfc822 / test / run-default-alloc-failure.c
1 #include "config.h"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5
6 #include <signal.h>
7 #include <errno.h>
8
9 #define CCAN_RFC822_DEBUG
10
11 #include <ccan/rfc822/rfc822.h>
12
13 #ifdef TAL_USE_TALLOC
14 #include <ccan/tal/talloc/talloc.h>
15 #else
16 #include <ccan/tal/tal.h>
17 #endif
18
19 static bool should_fail = false;
20
21 static void *mayfail_alloc(const void *ctx, size_t size)
22 {
23         if (should_fail)
24                 return NULL;
25         return tal_arrz(ctx, char, size);
26 }
27
28 /* Override various tallocation functions. */
29 #undef tal
30 #undef talz
31 #undef tal_arr
32 #define tal(ctx, type) mayfail_alloc((ctx), sizeof(type))
33 #define talz(ctx, type) mayfail_alloc((ctx), sizeof(type))
34 #define tal_arr(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num))
35
36 #include <ccan/rfc822/rfc822.c>
37
38 #include "testdata.h"
39
40 static void abort_handler(int signum)
41 {
42         printf("Aborted");
43         exit(0);
44 }
45
46 int main(int argc, char *argv[])
47 {
48         const char *buf;
49         size_t len;
50         struct rfc822_msg *msg;
51         struct sigaction sa = {
52                 .sa_handler = abort_handler,
53         };
54         int ret;
55
56         ret = sigaction(SIGABRT, &sa, NULL);
57         assert(ret == 0);
58
59         buf = assemble_msg(&test_msg_1, &len, 0);
60
61         msg = rfc822_start(NULL, buf, len);
62         should_fail = true;
63         (void) rfc822_next_header(msg, NULL);
64
65         /* We should never get here! */
66         abort();
67 }