cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: write callback issues (was Re: A new problem)

From: Linus Nielsen Feltzing <linus_at_haxx.se>
Date: Thu, 03 Apr 2008 15:29:05 +0200

Prasad J Pandit wrote:
> Well, my code looks something like this
>
> size_t
> recv_data (void *buf, size_t unit_sz, size_t no_of_units, void *usrp)
> {
> int ret = 0;
> Curldata *rsp = NULL; /* it's the WRITE_DATA struct from example */
>
> if (buf && (rsp = new Curldata))
> {
> rsp->len = unit_sz * no_of_units;
> rsp->data = new char[rsp->len];
>
> if (rsp->data)
> {
> memset (rsp->data, '\0', rsp->len);
> _snprintf (rsp->data, rsp->len, "%s", (char *)buf);
>
> *(Curldata **)usrp = rsp;
> ret = (*(Curldata **)usrp)->len;
> }
> }
>
> return ret;
> }

This code looks very suspicious. Firstly, you overwrite the usrp pointer
for every call, which will lead to a memory leak, since you don't delete
the existing one first.

Secondly, what purpose does the sprintf() fill? As you use it, it could
be replaced with a simple memcpy(), since you seem to expect printable
data anyway.

Thirdly, with this approach, you will never see the entire result, since
you are constantly overwriting the data from the previous callback.

All in all, this code will not work very well.

Linus
Received on 2008-04-03