https://gist.github.com/macarr/b49c0097666a613f639c4eab931f31d4
I'm making a little app in C that's supposed to connect to the ecobee API. Ignore for the moment that this is dumb/terrible/why god are you using C as a REST API client, it's a personal project for the fun of it.
I'm currently running into a problem with memory management. I've annotated the provided gist with comments and snipped out code that I don't believe is related. Basically, the app works exactly as expected until I reach getTokens. Then the following code freaks the heck out:
struct authorizations getTokens(char* apiKey, char* authCode) {
char* body = "grant_type=ecobeePin&code=";
body = concat(body, authCode);
printf("%s!!!!!!!!!!\n", body); //as expected
concat2(body, "&client_id=");
printf("%s!!!!!!!!!!\n", body); //as expected
concat2(body, apiKey);
printf("%s!!!!!!!!!!\n", body); //garbage
...
See the gist for the function of concat and concat2.
char* concat(const char *s1, const char *s2)
{
char* result;
result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
if(!result) {
exit(1);
}
strcpy(result, s1);
strcat(result, s2);
printf("CONCAT1: %s\n", result);
return result;
}
void concat2(char *s1, const char *s2) {
char temp[strlen(s1)];
strcpy(temp, s1);
printf("%s:%s\n", temp, s2);
s1 = realloc(s1, strlen(temp) + strlen(s2) + 1);//+1 for the null terminator
if(!s1) {
exit(1);
}
strcpy(s1, temp);
strcat(s1, s2);
printf("CONCAT2: %s\n", s1);
}
At the end of my function, I free(body)
, which kills the application because apparently body has already been freed. I guess one of my realloc
s aren't working or something?
What confuses me most is that when I was working with bad data two days ago (making invalid calls to the api and just pulling info from the errors to populate the requests going forward - I didn't have a login set up then), everything was working fine. Once I started getting real data, the application started "stack smashing". This is as far as I was able to push it last night.
Additionally, any general advice on where I'm going wrong in regards to string concatenation and pointer manipulation would be welcome. Assume I've already heard why I shouldn't be using C as a REST API client