cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: bug in curl_formget()

From: Aaron Orenstein <aorenste_at_gmail.com>
Date: Fri, 10 Jun 2011 11:03:28 -0400

On Fri, Jun 10, 2011 at 2:44 AM, Daniel Stenberg <daniel_at_haxx.se> wrote:

> On Fri, 10 Jun 2011, Aaron Orenstein wrote:
>
>> I think I've come across a bug in curl_formget() where it's not closing
>> the files that it's opened. The problem is that it looks like
>> readfromfile() has a '>' where there should be a '<':
>>
>
> Great. Can you also please tell us how you fell over this problem? I'd like
> to write up a test case that first can repeat the problem and then
> subsequently verify that the patch fixes it.
>
>
Sure - the basic repro case is:
1. Create a form using CURLFORM_FILE which references a local file
2. Serialize the form data using curl_formget()
3. Attempt to delete the file. On Windows having the file open will lock the
file so the delete will fail. I suspect that unix variants won't have this
problem.
Here's a test I just wrote up to show it (note: totally missing proper error
handling):

const char* TEMP_FILE_NAME = "test.txt";
static size_t StaticFormGetCallback(void* _arg, const char* _buf, size_t
_len) { return _len; }

void CurlFormTest()
{
    // create our temporary file
    FILE* tmpfile = fopen(TEMP_FILE_NAME, "wb");
    fwrite("abcde", 1, 5, tmpfile);
    fclose(tmpfile);
    // create a form with a file contained in it
    curl_httppost* postFirst = NULL;
    curl_httppost* postLast = NULL;
    curl_formadd(&postFirst, &postLast,
        CURLFORM_COPYNAME, TEMP_FILE_NAME,
        CURLFORM_FILE, TEMP_FILE_NAME,
        CURLFORM_END);
    // 'serialize' our data (and by serialize I mean drop it on the floor)
    size_t length = 0;
    // BUG: this function currently never calls fclose() on the
TEMP_FILE_NAME file
    curl_formget(postFirst, NULL, &StaticFormGetCallback);
    curl_formfree(postFirst);
    // On operating systems (Windows) where fopen() locks the file this will
fail.
    int result = _unlink(TEMP_FILE_NAME);
    assert(result == 0);
}

I'm not sure how you'd properly check that a file had fclose() called on it
on unix.

- Aaron

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-06-10