curl / Mailing Lists / curl-library / Single Mail

curl-library

Re: Usage of CURLOPT_ERRORBUFFER

From: Ray Satiro via curl-library <curl-library_at_cool.haxx.se>
Date: Sat, 23 Dec 2017 16:05:21 -0500

On 12/23/2017 3:29 PM, David Chapman wrote:
>> The docs for CURLOPT_ERRORBUFFER say:
>>
>> "You must keep the associated buffer available until libcurl no longer
>> needs it. Failing to do so will cause very odd behavior or even
>> crashes. libcurl will need it until you call curl_easy_cleanup or you
>> set the same option again to use a different pointer. "
>>
>> While this is an odd design choice, I wonder what "keeping the buffer
>> available" means in this context. Does it mean that it should remain
>> allocated, or that its contents shouldn't be touched? What happens if
>> I want to make multiple requests using the same bufffer, and zero it
>> before each request? Should I call curl_easy_cleanup, then
>> curl_easy_init again before each new request?
>
> This isn't an odd choice at all; a libcurl session could be active for
> a very long time, and there may be many errors reported.  The buffer
> would be used each time there is an error.  The help text points out
> that libcurl has no way to know when you have released (freed) the
> buffer, so you must notify it in some way.  If you think the text is
> unclear you are welcome to suggest changes.
>
> As for zeroing the buffer before each request, you could do that. The
> sample code does.  It strongly suggests that not all error conditions
> will cause the buffer to be overwritten.  You don't need to zero the
> buffer (e.g. by writing a '\0' into the first byte) before performing
> an action unless you plan to check it immediately after the action.
>
> You don't need to call curl_easy_cleanup() or curl_easy_init() each time.

Indeed. You'll set a pointer to the buffer that will receive the
extended error information from libcurl, and so of course you need to
keep it around. We could be clearer regarding when the buffer is written
to. I'm noticing the doc says:

"If the library does not return an error, the buffer may not have been
touched. Do not rely on the contents in those cases."

.. which could imply that if it does return an error that the buffer
will always be written to, but currently that is not the case. I've
opened a PR to address this, see https://github.com/curl/curl/pull/2190

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2017-12-23