P
P
Push Pull2014-04-17 21:09:26
C++ / C#
Push Pull, 2014-04-17 21:09:26

Beginner troubles or curl_easy_perform -> segfault

Prehistory: I don't have average knowledge in C either. By the will of fate, it so happened that I had to write a simple file loader in C. There are suspicions that I did not read the cUrl documentation correctly, but took an example from their documentation.

CURL *curl;
curl = curl_easy_init();
if(!curl) {
            fprintf(stderr, "curl init() failed: ");
            return token;
}
// используется библиотека cJSON для чтения конфига
const char * password = cJSON_GetObjectItem(service,"password")->valuestring;
const char * login = cJSON_GetObjectItem(service,"login")->valuestring;
const char * url = cJSON_GetObjectItem(service,"url")->valuestring;
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HEADER, "Content-Type: text/xml; charset=UTF-8");
// тут генерируется xml, читается файл с xml шаблоном запроса, логина и пароля заменяются в нужных местах
const char * xml = generate_xml(login, password);

curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, xml);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE , strlen(xml));
// хотелось бы конечно писать результат, но для начала хоть запрос бы выполнить
//curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
//curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
CURLcode res;
res = curl_easy_perform(curl);

printf("break!\n");
exit(0);

those. to "break!" does not reach. crashes with a Segmentation fault and well, at least some info, but no, keep the segfalt and think about what you did wrong.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Push Pull, 2014-04-18
@deadbyelpy

the problem was that the url was https, and without specifying the path to CURLOPT_CAINFO and setting CURLOPT_SSL_VERIFYHOST = 0, curl crashed with segfault

J
jcmvbkbc, 2014-04-18
@jcmvbkbc

to "break!" does not reach. crashes with a Segmentation fault and well, at least some info

Build the program with debug information and run it in gdb.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question