]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/test/run-default-alloc-failure.c
c208154281106b18a2371dfcabd62bd49a21d384
[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 #include <ccan/talloc/talloc.h>
14
15 static bool should_fail = false;
16
17 static void *mayfail_alloc(const void *ctx, size_t size)
18 {
19         if (should_fail)
20                 return NULL;
21         return talloc_zero_size(ctx, size);
22 }
23
24 /* Override various tallocation functions. */
25 #undef talloc
26 #undef talloc_zero
27 #undef talloc_array
28 #define talloc(ctx, type) mayfail_alloc((ctx), sizeof(type))
29 #define talloc_zero(ctx, type) mayfail_alloc((ctx), sizeof(type))
30 #define talloc_array(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num))
31
32 #include <ccan/rfc822/rfc822.c>
33
34 #include "testdata.h"
35
36 static void abort_handler(int signum)
37 {
38         printf("Aborted");
39         exit(0);
40 }
41
42 int main(int argc, char *argv[])
43 {
44         const char *buf;
45         size_t len;
46         struct rfc822_msg *msg;
47         struct sigaction sa = {
48                 .sa_handler = abort_handler,
49         };
50         int ret;
51
52         ret = sigaction(SIGABRT, &sa, NULL);
53         assert(ret == 0);
54
55         buf = assemble_msg(&test_msg_1, &len, 0);
56
57         msg = rfc822_start(NULL, buf, len);
58         should_fail = true;
59         (void) rfc822_next_header(msg, NULL);
60
61         /* We should never get here! */
62         abort();
63 }