cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: CURLOPT_WRITEFUNCTION - cannot make it working...

From: Dan Fandrich <dan_at_coneharvesters.com>
Date: Fri, 3 Jun 2011 22:40:00 -0700

On Fri, Jun 03, 2011 at 08:58:26PM -0700, Paolo Piacentini wrote:
> Here is the writeFunction. Things go awry immediately at the very 1st call,
> where I added comments.
>
> Does someone have any idea about what's going wrong or what I'm doing wrong?

[...]

> (*page_status).currentSize += nbytes;

On the first pass through, currentSize will be equal to the length of
the current data.

> if ( (*page_status).currentSize > (*page_status).spacemade ) {
> (*page_status).data = (char *)realloc( (*page_status).data,
> (*page_status).spacemade );

spacemade will be 0, so this realloc will be performed, creating a space of
0. This it the first problem.

> // Segmentation fault occurs here because
> (*page_status).spacemade is huge! ~3G!
>
> (*page_status).spacemade += NEWSPACE;

spacemade is increased by an arbitrary amount bearing no relation to the
space in the buffer. This is the second problem.

> }
>
> // realloc: in case ptr==NULL (1st argument), the function behaves as
> malloc, allocating a block
> // of size given by the 2nd argument (in bytes) and returning a pointer
> to the beginning of it.
>
> assert((*page_status).data != NULL);
> memcpy((*page_status).data + (*page_status).currentSize +1, ptr,
> nbytes);

The input data is copied not to the beginning of the data area where is
should go on the first pass, but to what would be past the end of the area.
That's the third problem. The fourth problem is the off-by-one error here;
even if the current size were fixed, this would copy the new data one
past where it should go.

>
> return nbytes;
> }
>

There's an example called getinmemory.c that has been time tested and would
be a good starting point for code to do this sort of thing.

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