From 8b049922d7b06705783c743585f01988b4052449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20D=C3=ADaz=20M=C3=A1s?= Date: Tue, 28 Nov 2017 17:32:42 +0100 Subject: [PATCH] Change exiv2::urlencode signature to return std::string The goal of this change is to remove the responsibility from the client code to free the memory of the returned string. --- include/exiv2/futils.hpp | 2 +- src/basicio.cpp | 6 ++---- src/futils.cpp | 8 +++++--- unitTests/test_futils.cpp | 5 ++--- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/exiv2/futils.hpp b/include/exiv2/futils.hpp index 344f588f..5a322fc9 100644 --- a/include/exiv2/futils.hpp +++ b/include/exiv2/futils.hpp @@ -78,7 +78,7 @@ namespace Exiv2 { @note Be sure to free() the returned string after use Source: http://www.geekhideout.com/urlcode.shtml */ - EXIV2API char* urlencode(char* str); + EXIV2API std::string urlencode(char *str); /*! @brief Decode the input url. @param str The url needs decoding. diff --git a/src/basicio.cpp b/src/basicio.cpp index 798e3885..f6e846f4 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -2045,7 +2045,7 @@ namespace Exiv2 { char* encodeData = new char[encodeLength]; base64encode(data, size, encodeData, encodeLength); // url encode - char* urlencodeData = urlencode(encodeData); + const std::string urlencodeData = urlencode(encodeData); delete[] encodeData; std::stringstream ss; @@ -2054,7 +2054,6 @@ namespace Exiv2 { << "to=" << to << "&" << "data=" << urlencodeData; std::string postData = ss.str(); - delete[] urlencodeData; // create the header ss.str(""); @@ -2268,7 +2267,7 @@ namespace Exiv2 { char* encodeData = new char[encodeLength]; base64encode(data, size, encodeData, encodeLength); // url encode - char* urlencodeData = urlencode(encodeData); + const std::string urlencodeData = urlencode(encodeData); delete[] encodeData; std::stringstream ss; ss << "path=" << hostInfo.Path << "&" @@ -2276,7 +2275,6 @@ namespace Exiv2 { << "to=" << to << "&" << "data=" << urlencodeData; std::string postData = ss.str(); - delete[] urlencodeData; curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, postData.c_str()); // Perform the request, res will get the return code. diff --git a/src/futils.cpp b/src/futils.cpp index f0f9b3a1..8fe1810b 100644 --- a/src/futils.cpp +++ b/src/futils.cpp @@ -71,7 +71,7 @@ namespace Exiv2 { return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; } // from_hex - char* urlencode(char* str) { + std::string urlencode(char* str) { char* pstr = str; char* buf = (char*)malloc(strlen(str) * 3 + 1); char* pbuf = buf; @@ -85,8 +85,10 @@ namespace Exiv2 { pstr++; } *pbuf = '\0'; - return buf; - } // urlencode + std::string ret(buf); + free(buf); + return ret; + } char* urldecode(const char* str) { const char* pstr = str; diff --git a/unitTests/test_futils.cpp b/unitTests/test_futils.cpp index c0aa7dbd..4279d112 100644 --- a/unitTests/test_futils.cpp +++ b/unitTests/test_futils.cpp @@ -56,7 +56,6 @@ TEST(getEnv, throwsWhenKeyDoesNotExist) TEST(urlencode, encodesGivenUrl) { - char *url = urlencode("http://www.geekhideout.com/urlcode.shtml"); - ASSERT_STREQ("http%3a%2f%2fwww.geekhideout.com%2furlcode.shtml", url); - free(url); + const std::string url = urlencode("http://www.geekhideout.com/urlcode.shtml"); + ASSERT_STREQ("http%3a%2f%2fwww.geekhideout.com%2furlcode.shtml", url.c_str()); }