I’m trying to allocate a struct of size sz in my malloc wrapper function and then automatically add a footer after that memory area to track potential writing beyond the boundary.
In malloc wrapper function:
malloc_result = (char *) malloc(sz+sizeof(FOOTERSTR));
...
struct metadata_record * metarec = get_metadata_record_new(sz);
...
create_footer(metarec,footer,key_address);
void create_footer(struct metadata_record *rec, char *footer, char *key_address) {
//Add Footer
printf("footer = %s\n",footer);
printf("key_address = %p\n",key_address);
char *new_footer_ptr = key_address+sizeof(key_address);
printf("new_footer_ptr = %p\n",new_footer_ptr);
strncpy(new_footer_ptr,footer,sizeof(footer));
new_footer_ptr[sizeof(footer)-1] = '\0';
printf("new_footer_ptr = %s\n",new_footer_ptr);
}
When I run this I get:
footer = zftsfviz
key_address = 0x964ef10
new_footer_ptr = 0x964ef14
new_footer_ptr = zft
I’m hoping to get the full footer text when printing out the new_footer_ptr as a string but instead I get only the first part of the footer.
I’m not sure what “key_address” is, but you should be aware that sizeof(key_address) and sizeof(footer) are both synonymous with sizeof(char*) which is probably not what you want. You probably want strlen(key_address) and strlen(footer) instead. If you actually need to know the allocated size you’ll need to store that somewhere and pass it in to this function.