cURL / Mailing Lists / curl-library / Single Mail

curl-library

libcurl crashes when reusing a curl handle over SFTP after calling curl_easy_reset()

From: Brian Ulm <ulmbj1_at_gmail.com>
Date: Fri, 7 Mar 2008 21:32:59 -0500

I'm having some issues with reusing a SFTP curl handle after sending the
curl_easy_reset() command.

The scenario is:

1.) Initialize a curl handle
2.) Download a file over SFTP.
3.) Call curl_easy_reset() on the curl handle.
4.) Attempt to download the same file using the same commands as in step 2.

Everything seems good up until step 4. As soon as the curl_easy_perform()
is called during step 4, the program crashes. The line of code is in
ssh.cand the ssh_getworkingpath() function on the line:

 /* store the pointer for the caller to receive */
  *path = real_path;

I am using the latest nightly version of libcurl compiled as a static
library on Windows with libssh2, openssl-0.9.8g, and zlib-1.2.3 support.

The following code reproduces the issue - just fill in your SSH server
specific settings:

#include <stdio.h>
#include <tchar.h>
#include <curl/curl.h>
#include <iostream>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{

///////////////////////////////////////////////////////////////////////////////////////
    // SSH SERVER SPECIFIC SETTINGS

///////////////////////////////////////////////////////////////////////////////////////
    std::string remoteFile = "sftp://REMOTE FILE URL";
    long serverPort = SERVERPORT;

    std::string loginInfo = "USER:PASS";
    std::string localFile = "C:/LOCAL FILE PATH";

///////////////////////////////////////////////////////////////////////////////////////
    // MAIN

///////////////////////////////////////////////////////////////////////////////////////
    CURL *curl;
    CURLcode curlRes;

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL, remoteFile.c_str()); // set URL
to fetch
    curl_easy_setopt(curl, CURLOPT_PORT, serverPort); // set
server port
    curl_easy_setopt(curl, CURLOPT_USERPWD, loginInfo.c_str()); // set
login info

    // download a file from the server

    FILE *fp;
    fopen_s(&fp, localFile.c_str(), "wb");

    if(fp != NULL)
    {
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); // setup
file pointer
        curl_easy_setopt(curl , CURLOPT_UPLOAD , 0 ); // not
uploading

        curlRes = curl_easy_perform(curl);
        std::cout << "curlRes = " << curlRes << std::endl;
    }

    if(fp)
    {
        fclose(fp); // close file pointer
    }

    // reset the CURL handle to reuse the connection
    curl_easy_reset(curl);

    // Now try downloading the file again

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL, remoteFile.c_str()); // set URL
to fetch
    curl_easy_setopt(curl, CURLOPT_PORT, serverPort); // set
server port
    curl_easy_setopt(curl, CURLOPT_USERPWD, loginInfo.c_str()); // set
login info

    fopen_s(&fp, localFile.c_str(), "wb");

    if(fp != NULL)
    {
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); // setup
file pointer
        curl_easy_setopt(curl , CURLOPT_UPLOAD , 0 ); // not
uploading

        curlRes = curl_easy_perform(curl);
        std::cout << "curlRes = " << curlRes << std::endl;
    }

    if(fp)
    {
        fclose(fp); // close file pointer
    }

    return 0;
}
Received on 2008-03-08