#using chained cout causes crash [libcurl]

45 messages · Page 1 of 1 (latest)

shut atlas
#

#include <iostream>
#include <curl/curl.h>

size_t writefunction(void* contents, size_t size, size_t nmemb, void* s){
size_t dummyLengthToReturn = size * nmemb;
return dummyLengthToReturn;
}

int main(){

CURL *curl = curl_easy_init();

if (curl){

curl_easy_setopt(curl,CURLOPT_CUSTOMREQUEST,"GET");
curl_easy_setopt(curl,CURLOPT_URL,"http://google.com");
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
// curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writefunction);

CURLcode result = curl_easy_perform(curl);

// std::cout << "Response code received: " << result << std::endl; // crashes
//std::cout << "Response code received: " << result; // crashes
std::cout << "Response code received: "; // no crash
std::cout << result; // no crash
// std::cout << result << "\n"; // crashes

curl_easy_cleanup(curl);
}

return 0;
}

vagrant pecanBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

shut atlas
#

Checked gemini and chatgpt and after lots of back and forth they say the output of first cout << is giving a corrupt output stream and thus chaining it causes crash. but im still confused with this.

dark forge
#

based on what you've shown I'd guess that this isn't about std::cout

#

have you tried running it in a debugger?

#

also, how are you compiling? You should disable optimizations and use sanitizers (on gcc and clang, -O0 -g3 -fsanitize=address,undefined, also -fhardened if your compiler supports that)

shut atlas
# dark forge have you tried running it in a debugger?

This is on MacOS [office provided], issues with using a debugger [from vscode].

For compiling was using "g++ -o main main.cpp -lcurl"

Now I changed to this:
"g++ -o main main.cpp -lcurl -O0 -g3 -fsanitize=address,undefined" [-fhardeded is not available]
And I see a change, now its hung and not returning/crashing immediately

#

I was sure that I'm doing something dumb but find it interesting that gemini/cgpt/claude all bailed out

dark forge
#

I don't know a lot about macos but you should still be able to use a debugger

idle moss
shut atlas
idle moss
#

That is not the problem.

#

Also it seem this is missing: curl_global_init(CURL_GLOBAL_DEFAULT);

shut atlas
#

printf("response is: %u", result);

this works..

shut atlas
shut atlas
shut atlas
idle moss
#

result is just an enum.

#

the problem is not in std::cout

#

something else is wrong that put the machine in a chaotic state

#

make sure the libraries version is the correct for your platform

shut atlas
shut atlas
#

I think the root cause is macOS, i'll confirm it; but the suggestion offered here by gemini are also not remidiating [curl_easy_strerr() wrapping also crashes]. thank you guys

vagrant pecanBOT
#

@shut atlas Has your question been resolved? If so, type !solved :)

median fossil
median fossil
shut atlas
rocky ore
#

Same thing for me, I uncommented every "crashes" line, everything compiled and ran normally

fringe crow
#

does it work if you comment out the line with setopt writefunction?

shut atlas
fringe crow
# shut atlas can’t run valgrind on this mac, but i understand the crashing is due to the memo...

it's not really "decides", it's just a side effect of messing something up somewhere. Like if you randomly overwrite some memory by mistake, of course sometimes it does different things, because it depends on what gets overwritten.

Again, no idea what's wrong with this program because it seems too simple to have a memory corruption bug in it, unless something is wrong with the compiler settings or something...

idle moss
#

This is why I prefer header only libraries. Everything points to a problem between the library being linked and the target platform.

median fossil
# shut atlas just tested it on linux [debian], and even the working version (non chained cout...

well then in your linux debian machine install gdb, and use the gdb debugger to run the program gdb ./a.out , when the program hangs press Ctrl + C to force the program to stop, then type bt which is the gdb short alias for backtrace to see what C source code is triggering the freezing of the program, it may be an obfuscated mess due to compiler optimizations

in my testing the libcurl library I took by doing ldd /usr/bin/curl to print the list of dynamic libraries which the command line program curl is using, then filter that for the name of the library a.k.a.

ldd /usr/bin/curl 2>&1 | grep -i curl

and from there, get the full path to the existing libcurl file, and use that to recompile the program you gave in your post here.

I never ran gdb because for me libcurl successfully downloaded the web page contents and printed it to the screen followed by the exit code, literally your source code in main.c

median fossil
shut atlas
#

update: same program compiled on ubuntu doesn't crash/hang at all. in any cases.. but does hand on debian [compiled on ubuntu]

shut atlas
# median fossil well then in your linux debian machine install gdb, and use the gdb debugger to ...

let me try, also the lib i was using is installed from apt (on both debian/ubuntu) is "libcurlpp-dev", on macOS it's the default one [i think comes from the default xcode tools package]

ldd on ubuntu on debian gave libs with different names.
ubuntu: libcurl.so.4 => /lib/x86_64-linux-gnu/libcurl.so.4 (0x00007521aeba9000)
debian: libcurl-gnutls.so.4 => /lib/x86_64-linux-gnu/libcurl-gnutls.so.4 (0x00007f57504c5000)

and compiling using this dir on a debian machine without the libcurlpp-dev package didn't work:
g++ main.cpp -L/lib/x86_64-linux-gnu -lcurl main.cpp:2:10: fatal error: curl/curl.h: No such file or directory 2 | #include <curl/curl.h> | ^~~~~~~~~~~~~ compilation terminated.

update: gdb couldn't find any debugging symbols. do i need to add any flags?

`$ gdb ./a.out
GNU gdb (Ubuntu 15.1-1ubuntu1~24.04.1) 15.1
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
https://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...

This GDB supports auto-downloading debuginfo from the following URLs:
https://debuginfod.ubuntu.com
Enable debuginfod for this session? (y or [n]) y
Debuginfod has been enabled.
To make this setting permanent, add 'set debuginfod enabled on' to .gdbinit.
(No debugging symbols found in ./a.out)
(gdb) bt
No stack.
(gdb) backtrace
No stack.
(gdb) `

median fossil
# shut atlas let me try, also the lib i was using is installed from apt (on both debian/ubunt...
and compiling using this dir on a debian machine without the libcurlpp-dev package didn't work:

well then install that in the debian machine, compile the program a.out and run gdb. For real life serious use cases, installing libcurlpp-dev on the servers of your paying customers is a horrible idea, but we will ignore that for now.

When I was testing this, since I had the libcurl.so file, but not the headers, I downloaded the git repo of curl itself which has all the headers in there, this requires more manual effort and a deep understanding of compiler directory settings.

#

for the broken MacOS behavior you see, well use gdb to see why it freezes like I showed you

shut atlas
#

sorry if im dumb, but as i said gdb didn't show anything when run backtrace. none of this is in production, im just trying to understand what is causing this discrepancy. the library name i said is because both of them are the same kind (aside version diff in deb12 vs ubu24.04), and in mac it is from the xcode tools package that comes default.

gdb says no debugging symbols available and backtrace shows "No stack", so am i doing anything wrong or is there anyother way to find out the cause? looking at the differences bw debian and ubuntu, could this be due to apparmor/aslr & similar thing in macos? because no other obv intelligible change is observable..

printf doesn't cause any hang/crash anywhere, only chaining cout insertion operator does

median fossil
median fossil
#

oh yeah, and also ask gdb to execute the program:

gdb ./a.out
#wait for gdb to load
start
#gdb prints that a breakpoint is placed
continue
#wait for the program to freeze
#press Ctrl + C to force interrupt the program
bt
median fossil
#

for example: