cURL / Mailing Lists / curl-library / Single Mail

curl-library

RE: Timeout & crash Issue

From: Yoav Schreiber <yoavs_at_mercury.co.il>
Date: Sun, 21 Mar 2004 05:57:06 +1100

And in case you work with cpp (rather than c), always ostringstream is
preferred. The use of strstream is not recommended.

Yoav

-----Original Message-----
From: curl-library-bounces_at_cool.haxx.se
[mailto:curl-library-bounces_at_cool.haxx.se] On Behalf Of Darrick Brown
Sent: Saturday, March 20, 2004 12:11 AM
To: 'libcurl development'
Subject: RE: Timeout & crash Issue

Hi

Your problem may be due to a non-terminated strstream. In the example you
gave, the strstream is being used to store incoming data from a curl
callback which is basically raw bytes. Before trying to use the stream data
as a string, null terminate it.

...
     if (status == 0)
     {
         outbuf.put('\0'); // terminate the string
         string out = outbuf.str();
         int index = out.find_last_of("</Packet>");
         xml_data = out.substr(0,index+1);
     }
...

Here's some sample code that exhibits this "bug". You should see the first
printf contain garbage or perhaps crash:

int main(int argc, char* argv[])
{
        std::strstream buf1, buf2;

        buf1.write("test1", 4);
        buf2.write("test2", 4);

        // May crash
        printf("not terminated: %s\n", buf1.str());

        // terminate buf2
        buf2.put('\0');
        printf(" terminated: %s\n", buf2.str());

        return 0;
}

Hope this helps.

 - Darrick

-----Original Message-----
From: curl-library-bounces_at_cool.haxx.se
[mailto:curl-library-bounces_at_cool.haxx.se] On Behalf Of Gunn_Fergal_at_emc.com
Sent: Friday, March 19, 2004 1:58 AM
To: curl-library_at_cool.haxx.se
Subject: Timeout & crash Issue

I've seen some issues when using libcurl. We are using Curl 7.10.5

1) Our program is crashing out on us at the same place every time (see red
line below).
        "string out = outbuf.str();"
    The thing that makes this all the more hard to debug is that we have no
debug or trace
    information when it does crash. The program just stops and not core
file or error log.
    Is there some setting we must set in order to get debug info???

2) The http request we are making sometimes fails to return and I tried to
use the CURLOPT_TIMEOUT
    setting below to interrupt a request. This has not worked for me
though. Is there another
    setting that needs to be set in conjunction with CURLOPT_TIMEOUT??

I'd appreciate any help with these problems as I have only very minimal Curl
experience.

Here's the problematic code

void XHMP::GetXmlData(std::string & xml_data, const bool secure)
{
    try
    {
        CURL *curl;
        ostrstream outbuf;
        string url;

        curl = curl_easy_init();
        if (curl != NULL)
        {
            struct curl_slist *headers=NULL;

            url = secure ? "https://" : "http://";

            url += m_host;

            url += secure ? "" : ":8000";
    
            url += "/servlets/CelerraManager" ;

            curl_easy_setopt(curl, CURLOPT_URL,url.c_str());

            headers = curl_slist_append(headers, "Content-Type: text/xml");

            string user = "EMCXMPUser: " + m_uname;
            headers = curl_slist_append(headers, user.c_str());

            headers = curl_slist_append(headers, "EMCXMPAction: mtr");

            if (secure)
            {
              curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
              curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
            }

            curl_easy_setopt(curl, CURLOPT_POSTFIELDS,m_XmlString.c_str() );
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

            curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,&curl_write_hook);

            curl_easy_setopt(curl,CURLOPT_FILE,&outbuf);
            curl_easy_setopt(curl,CURLOPT_TIMEOUT,30);

            char errorBuf[CURL_ERROR_SIZE];
            curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,&errorBuf);

            int status = curl_easy_perform(curl);
            if (status == 0)
            {
                string out = outbuf.str();
                int index = out.find_last_of("</Packet>");
                xml_data = out.substr(0,index+1);
            }
            curl_slist_free_all(headers);
            curl_easy_cleanup(curl);
        }
}

size_t XHMP::curl_write_hook(void *ptr, size_t size, size_t nmemb, void
*stream)
{
        // *stream is actually an ostream object
        ostrstream& buf=*(reinterpret_cast<ostrstream*>(stream));
        buf.write(reinterpret_cast<const char*>(ptr),size*nmemb);
        return size*nmemb;
}

_________________________________________________________
Fergal Gunn
Software Engineer
EMC Cork, Software Development ( Phone: + 353 (0)21 493 8277
Ovens, Co. Cork, Ireland. Ê Fax: + 353 (0)21 428 1833
http://www.emc.com * e-mail: Gunn_Fergal@emc.com
_________________________________________________________

________________________________________________________________________
This email has been scanned for all viruses.

Mercury Interactive Corporation
Optimizing Business Processes to Maximize Business Results

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
Received on 2004-03-20