Skip to content

Commit 56c02da

Browse files
committed
Fixed several leaks in curl_easy
1 parent bb38816 commit 56c02da

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/curl_easy.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,20 @@ void curl_easy::perform() {
8282

8383
// Implementation of escape method.
8484
void curl_easy::escape(string &url) {
85-
char *url_encoded = curl_easy_escape(this->curl,url.c_str(),(int)url.length());
86-
if (url_encoded == nullptr) {
87-
throw curl_easy_exception("Null pointer intercepted",__FUNCTION__);
85+
std::unique_ptr<char, void(*)(char*)> url_encoded(curl_easy_escape(this->curl, url.c_str(), (int)url.length()), [](char *ptr) { curl_free(ptr); });
86+
if (!url_encoded) {
87+
throw curl_easy_exception("Null pointer intercepted", __FUNCTION__);
8888
}
89-
url = string(url_encoded);
90-
curl_free(url_encoded);
89+
url = string(url_encoded.get());
9190
}
9291

9392
// Implementation of unescape method.
9493
void curl_easy::unescape(string &url) {
95-
char *url_decoded = curl_easy_unescape(this->curl,url.c_str(),(int)url.length(),nullptr);
94+
std::unique_ptr<char,void(*)(char*)> url_decoded(curl_easy_unescape(this->curl,url.c_str(),(int)url.length(),nullptr),[](char *ptr) { curl_free(ptr); });
9695
if (url_decoded == nullptr) {
9796
throw curl_easy_exception("Null pointer intercepted",__FUNCTION__);
9897
}
99-
url = string(url_decoded);
100-
curl_free(url_decoded);
98+
url = string(url_decoded.get());
10199
}
102100

103101
// Implementation of reset method.

test/easy.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ int main() {
1414
// Add some options.
1515
easy.add<CURLOPT_URL>("http://www.google.it");
1616
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
17+
easy.add<CURLOPT_HTTP_VERSION>(2);
1718
try {
1819
// Execute the request.
1920
easy.perform();

0 commit comments

Comments
 (0)