(The incorrect length has already been mentioned.)
There is a lot of pointless copying going on in your code. Since you don't modify platformUrlDelimiter
, there is no need to copy it twice. This is much simpler:
const char *platformUrlDelimiter = "common";
if (isLinux == 0) {
platformUrlDelimiter = "linux";
} else if (isOSX == 0) {
platformUrlDelimiter = "osx";
}
And remove the call to free (platformUrlDelimiter)
.
Every call to strcat()
(probably) has to needlessly loop through the buffer. I would write:
int length = strlen(kBaseUrl)
+ strlen(platformUrlDelimiter)
+ strlen(arg)
+ 6;
*dest = malloc(length);
if (*dest) {
sprintf (*dest, "%s/%s/%s.md", kBaseUrl, platformUrlDelimiter, arg);
} else {
/* Error */
}
return length;
Now there is no need to use buffer
at all.
Also, there's a real bug right at the end of your code:
free(buffer);
return strlen(buffer) + 1;
You must not use the value of buffer
in any way after it has been freed.