Re: cURL bug -- Segmentation Fault when timeout is 1 second
Date: Wed, 4 Feb 2009 19:57:53 +0100
> Please don't top-post. http://curl.haxx.se/mail/etiquette.html
> Daniel Marschall on February 04, 2009 10:24 AM wrote:
>> I am very confused at the moment. Can you please help me? I don't know
>> to do. It doesn't work...
>> Now I have again a memory-access-error :-(
>> static int writer(char* data, size_t size, size_t nmemb, char* buffer)
>> // What we will return
>> int result = 0;
>> // Is there anything in the buffer?
>> if (buffer != NULL)
>> buffer = (char*)realloc(buffer, size * nmemb); // Extend
>> the buffer
> No, this does not "extend" the buffer. It will resize the buffer to
> exactly size*nmemb bytes (and forget how large it was before). Did
> you read the documentation for realloc()?
>> // Append the data to the buffer
>> strcat(buffer, data);
> You claimed that you didn't use "strncpy() or a similar function".
> certainly qualifies. This is dangerous code, causing potential buffer
> overruns and security risks. Did you earlier say this code runs on a
> root server? That would be scary. Again, use memcpy() or a similar
> For example, assume "buffer" is infinitely large (for now), and
> is the next element in "buffer" that you will write to. More correct
> code would be:
> memcpy( &buffer[lastindex], data, size*nmemb );
> lastindex += size*nmemb;
Ok, I will try it. But lastindex may not be a global variable. I have to
detect it direct inside of the writer-function to allow asychronous calls.
> Of course, in reality you would need to ensure that buffer is large
> to hold the new data. At a minimum, your realloc call would be:
> buffer = realloc( buffer, lastindex + 1 + size*nmemb );
> But this is inefficient, because you would call realloc on every write
> callback. It is better to allocate a large buffer, and then expand it
> in large chunks as needed later on.
I have now following code, but it still doesn't work:
Isn't there any example code that simply make a http-call with C?
static int writer(char* data, size_t size, size_t nmemb, char* buffer)
// What we will return
int result = 0;
// Is there anything in the buffer?
if (buffer != NULL)
//buffer = (char*)realloc(buffer, size * nmemb);
int lastindex = strlen(buffer); // is that correct?
buffer = (char*)realloc( buffer, lastindex + 1 + size*nmemb );
// Append the data to the buffer
memcpy( &buffer[lastindex], data, size*nmemb );
lastindex += size*nmemb;
// How much did we write?
result = size * nmemb;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
char *buffer = (char*)malloc(12);
//buffer = '\0';
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
Can you please show me how to make a correct code for that simple suppose
"http-output into char*"?
Sorry, I am very new in C++ and in C I have also more problems than in C++
where you have "easy" functions like ->Append. I developed in the past with
Java, Delphi and PHP where the pointer stuff is not so complex like here.
> GaryM at Casabi
Received on 2009-02-04