I am learning about interposition. The code below is taken from a course (CMU 15-213), however, when I compile, this warning "mymalloc.c:6:29: warning: all paths through this function will call itself [-Winfinite-recursion]" occurs.
As I understand, a call to malloc in main() will instead be transferred to the call to mymalloc(). Inside mymalloc(), malloc is called again and hence, the loop occurs.
//int.c
#include <stdio.h>
#include "malloc.h"
int main() {
int *p = malloc(32);
free(p);
return (0);
}
//malloc.h
#include <stdlib.h>
#define malloc(size) mymalloc(size)
#define free(ptr) myfree(ptr)
void *mymalloc(size_t size);
void myfree(void *ptr);
//mymalloc.c
#ifdef COMPILETIME
#include <stdio.h>
#include "malloc.h"
/* malloc wrapper function */
void *mymalloc(size_t size) {
void *ptr = malloc(size);
printf("malloc(%d)=%p\n",
(int)size, ptr);
return ptr;
}
/* free wrapper function */
void myfree(void *ptr)
{
free(ptr);
printf("free(%p)\n", ptr);
}
#endif
//Makefile
CC = gcc
CFLAGS = -Wall
intc: int.c mymalloc.c
$(CC) $(CFLAGS) -DCOMPILETIME -c mymalloc.c
$(CC) $(CFLAGS) -I. -o intc int.c mymalloc.o
runc:
./intc
clean:
rm -f *~ intr intl intc *.so *.o
I tried searching but couldn't find any solution to this problem. Can anyone please help ? Thank you.