Red Hat Bugzilla – Bug 996793
compiling curl example sendrecv.c with -O0 NULLs a variable.
Last modified: 2014-04-21 13:02:55 EDT
Description of problem:
Try compiling the libcurl-devel-7.19.7-37 example sendrecv.c:
$ gcc sendrecv.c -lcurl -o sendrecv
Compile and linking go well but executing the resulting binary fails. After the call to curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockfd) the parameter curl is set to NULL, making the following curl_easy_send() fail.
I first suspected this to be a curl bug but this seems not to be the case.
When using another optimization level (say -O1 or -O2) the resulting binary behaves as expected.
Version-Release number of selected component (if applicable):
Please provide preprocessed testcase.
What do you mean by a preprocessed test case? Do you want me to send you a precompiled binary?
The file sendrecv.c can be found in libcurl-devel-7.19.7-36.el6_4.x86_64.rpm under /usr/share/doc/libcurl-devel-7.19.7.
If you build the file as per the above instructions you will see that on execution the resulting binary terminates with the message "Error: A libcurl function was given a bad argument". This is caused by the parameter curl having been set to NULL and being passed to curl_easy_send() as such. (You can verify that the error is caused by having been passed a NULL value from the curl sources.)
This setting to NULL is something the compiler does, it does NOT happen in curl. I added some printfs in various curl functions to verify this. The variable curl is still set when leaving curl_easy_getinfo() but unset right after returning on line 71 in sendrecv.c.
Building the same file with -O1 or -O2 results in a correct binary.
No, I need the sendrecv.i file generated by adding -save-temps to the command line options. This preprocessed file should fail with -O0, but work with -O, as you say.
For more info see http://gcc.gnu.org/bugs/ .
That testcase is just invalid. If you look at curl.h, you'll see that
for CURLINFO_LASTSOCKET (and many others) you need to call it with address of a long variable, but it is called with address of sockfd which has type int rather than long. So there is buffer overflow in the program.
Thanks for clearing this up. Indeed using a long for sockfd fixes the issue. So this is a bug in curl after all, even if just in an example.
Comparing the example in curl-7.32.0 shows the issue is indeed fixed upstream. sockfd is now of type curl_socket_t.