The data passed in to write_data()
isn't necessarily nul-terminated; that's why it tells you the number of bytes.
This means you can't use strcat()
. Using it is running off the end of the array and corrupting the data structures used by malloc
/ realloc
, hence the error.
Your write_data()
should use memcpy()
instead, like so:
int write_data( char *ptr, size_t size, size_t nmemb, void *stream )
{
size_t ThisSize = (size * nmemb); //Stores the size of the data to be stored
size_t DataLen = RunningSize; //length of the data so far
RunningSize = (RunningSize + ThisSize ); //update running size (used as new size)
Data = realloc( Data, RunningSize ); //get new mem location (on the 12th iteration, this fails)
memcpy((char *)Data + DataLen, ptr, ThisSize); //add data in ptr to Data
return ThisSize; //the function must return the size of the data it received so cURL knows things went ok.
}
You will also need to initialise RunningSize
to 0, not 1. You can initialise Data
to NULL
- passing NULL
to realloc()
is allowed (and makes it behave just like malloc()
).