简体   繁体   中英

Crash on curl_easy_perform() when uploading a file on CURL in C++

I'm having an issue with a crash when uploading a file via the curl library in C++. I'm using the exact demo code from this location: https://curl.haxx.se/libcurl/c/fileupload.html

The only thing I change in the code is the upload location, to upload to a local wamp server on windows, and the file to upload, which I've verified that its opening ok.

I'm running through visual studio 2014, and building CURL through DLL

The output from the program is:

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> PUT /replayupload.php HTTP/1.1
Host: 127.0.0.1
Accept: */*
Content-Length: 43
Expect: 100-continue

< HTTP/1.1 100 Continue

*then I get a crash at line 66 in the program. It seems the line:

 res = curl_easy_perform(curl);

is causing the problem with an invalid parameter. I have verified that the curl variable is not null, but I'm finding it very difficult to get any more debug info than that, the call stack just references a memory address within the DLL.

I'm able to run the demo to just upload post variables and get a page, this runs fine without a crash. The crash only occurs when uploading a file.

my exact code is:

int main(void)
{
    CURL *curl;
    CURLcode res;
    struct stat file_info;
    double speed_upload, total_time;
    FILE *fd;

    fd = fopen("E:\\testfiles\\test.txt", "rb"); /* open file to upload */
    if (!fd)
        return 1; /* can't continue */

              /* to get the file size */
    if (fstat(_fileno(fd), &file_info) != 0)
        return 1; /* can't continue */

    curl = curl_easy_init();
    if (curl) {
        /* upload to this place */
        curl_easy_setopt(curl, CURLOPT_URL,
            "http://127.0.0.1/testupload.php");

        /* tell it to "upload" to the URL */
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        /* set where to read from (on Windows you need to use READFUNCTION too) */
        curl_easy_setopt(curl, CURLOPT_READDATA, fd);

        /* and give the size of the upload (optional) */
        curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
            (curl_off_t)file_info.st_size);

        /* enable verbose for easier tracing */
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        }
        else {
            /* now extract transfer info */
            curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
            curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);

           fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
                speed_upload, total_time);

        }
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    fclose(fd);
    return 0;
}

Thanks to Tkausl for spotting the line

/* set where to read from (on Windows you need to use READFUNCTION too) */

I added this line to my code

curl_easy_setopt(curl, CURLOPT_READFUNCTION, &fread);

And now everything seems to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM