简体   繁体   English

curl_easy_perform在Linux上的分段错误

[英]curl_easy_perform segmentation fault on Linux

I am working on a c++ app which uses curl in order to an HTTP GET request. 我正在使用使用curl来响应HTTP GET请求的C ++应用程序。 It works perfectly fine on Windows but on Linux (CentOS 7) I get a segmentation fault when curl_easy_cleanup is used. 它在Windows上运行良好,但在Linux(CentOS 7)上,使用curl_easy_cleanup时出现分段错误。

Below is my code 下面是我的代码

CURL* curl = NULL;
    curl = curl_easy_init();

    if (curl == NULL)
    {

        this->getBitsLibrary()->writeToLog("Failed to init curl");
        this->getBitsLibrary()->setAlarm("CrashInfo", Alarms::AlarmLevel::Critical, "Failed to init curl for IP lookup");
        return;
    }

    stringstream urlstream;
    string iplookupURL;
    //curlResponse = "";
    urlstream << StaticSettings::IP_Lookup::ipLookupURL << "/" << clientIP << "?access_key="<<StaticSettings::IP_Lookup::api_key;
    iplookupURL = urlstream.str();
    logstream << "Using URL " << iplookupURL << " for IP lookup";
    this->getBitsLibrary()->writeToLog(logstream.str(), "CrashInfo", "performIPLookup");
    logstream.clear();
    logstream.str(string());
    //string response = "";
    this->curlResponse = new string();
    curl_easy_setopt(curl, CURLOPT_URL, iplookupURL.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)this);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlStaticCallback);



    CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
    {
        logstream << "Failed to perform IP lookup using curl";
        this->getBitsLibrary()->writeToLog(logstream.str(), "CrashInfo", "performIPLookup");
        this->getBitsLibrary()->setAlarm("CrashInfo", Alarms::AlarmLevel::Warning, "Failed to perform IP Lookup. Curl error");
        curl_easy_cleanup(curl);
        return;
    }

    int httpResponseCode = 0;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpResponseCode);
    if (httpResponseCode == 200)
    {

        if (!this->curlResponse->empty())
        {
            this->getBitsLibrary()->writeToLog("Processing response for IP lookup");
            rapidjson::Document jsonObject;
            jsonObject.Parse(this->curlResponse->c_str());

            const rapidjson::Value& countryValue = jsonObject["country_name"];
            if (!countryValue.IsNull())
            {
                string country = string(jsonObject["country_name"].GetString());
                this->setCountry(!country.empty() ? country : "N/A");
            }
            else
            {
                this->setCountry("N/A");
            }

            const rapidjson::Value& cityValue = jsonObject["city"];
            if (!cityValue.IsNull())
            {
                string city = string(jsonObject["city"].GetString());
                this->setCity(!city.empty() ? city : "N/A");
            }
            else
            {
                this->setCity("N/A");
            }

            this->getBitsLibrary()->writeToLog("IPLookup performed successfully", "CrashInfo", "performIPLookup");
        }
delete this->curlResponse;
    this->curlResponse = NULL;
    curl_easy_cleanup(curl);

Due the curl being a C library, I've created a static function (outside of the C++ class) as follows: 由于curl是C库,因此我创建了一个静态函数(在C ++类之外),如下所示:

size_t curlStaticCallback(void *contents, size_t size, size_t nmemb, void * userp)
{
    CrashInfo *crashInfo = (CrashInfo*)userp;
    crashInfo->curlResponseWriteCallback(contents, size, nmemb, userp);
    return size * nmemb;
}

The above static function called by the WRITEFUNCTION in curl. 上面的静态函数由curl中的WRITEFUNCTION调用。 I get the class object and then call the C++ function which is as follows: 我得到了类对象,然后调用C ++函数,如下所示:

size_t CrashInfo::curlResponseWriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    curlResponse->append((char*)contents, size * nmemb);
    return size * nmemb;
}

As mentioned above, this works perfectly fine on Windows but on Linux it works fine where I get the expected response the URL, but when curl_easy_cleanup is called I then get a segmentation fault which contains the following stack: 如上所述,这在Windows上可以正常运行,但是在Linux上,可以在获得预期响应的URL上正常运行,但是当curl_easy_cleanup被调用时,我得到了包含以下堆栈的分段错误:

