cURL / Mailing Lists / curl-library / Single Mail

curl-library

WRITEFUNCTION crashes

From: Khaled El Manawhly <k.elmanawhly_at_gmail.com>
Date: Tue, 22 Jan 2013 20:34:21 -0500

Hello, I basically did a copy-paste of the WRITEFUNCTION example found on
this site and on other forums, but for some reason, it crashes every time.
When I debugged, I found that the last variable going to the callback
function (void* stream in the code below) is NULL thus causing a memory
access violation. Can anybody help please? Here is the relevant code:

struct MemoryStruct {
char *memory;
size_t size;
};

size_t callback_func(char *ptr, size_t size, size_t count, void *stream)
{
  printf("Entered callback\n");

size_t realsize = size * count;
struct MemoryStruct *mem = (struct MemoryStruct *)stream;

mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
//explicitly changed to char * for compiler issues
if (!mem->memory) {
printf("Not enough memory (realloc returned NULL)\n");
exit(EXIT_FAILURE);
}

memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;

return realsize;
}

void SSHConnect(char *hostname,char *user,char *pass)
{
char strBuffer[1024];
sprintf(strBuffer,"sftp://%s:%s@%s",user,pass,hostname);
 curl_easy_setopt(hCurl, CURLOPT_URL, strBuffer);

struct MemoryStruct chunk;
chunk.memory = (char *)malloc(1); //explicitly changed to char * for
compiler issues
chunk.size = 0;

curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, (void *)&chunk);

curl_easy_perform(hCurl);

printf("%lu bytes retrieved\n", (long)chunk.size);

if(chunk.memory) {
free(chunk.memory);
}
}

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-01-23