This commit is contained in:
whaffman 2025-01-29 17:14:26 +01:00
parent 0caac11ca8
commit 0b47c2e54b
6 changed files with 37 additions and 35 deletions

View File

@ -33,7 +33,7 @@ LDLIBS = -fPIC -shared -ldl
all: $(NAME)
$(NAME): $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME)
$(CC) $(CFLAGS) $(OBJECTS) -o $(NAME) $(LDLIBS)
-include ${DEPENDS}
@ -41,7 +41,7 @@ $(OBJ_PATH):
mkdir -p $@
$(OBJ_PATH)/%.o: %.c | $(OBJ_PATH)
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ -fPIC
clean:
$(RM) $(OBJECTS) $(OBJ_PATH)

10
inc/funfail.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef FUNFAIL_H
#define FUNFAIL_H
# define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#endif

View File

@ -1,6 +1,4 @@
#include <dlfcn.h>
#include <errno.h>
#include <stdlib.h>
#include "funfail.h"
typedef void *(*f)(size_t __nmemb, size_t __size);
@ -22,4 +20,4 @@ void *calloc(size_t __nmemb, size_t __size)
}
errno = ENOMEM;
return NULL;
}
}

View File

@ -1,23 +1,23 @@
#include <dlfcn.h>
#include <errno.h>
#include <stdlib.h>
#include "funfail.h"
typedef void *(*f)(size_t size);
typedef void* (*f)(size_t size);
void *malloc(size_t size)
{
static f og_malloc = NULL;
static int calls = 0;
const int MAX_CALLS = getenv("MAX_MALLOC_CALLS") ? atoi(getenv("MAX_MALLOC_CALLS")) : -1;
void* malloc(size_t size) {
static f og_malloc = NULL;
static int calls = 0;
const int MAX_CALLS = getenv("MAX_MALLOC_CALLS") ? atoi(getenv("MAX_MALLOC_CALLS")) : -1;
if (!og_malloc)
{
og_malloc = (f)dlsym(RTLD_NEXT, "malloc");
}
if (!og_malloc) {
og_malloc = (f)dlsym(RTLD_NEXT, "malloc");
}
calls++;
if (MAX_CALLS < 0 || calls <= MAX_CALLS) {
return og_malloc(size);
}
errno = ENOMEM;
return NULL;
}
calls++;
if (MAX_CALLS < 0 || calls <= MAX_CALLS)
{
return og_malloc(size);
}
errno = ENOMEM;
return NULL;
}

View File

@ -1,7 +1,4 @@
#include <dlfcn.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include "funfail.h"
typedef ssize_t (*f)(int fd, void *buf, size_t count);
@ -23,4 +20,4 @@ ssize_t read(int fd, void *buf, size_t count)
}
errno = EBADF;
return -1;
}
}

View File

@ -1,7 +1,4 @@
#include <dlfcn.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include "funfail.h"
typedef ssize_t (*f)(int fd, const void *buf, size_t count);
@ -23,4 +20,4 @@ ssize_t write(int fd, const void *buf, size_t count)
}
errno = EBADF;
return -1;
}
}