#0  0x00007fe7f45ff2ad in ?? () from /lib64/libstdc++.so.6
#1  0x00007fe7f4661c03 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () from /lib64/libstdc++.so.6
#2  0x000000000043f773 in WebManager::CrashInfo::performIPLookup (this=0x7fe7d00020d0, clientIP="192.168.1.96") at CrashInfo.cpp:732
#3  0x000000000043aeb0 in WebManager::CrashInfo::processCrashInfo (this=0x7fe7d00020d0, httpRequest=0x7fe7d00008f0) at CrashInfo.cpp:308
#4  0x00000000004396ee in WebManager::CrashInfo::CrashInfo (this=0x7fe7d00020d0, httpRequest=0x7fe7d00008f0) at CrashInfo.cpp:50
#5  0x000000000046ce23 in WebManager::WebProcessor::processWebSocketData (this=0x7fe7ed841a60, clientpointer=0x7fe7e40008c0, ipAddress=0x7fe7ed8426b8 "192.168.1.96") at WebProcessor.cpp:71
#6  0x0000000000473bd6 in std::_Mem_fn<void (WebManager::WebProcessor::*)(void*, char*)>::operator()<int*, char*, void>(WebManager::WebProcessor*, int*&&, char*&&) const (this=0x7fe7e4000948, __object=0x7fe7ed841a60)
    at /usr/include/c++/4.8.2/functional:601
#7  0x00000000004739fd in std::_Bind_simple<std::_Mem_fn<void (WebManager::WebProcessor::*)(void*, char*)> (WebManager::WebProcessor*, int*, char*)>::_M_invoke<0ul, 1ul, 2ul>(std::_Index_tuple<0ul, 1ul, 2ul>) (this=0x7fe7e4000930)
    at /usr/include/c++/4.8.2/functional:1732
#8  0x00000000004737fd in std::_Bind_simple<std::_Mem_fn<void (WebManager::WebProcessor::*)(void*, char*)> (WebManager::WebProcessor*, int*, char*)>::operator()() (this=0x7fe7e4000930) at /usr/include/c++/4.8.2/functional:1720
#9  0x000000000047372e in std::thread::_Impl<std::_Bind_simple<std::_Mem_fn<void (WebManager::WebProcessor::*)(void*, char*)> (WebManager::WebProcessor*, int*, char*)> >::_M_run() (this=0x7fe7e4000918)
    at /usr/include/c++/4.8.2/thread:115
#10 0x00007fe7f4659070 in ?? () from /lib64/libstdc++.so.6
#11 0x00007fe7f3aaae25 in start_thread () from /lib64/libpthread.so.0
#12 0x00007fe7f3dbdbad in clone () from /lib64/libc.so.6

I've stepped through in GDB watching the memory location for Curl to see if something unexpectedly rights/corrupts to it but it doesn't look like there's any issues. 我已经在GDB中逐步检查了Curl的内存位置,以查看是否有意外的权限/损坏了它,但看起来没有任何问题。

I've figured the issues, it was rather odd one. 我已经弄清楚了问题,这很奇怪。 As Michael Doubez said in the comments it wasn't directly curl. 正如迈克尔·杜贝兹(Michael Doubez)在评论中说的那样,它并不是直接卷曲的。 I thought it was curl because the 2nd line in the stack track was pointing to the curl_easy_cleanup and then the top line was the string destructor so thought it was curl. 我以为是卷曲的,因为堆栈轨道中的第二行指向curl_easy_cleanup,然后最上面的一行是字符串析构函数,因此认为它是卷曲的。

I put a log line after the curl_easy_cleanup and it logged successfully and then crashed. 我在curl_easy_cleanup之后放了一条日志行,它成功记录了日志,然后崩溃了。 The curl_easy_cleanup was the last call in the method and it was when the function finished the crash happened. curl_easy_cleanup是方法中的最后一次调用,它是函数完成崩溃时发生的。

I ended up commenting out the entire function and then slowly re-add chunks of it to nail down at what point it caused it to crash. 我最终注释掉了整个功能,然后慢慢地重新添加了它的大块,以明确导致它崩溃的那一点。

It ended up being the line curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpResponseCode); 最后是curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpResponseCode); . I was passing in a pointer to an int when the function wants a pointer to a long. 当函数需要一个指向long的指针时,我正在传递一个指向int的指针。

I corrected the above and now everything works as expected, not entirely sure why though this didn't 1, raise a compiler warning that was I passing in the wrong type, and 2, why the stack trace was showing a string being destructed. 我纠正了上面的问题,现在一切都按预期进行了,不能完全确定为什么虽然这样做没有做到以下几点:1,引发编译器警告,即我传递了错误的类型; 2,为什么堆栈跟踪显示了被破坏的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM