I a writing a custom wrapper that implements strcat, strcpy etc in a safer way by trying to remember heap size allocations to adresses. For example, if a malloc(100) returned a pointer p, strcat checks current size of string at p and converts safely to strncat if it remembers the p from malloc. Here is my implementation:
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include "linkedlist.h"
List * ptrSizes;
char *(*libc_strcat)(char * , const char * );
char* strcat(char * dest, const char * src){
printf("Called strcat\n");
if (ptrSizes == NULL) {
ptrSizes = createList();
}
if (libc_strcat == NULL) {
libc_strcat = dlsym(RTLD_NEXT, "strcat");
}
// Assume node returns the saved mapping correctly
Node* node = findInList(ptrSizes, dest);
if (node != NULL){
size_t curr_sz = strlen(dest);
size_t left = node -> max_sz - curr_sz - 1;
if (left > 0) {
printf("Copying %zd bytes with strncat\n", left);
dest = strncat(dest, src, left);
}
else{
printf("No copy to prevent overflow\n");
}
}
else{
// no memory of malloc. Can't help yet
dest = libc_strcat(dest, src);
}
return dest;
}
Now my problem is that in any test file, strcat(dest, "Hello World");
does not call my wrapper strcat, but
char src[]="Hello World";
strcat(dest, src);
properly calls my wrapper. I can't remove the const keyword as that makes it non-conformant with the declaration in string.h.
What could I do so that both calls go to my wrapper?