curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: CURLOPT_WRITEDATA user pointer not being passed in callback

From: Henrik Holst via curl-library <curl-library_at_cool.haxx.se>
Date: Mon, 5 Apr 2021 23:16:51 +0200

Den mån 5 apr. 2021 kl 22:35 skrev J Curl via curl-library <
curl-library_at_cool.haxx.se>:

> I am trying to write a http client to send a HTTPs PUT request and save
> the response in memory. To try this I have combined the anyauthput.c
> <https://curl.se/libcurl/c/anyauthput.html>and getinmemory.c
> <https://curl.se/libcurl/c/getinmemory.html>examples but when I run it
> the user pointer set via the CURLOPT_WRITEDATA is not being passed back in
> the callback. Instead a different pointer is passed (and so trying to
> access it leads to a segmentation fault).
>
> I have put the code on my google drive if anyone cares to have a look.
> <https://drive.google.com/file/d/11YVmqj4qGxSy5Ax_55PYz6POzoYDGpyT/view?usp=sharing>
>
> I have reviewed the code many times and cannot see what it is I'm doing
> wrong. I'm starting to suspect a bug in libcurl but would like to eliminate
> anything obvious someone could see that I'm overlooking.
>
> I print the value of the pointer set in CURLOPT_WRITEDATA and the value
> that is passed when the callback is invoked and I can clearly see it is
> different.
>

This happens due to you earlier setting the CURLOPT_HEADERDATA:

    curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);

curl will call the WriteFunction with this as the userp when receiving the
HTTP headers since you have no defined CURLOPT_HEADERFUNCTION function. If
CURLOPT_HEADERFUNCTION is set then that will handle all the headers but
since that is not set then your write function will get the HeaderData
pointer instead until there is real data to receive.

/HH

>
> in the main initialization:
> struct MemoryStruct chunk;
> curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
> curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)(*&chunk*));
> *printf("chunk: %p, chunk.memory: %p, chunk.size: %lu\n", &chunk,
> chunk.memory, (unsigned long)chunk.size);*
>
> in WriteMemoryCallback:
> static size_t WriteMemoryCallback(void *contents, size_t size, size_t
> nmemb, *void *userp*) {
> *fprintf(stderr, "contents: %p, size: %lu, nmemb: %lu, userp: %p\n",
> contents, size, nmemb, userp);*
>
> The print output shows the following:
> chunk: *0x7ffc7f6ea470*, chunk.memory: 0x15b4840, chunk.size: 0
> ...
> contents: 0x15d5dc0, size: 1, nmemb: 15, userp: *0x1593010*
>
> Can anyone help ? Thanks.
> -------------------------------------------------------------------
> Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
> Etiquette: https://curl.se/mail/etiquette.html


-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.se/mail/etiquette.html
Received on 2021-04-05