mirror of
https://github.com/cryb-to/cryb-to.git
synced 2024-11-14 01:35:40 +00:00
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.
This commit is contained in:
parent
bc8e9d67ba
commit
3ec87c7a8c
1 changed files with 13 additions and 0 deletions
13
t/t_malloc.c
13
t/t_malloc.c
|
@ -131,6 +131,9 @@ unsigned long nmapalloc, nmapfree;
|
||||||
/* if non-zero, all allocations fail */
|
/* if non-zero, all allocations fail */
|
||||||
int t_malloc_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 */
|
/* if non-zero, unintentional allocation failures are fatal */
|
||||||
int t_malloc_fatal;
|
int t_malloc_fatal;
|
||||||
|
|
||||||
|
@ -298,6 +301,8 @@ malloc(size_t size)
|
||||||
if (t_malloc_fail) {
|
if (t_malloc_fail) {
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
} else if (t_malloc_fail_after > 0 && --t_malloc_fail_after == 0) {
|
||||||
|
t_malloc_fail = 1;
|
||||||
}
|
}
|
||||||
p = t_malloc(size);
|
p = t_malloc(size);
|
||||||
if (p == NULL && t_malloc_fatal)
|
if (p == NULL && t_malloc_fatal)
|
||||||
|
@ -320,6 +325,8 @@ calloc(size_t n, size_t size)
|
||||||
if (t_malloc_fail) {
|
if (t_malloc_fail) {
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
} else if (t_malloc_fail_after > 0 && --t_malloc_fail_after == 0) {
|
||||||
|
t_malloc_fail = 1;
|
||||||
}
|
}
|
||||||
p = t_malloc(n * size);
|
p = t_malloc(n * size);
|
||||||
if (p == NULL && t_malloc_fatal)
|
if (p == NULL && t_malloc_fatal)
|
||||||
|
@ -374,6 +381,12 @@ realloc(void *o, size_t size)
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
found:
|
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 ((p = t_malloc(size)) == NULL) {
|
||||||
if (t_malloc_fatal)
|
if (t_malloc_fatal)
|
||||||
abort();
|
abort();
|
||||||
|
|
Loading…
Reference in a new issue