aliyun / aliyun-openapi-cpp-sdk

Alibaba Cloud SDK for C++
Other
99 stars 77 forks source link

Potential memory leak point in core/src/Utils.cc. #5

Closed gctalk closed 5 years ago

gctalk commented 6 years ago

Hi,

I found following part of code may cause memory leak , Is there anyone can help double check?

https://github.com/aliyun/aliyun-openapi-cpp-sdk/blob/master/core/src/Utils.cc:

std::string AlibabaCloud::UrlEncode(const std::string & src)
{
    CURL *curl = curl_easy_init();  
    char *output = curl_easy_escape(curl, src.c_str(), src.size());
    std::string result(output);
    curl_free(output);
    return result;
}

std::string AlibabaCloud::UrlDecode(const std::string & src)
{
    CURL *curl = curl_easy_init();
    int outlength = 0;
    char *output = curl_easy_unescape(curl, src.c_str(), src.size(), &outlength);
    std::string result(output, outlength);
    curl_free(output);
    return result;
}

UrlEncode and UrlDecode was call curl_easy_init to initial CURL object, but seems both of them didn't call curl_easy_cleanup to free it.

According https://curl.haxx.se/libcurl/c/curl_easy_init.html, curl_easy_init must be the first function to call, and it returns a CURL easy handle that you must use as input to other functions in the easy interface. This call MUST have a corresponding call to curl_easy_cleanup when the operation is complete.

fenglc commented 6 years ago

You are right!

zhangzifa commented 5 years ago

This is fixed in master fix memory leak in url encode and decode. I close this issue.