I know from previous C projects that ASAN tells me "heap-use-after-free" when I try to access to a pointer after the use offree() but in this case I don't even understand because i use free() at the end of the main and do nothing after.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define URL "URL"
#define API_KEY "KEY"
void add_parameter(char *query, char *key, char *value)
{
const size_t new_len = strlen(query) + strlen(key) + strlen(value) + 3;
query = realloc(query, new_len * sizeof(char));
if (query)
{
strcat(query, "?");
strcat(query, key);
strcat(query, "=");
strcat(query, value);
query[new_len - 1] = '\0';
printf("%s\n", query);
}
}
int main()
{
char *query = malloc((strlen(URL) + 1) * sizeof(char));
memcpy(query, URL, sizeof(URL));
add_parameter(query, "api_key", API_KEY);
printf("%s\n", query);
free(query);
return 0;
}
Thanks for your help