1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#define _GNU_SOURCE
#include <stdio.h>
#include <math.h>
#include <dlfcn.h>
#include "tools.h"
static int n_fails = 0;
static int fail_after = 0;
void*
malloc(size_t s)
{
if (fail_after > 0) {
++n_fails;
if (n_fails >= fail_after) return NULL;
}
void *(*real_malloc)(size_t) = dlsym(RTLD_NEXT, "malloc");
return real_malloc(s);
}
void*
realloc(void * ptr, size_t s)
{
if (fail_after > 0) {
++n_fails;
if (n_fails >= fail_after)
return NULL;
}
void *(*real_realloc)(void *, size_t) = dlsym(RTLD_NEXT, "realloc");
return real_realloc(ptr, s);
}
void*
strdup(const char *s)
{
if (fail_after > 0) {
++n_fails;
if (n_fails >= fail_after) {
return NULL;
}
}
void *(*real_strdup)(const char*) = dlsym(RTLD_NEXT, "strdup");
return real_strdup(s);
}
void
set_fail_after(int limit) {
if (limit < 0) limit = 0;
fail_after = limit;
n_fails = 0;
}
int float_eq(double a, double b, double tolerance)
{
if (fabs(a-b) < tolerance) return 1;
printf("floats not equal: a = %f, b = %f, diff = %f, tolerance = %f\n",
a, b, fabs(a-b), tolerance);
fflush(stdout);
return 0;
}
|