clinuxcurlsegmentation-fault

curl_easy_cleanup() causes Segfault- why?


I have a function which reads from an url and writes to a file. I am getting a segfault when calling the curl_easy_cleanup(). I uncommented this call and I am not getting the segfault any more. someone having an idea why I am getting a segfault?

Compiler call:

root@zentrale:/root# cc -rdynamic -Wall -g  -lcurl -o Beispiel beispiel.c

This is the code including a full program to show its behaviour:

#include <curl/curl.h>

int main(int argc, char* argv[])
{
int get_2file(char *url, char *filename )


{
CURL *curl_handle;
FILE *FI;
int r,code;
 
code = 0; r=0;
/* init the curl session iand set url*/ 
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, url); 
FI = fopen(filename, "w");
if(FI) {
    /* write the page body to this file handle */ 
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, FI);
    r = curl_easy_perform(curl_handle);
    if (r != 0) {
        code = -1;
    }
    r = fclose(FI);
} else {
    code=-1;
}
/* cleanup curl stuff */ 
curl_easy_cleanup(curl_handle);
return code;
}
char *url="https://www.knebb.de/files/text.txt";
char *jsonfile="/tmp/wetter.test";
int ret;

ret=get_2file(url,jsonfile); 
printf("ret ist: %d.\n",ret);
return 0;
}

strace result (only last lines):

getdents64(5, /* 0 entries */, 32768)   = 0
close(5)                                = 0
getrandom("\xd6\x4d\x5f\x3a\x12\x6d\x04\xc5\xce\xf3\x0f\x14\x88\xf0\x5a\x13\xbf\xbf\xea\xab\xdd\xb8\x41\x04\x0b\xe8\x81\x86\xb2\xdc\x87\xb3"..., 64, 0) = 64
send(4, "\26\3\1\0\356\1\0\0\352\3\3\207XL\343\231\301\340\212\314\367\340\343\21\354\35\232X\262F\0R"..., 243, MSG_NOSIGNAL) = 243
recv(4, 0x16d820b, 5, 0)                = -1 EAGAIN (Die Ressource ist zur Zeit nicht verfügbar)
poll([{fd=4, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 0 (Timeout)
poll([{fd=4, events=POLLIN}], 1, 0)     = 0 (Timeout)
poll([{fd=4, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 0 (Timeout)
poll([{fd=4, events=POLLIN}], 1, 1000)  = 1 ([{fd=4, revents=POLLIN}])
poll([{fd=4, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 1 ([{fd=4, revents=POLLIN|POLLRDNORM}])
recv(4, "\26\3\3\0p", 5, 0)             = 5
recv(4, "\2\0\0l\3\3j\247'\311\215\246\312kWT\351\303\370\230\37\n\204\240E\37\26$\7\375DO"..., 112, 0) = 112
recv(4, "\26\3\3\26/", 5, 0)            = 5
recv(4, "\v\0\26+\0\26(\0\6J0\202\6F0\202\5.\240\3\2\1\2\2\21\0\351\35\27\266\342\r"..., 5679, 0) = 5422
recv(4, "\0\10V\374\2\360\233\350\377\244\372\326{\306D\200\316O\304\305\366\0X\314\246\266\274\24Ih\4v"..., 257, 0) = 257
recv(4, "\26\3\3\1,", 5, 0)             = 5
recv(4, "\f\0\1(\3\0\35 ;;\375\337\310n^\275H'\315\356\346\246\303\207\200\313\3434\304\2W\374"..., 300, 0) = 300
recv(4, "\26\3\3\0\4", 5, 0)            = 5
recv(4, "\16\0\0\0", 4, 0)              = 4
send(4, "\26\3\3\0%\20\0\0! \2308\22G\27\374a\36\25\307F\346v,F0G\211\37\361\215<"..., 42, MSG_NOSIGNAL) = 42
send(4, "\24\3\3\0\1\1", 6, MSG_NOSIGNAL) = 6
send(4, "\26\3\3\0(\0\0\0\0\0\0\0\0\n1CR.t\326\202\221\252\16\220\177\35y\251%>\233"..., 45, MSG_NOSIGNAL) = 45
recv(4, 0x16d8203, 5, 0)                = -1 EAGAIN (Die Ressource ist zur Zeit nicht verfügbar)
poll([{fd=4, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 0 (Timeout)
poll([{fd=4, events=POLLIN}], 1, 1000)  = 1 ([{fd=4, revents=POLLIN}])
poll([{fd=4, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 1 ([{fd=4, revents=POLLIN|POLLRDNORM}])
recv(4, "\24\3\3\0\1", 5, 0)            = 5
recv(4, "\1", 1, 0)                     = 1
recv(4, "\26\3\3\0(", 5, 0)             = 5
recv(4, "\232H\34\311}\23\302\203V\223w\350\21\305\356\313G\342\24.\311r\351\\\327\301,\212\253\2560#"..., 40, 0) = 40
openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = 5
fstat64(5, {st_mode=S_IFREG|0644, st_size=2335, ...}) = 0
fstat64(5, {st_mode=S_IFREG|0644, st_size=2335, ...}) = 0
read(5, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\t\0\0\0\t\0\0\0\0"..., 4096) = 2335
_llseek(5, -1476, [859], SEEK_CUR)      = 0
read(5, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\t\0\0\0\t\0\0\0\0"..., 4096) = 1476
close(5)                                = 0
send(4, "\27\3\3\0\324\0\0\0\0\0\0\0\1@L\317\304\325\343\360o.Zg\276!m\213>*e/"..., 217, MSG_NOSIGNAL) = 217
poll([{fd=4, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 0 (Timeout)
poll([{fd=4, events=POLLIN}], 1, 1000)  = 1 ([{fd=4, revents=POLLIN}])
poll([{fd=4, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 1 ([{fd=4, revents=POLLIN|POLLRDNORM}])
recv(4, "\27\3\38r", 5, 0)              = 5
recv(4, "\232H\34\311}\23\302\204\206w\346c\372T-\327\201\263\350\206Y\4\221j\243i\335\337+3\246B"..., 14450, 0) = 7235
recv(4, "\324U\1t\211\274\346\16\314\343\260\16\242\2<A\276\3218\240\253=P!\275%2\332\24\224:\\"..., 7215, 0) = 1448
recv(4, ";\215\371\347\320\243\273\21~\311\23B\243/\372\3258$\316\275\271C\22-N-a\6a\276;s"..., 5767, 0) = 1448
recv(4, "\334\263\2143O\353\377\346\\n\37\0\331x\226\33\260\344\375\262\227\343\26\315^\345\221\10q\326\334\267"..., 4319, 0) = 2896
recv(4, "\r\335\273'\31J\323\305f\22\203Sq,i\275\203\360\274n*.\330\f\260\271\v%\314\nY\314"..., 1423, 0) = 1423
fstat64(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
write(3, "{\"lat\":53.4731,\"lon\":13.2316,\"ti"..., 12288) = 12288
write(3, "\"id\":800,\"main\":\"Clear\",\"descrip"..., 1761) = 1761
close(3)                                = 0
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0xd6ed9fc8} ---
+++ killed by SIGSEGV +++
Speicherzugriffsfehler


Solution

  • as not even the simplest example did work I tried on a different box. There it worked fine!

    I did apt --purge remove libcurl4-gnutls-dev libcurl3-gnutls and installed it again.

    Since then the program is running fine!

    Greetings

    Christian