curl-library
Re: cURL bug -- Segmentation Fault when timeout is 1 second
Date: Wed, 4 Feb 2009 18:02:37 +0100
Here is my current C-Code (because I have switched from C++ to C):
Still the same error. Is there some mistake I made?
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <curl/curl.h>
using namespace std;
//********************************************************************************************
//
// writer
//
// This is the writer call back function used by curl
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)
{
// Append the data to the buffer
strcat(buffer, data);
// How much did we write?
result = size * nmemb;
}
return result;
}
//********************************************************************************************
//
// http_call
//
// Ruft eine URL auf und gibt im Fehlerfall false zurück.
static bool http_call (void) {
CURL *curl;
curl = curl_easy_init();
if(curl) {
// Infos: http://us3.php.net/curl_setopt
// URL übergeben
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
// Keine Kopfzeile einblenden
curl_easy_setopt(curl, CURLOPT_HEADER, false);
// Verbindung danach beenden
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, true);
// Pufferoptionen
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
char errorBuffer[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
char* buffer;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
// Benutzerdefinierter User-Agent-Name
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Hello World Browser");
// Einen Timeout setzen
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
// Weiterleitungen beachten, aber nur max. 50x
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50);
// Einen HTTP-Fehler-Statuscode beachten
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
// SSL-Zertifikate nicht prüfen (Warnung! Sicherheitslücke)
// http://ademar.name/blog/2006/04/curl-ssl-certificate-problem-v.html
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
// Abfrage ausführen und Resultat speichern
CURLcode res;
res = curl_easy_perform(curl);
// Clean up
curl_easy_cleanup(curl);
// Alles OK?
if (res == CURLE_OK)
{
return true;
}
else
{
return false;
}
} else return false;
}
int main(void) {
while (true) {
cout << "A" << ::std::endl;
http_call();
sleep(2);
}
return 0;
}
--------------------------------------------------
From: "Daniel Marschall" <info_at_daniel-marschall.de>
Sent: Wednesday, February 04, 2009 4:32 PM
To: "libcurl development" <curl-library_at_cool.haxx.se>
Subject: Re: cURL bug -- Segmentation Fault when timeout is 1 second
> In my C-Adaption I replaced the "string buffer" with a "char* buffer" and
> rewrote the writer-function. Also there, the bug appears. And: I copied
> the code from the offical size, so it cannot be my fault. So, is there a
> bug in my code?
>
> --------------------------------------------------
> From: "Gisle Vanem" <gvanem_at_broadpark.no>
> Sent: Wednesday, February 04, 2009 7:08 AM
> To: "libcurl development" <curl-library_at_cool.haxx.se>
> Subject: Re: cURL bug -- Segmentation Fault when timeout is 1 second
>
>>> ... * snip *
>>>>
>>>> string buffer; curl_easy_setopt(curl, CURLOPT_WRITEDATA,
>>>> &buffer);
>>> .... * snip *
>>>>
>>>> http_call(); sleep(2);
>>>
>>
>> Daniel Marschall <info_at_daniel-marschall.de> wrote:
>>
>>> No, I cannot see what could be wrong there. I call the URL and wait 2
>>> seconds.
>>
>> At first look it seemed you were using a variable on stack in a
>> global scope (while sleep() is running). But as Daniel saids, you cannot
>> pass a 'std::string' to CURLOPT_WRITEDATA.
>>
>> --gv
>>
>
Received on 2009-02-04