From 3ec87c7a8cfea45b398280e3117f9c4f1f7f5784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 29 Dec 2014 23:49:29 +0000 Subject: [PATCH] Introduce t_malloc_fail_after: if non-zero, it serves as a countdown to setting t_malloc_fail. Also, enforce t_malloc_fail in realloc(), but not before checking that the argument was valid. --- t/t_malloc.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/t/t_malloc.c b/t/t_malloc.c index cf8d166..d81c771 100644 --- a/t/t_malloc.c +++ b/t/t_malloc.c @@ -131,6 +131,9 @@ unsigned long nmapalloc, nmapfree; /* if non-zero, all allocations fail */ int t_malloc_fail; +/* if non-zero, all allocations will fail after a countdown */ +int t_malloc_fail_after; + /* if non-zero, unintentional allocation failures are fatal */ int t_malloc_fatal; @@ -298,6 +301,8 @@ malloc(size_t size) if (t_malloc_fail) { errno = ENOMEM; return (NULL); + } else if (t_malloc_fail_after > 0 && --t_malloc_fail_after == 0) { + t_malloc_fail = 1; } p = t_malloc(size); if (p == NULL && t_malloc_fatal) @@ -320,6 +325,8 @@ calloc(size_t n, size_t size) if (t_malloc_fail) { errno = ENOMEM; return (NULL); + } else if (t_malloc_fail_after > 0 && --t_malloc_fail_after == 0) { + t_malloc_fail = 1; } p = t_malloc(n * size); if (p == NULL && t_malloc_fatal) @@ -374,6 +381,12 @@ realloc(void *o, size_t size) abort(); found: + if (t_malloc_fail) { + errno = ENOMEM; + return (NULL); + } else if (t_malloc_fail_after > 0 && --t_malloc_fail_after == 0) { + t_malloc_fail = 1; + } if ((p = t_malloc(size)) == NULL) { if (t_malloc_fatal) abort();