cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: unexpected output in error buffer & correct gdiff

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Tue, 9 Oct 2001 00:44:28 +0200 (MET DST)

On Sun, 7 Oct 2001, Lucas Adamski wrote:

> Occasionally (with no discernable corresponding pattern) I get the HTTP
> req in the error buffer field (set by CURLOPT_ERRORBUFFER), like so:
>
> GET / HTTP/1.1^M
> User-Agent: netscape^M
> Host: 192.168.0.1:8180^M
> Pragma: no-cache^M
> Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*^M
>
> This happens sporadically about every 100 attempts or so. These are
> separate processes querying only 1-3 urls right now, so I don't think its
> a memory corruption issue. It returns a 200 OK though, so the error
> buffer should be empty. In all other requests it behaves normally (i.e.
> blank if no error, or it contains the appropriate error msg). Any ideas?
> Thanks,

Yes, here's one theory: the buffer is not touched when there's no error to
fill in. If you malloc() the error buffer before libcurl is called, it is
likely that the buffer you get contains a piece of a previously allocated
string. The HTTP request is an example of a previously allocated string.

When libcurl returns CURLE_OK, you should not use the error buffer.

> Oh, here's the proper gdiff for the CURLOPT_MAXDOWNLOAD hack... sorry, I
> did it backwards last time. :)

I've added some comments about this code inlined here (please attach the diff
the next time, line wrapping etc caused when just inserted in the mail makes
it harder to apply!)

> @@ -557,8 +561,19 @@
> /* check for Content-Length: header lines to get size */
> if (strnequal("Content-Length:", p, 15) &&
> sscanf (p+15, " %ld", &contentlength)) {
> - conn->size = contentlength;
> - Curl_pgrsSetDownloadSize(data, contentlength);
> + if((conn->maxdownload!=-1) && (contentlength > conn->maxdownload)) {
> + if (conn->bits.rangestringalloc == TRUE)
> + free(conn->range);
> + snprintf(maxrange, sizeof(maxrange), "0-%ld",
> conn->maxdownload);
> +
> + /* tell ourselves to fetch this range */
> + conn->range = strdup(maxrange);
> + conn->bits.use_range = TRUE; /* enable range download */
> + conn->bits.rangestringalloc = TRUE; /* mark range string
> allocated */
> + contentlength=conn->maxdownload;
> + }
> + conn->size = contentlength;
> + Curl_pgrsSetDownloadSize(data, conn->size);

Why do you need to set the range string at this point? The range string is
used for the HTTP request, and at this point you're already receiving
headers...

> /* maxdownload must be -1 on init, as 0 is a valid value! */
> - conn->maxdownload = -1; /* might have been used previously! */
> + conn->maxdownload = data->set.maxdownload; /* might have been used
> previously! */
> + infof(data, "Setting download limit to %ld\n", conn->maxdownload);

Please don't do unconditional infof() of this value. It'll be -1 to the
majority of people 99.99% of all times...

Apart from these comments, I think your fixes are fine. Thanks a lot!

Could you make a new diff against a more recent libcurl version, preferably
the CVS tree?

-- 
    Daniel Stenberg -- curl groks URLs -- http://curl.haxx.se/
Received on 2001-10-09