cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: libcurl PUT retry and READFUNCTION

From: Dan Fandrich <dan_at_coneharvesters.com>
Date: Wed, 25 Jan 2012 11:27:50 -0800

On Wed, Jan 25, 2012 at 02:34:12PM +0000, Wenjun Chen wrote:
> Thanks Dan for the response. No it is not on the stack. The same

Um, that's not what the code shows.

> data is passed in the put_data_callback and the length is fine there
> (489 bytes), data is copied when put_data_callback is called. It is
> possible that I may misuse the seek_data_callback, I coded based on my
> understanding of the document.
>
> The code looks like this.
>
> Put_data_t *data = malloc(sizeof(put_data_t));

Here, the data pointer is stored on the stack. It just happens to points
to data on the heap.

> data->buff = malloc(489); ( The buff is filled with data).
> data->read__pos = 0;
> data->data_len = 489;
> curl_easy_setopt(curl, CURLOPT_READDATA, &data); #### Note, the same data is passed in to the put_data_callback, when put_data_callback is called, data is fine.

Here, you're passing a pointer to data which is on the stack. As long as
all the code you showed is located within the same function you should
be fine. But if the curl_easy_perform is called after this function
returns, then the data pointer will go out of context and the callback
will get bogus data. Same goes if you try to access the pointer
elsewhere in the code after this function returns.

> curl_easy_setopt(curl, CURLOPT_READFUNCTION, put_data_callback);
> curl_easy_setopt(curl, CURLOPT_SEEKDATA, &data); #### It is the same data that passed in to the put_data_callback.
> curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, seek_data_callback);
> curl_easy_perform(curl);

>>> Dan
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-01-25