Discussion:
CURLOPT_WRITEDATA user pointer not being passed in callback
J Curl via curl-library
2021-04-05 19:06:35 UTC
Permalink
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.

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.
Henrik Holst via curl-library
2021-04-05 21:16:51 UTC
Permalink
Den mån 5 apr. 2021 kl 22:35 skrev J Curl via curl-library <
Post by J Curl via curl-library
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
Post by J Curl via curl-library
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);*
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);*
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
J Curl via curl-library
2021-04-06 02:57:09 UTC
Permalink
Thank you Henrik, that solved the problem. I did not "review" the code
enough, had I read the documentation for CURLOPT_HEADERFUNCTION I would
have seen it also affected the write callback. My bad. Thank you for your
help.

Loading...