swrap: Make sure cmbuf is not NULL.
[Samba.git] / lib / ccan / failtest / test / run-malloc.c
blob96641b85250bd97aaa012f54edb1d945742eb3b9
1 #include "config.h"
2 #include <stdlib.h>
3 #include <setjmp.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <assert.h>
7 #include <ccan/tap/tap.h>
9 /* We don't actually want it to exit... */
10 static jmp_buf exited;
11 #define exit(status) longjmp(exited, (status) + 1)
13 #define printf saved_printf
14 static int saved_printf(const char *fmt, ...);
16 #define fprintf saved_fprintf
17 static int saved_fprintf(FILE *ignored, const char *fmt, ...);
19 #define vfprintf saved_vfprintf
20 static int saved_vfprintf(FILE *ignored, const char *fmt, va_list ap);
22 /* Hack to avoid a memory leak which valgrind complains about. */
23 #define realloc set_realloc
24 static void *set_realloc(void *ptr, size_t size);
26 #define free set_free
27 static void set_free(void *ptr);
29 /* Include the C files directly. */
30 #include <ccan/failtest/failtest.c>
32 #undef realloc
33 #undef free
35 static char *buffer;
36 static void *set_realloc(void *ptr, size_t size)
38 return buffer = realloc(ptr, size);
41 static void set_free(void *ptr)
43 if (ptr == buffer)
44 buffer = NULL;
45 free(ptr);
48 static char *output = NULL;
50 static int saved_vprintf(const char *fmt, va_list ap)
52 int ret;
53 int len = 0;
54 va_list ap2;
56 va_copy(ap2, ap);
57 ret = vsnprintf(NULL, 0, fmt, ap2);
58 va_end(ap2);
60 if (output)
61 len = strlen(output);
63 output = realloc(output, len + ret + 1);
64 return vsprintf(output + len, fmt, ap);
67 static int saved_vfprintf(FILE *ignored, const char *fmt, va_list ap)
69 return saved_vprintf(fmt, ap);
72 static int saved_printf(const char *fmt, ...)
74 va_list ap;
75 int ret;
77 va_start(ap, fmt);
78 ret = saved_vprintf(fmt, ap);
79 va_end(ap);
80 return ret;
83 static int saved_fprintf(FILE *ignored, const char *fmt, ...)
85 va_list ap;
86 int ret;
88 va_start(ap, fmt);
89 ret = saved_vprintf(fmt, ap);
90 va_end(ap);
91 return ret;
94 int main(void)
96 int status;
98 plan_tests(3);
99 failtest_init(0, NULL);
101 status = setjmp(exited);
102 if (status == 0) {
103 char *p = failtest_malloc(1, "run-malloc.c", 1);
104 /* If we just segv, valgrind counts that as a failure.
105 * So kill ourselves creatively. */
106 if (!p)
107 kill(getpid(), SIGSEGV);
108 fail("Expected child to crash!");
109 } else {
110 ok1(status == 2);
111 ok1(strstr(output, "Killed by signal"));
112 ok1(strstr(output, "--failpath=M\n"));
114 free(buffer);
115 return exit_status();