From c7b468455d888402ab1d9a5e0184f1c2eee26036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Skalick=C3=BD?= Date: Wed, 1 May 2024 18:09:12 +0200 Subject: [PATCH 1/4] fix memory leak --- sources/urn.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sources/urn.cpp b/sources/urn.cpp index 7ddbace..fea8c11 100644 --- a/sources/urn.cpp +++ b/sources/urn.cpp @@ -81,7 +81,9 @@ namespace WebDAV auto escape(void* request, const string& name) -> string { - string path = curl_easy_escape(request, name.c_str(), static_cast(name.length())); + char * escaped = curl_easy_escape(request, name.c_str(), static_cast(name.length())); // allocates new string + string path{escaped}; // this copies the string + free(escaped); return path; } From 0d1147120b8b7dd6fd78fee2c1ab24fb03902a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Skalick=C3=BD?= Date: Wed, 1 May 2024 18:14:30 +0200 Subject: [PATCH 2/4] Improve comments --- sources/urn.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/urn.cpp b/sources/urn.cpp index fea8c11..1d8d787 100644 --- a/sources/urn.cpp +++ b/sources/urn.cpp @@ -81,9 +81,9 @@ namespace WebDAV auto escape(void* request, const string& name) -> string { - char * escaped = curl_easy_escape(request, name.c_str(), static_cast(name.length())); // allocates new string - string path{escaped}; // this copies the string - free(escaped); + char * escaped = curl_easy_escape(request, name.c_str(), static_cast(name.length())); // allocates new memory + string path{escaped}; // this copies the data + free(escaped); // must free the memory return path; } From 7fbc55a6422d113079c1eaca4ba759af7cb5dccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Skalick=C3=BD?= Date: Wed, 1 May 2024 18:22:33 +0200 Subject: [PATCH 3/4] Fix another leak --- sources/client.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sources/client.cpp b/sources/client.cpp index 9f2e7b2..f32ae98 100644 --- a/sources/client.cpp +++ b/sources/client.cpp @@ -458,7 +458,9 @@ namespace WebDAV { pugi::xml_node href = response.node().select_node("*[local-name()='href']").node(); std::string encode_file_name = href.first_child().value(); - std::string resource_path = curl_unescape(encode_file_name.c_str(), static_cast(encode_file_name.length())); + char* unescaped = curl_unescape(encode_file_name.c_str(), static_cast(encode_file_name.length())); //this allocates new memory + std::string resource_path{unescaped}; // the data is copied + free(unescaped); auto target_path = target_urn.path(); auto target_path_without_sep = target_urn.path(); if (!target_path_without_sep.empty() && target_path_without_sep.back() == '/') From a1a88c33683b0bc57e2166f5660b9433f0e84527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Skalick=C3=BD?= Date: Wed, 1 May 2024 18:35:56 +0200 Subject: [PATCH 4/4] Use curl_free() instead of free() --- sources/client.cpp | 2 +- sources/urn.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/client.cpp b/sources/client.cpp index f32ae98..1ee1361 100644 --- a/sources/client.cpp +++ b/sources/client.cpp @@ -460,7 +460,7 @@ namespace WebDAV std::string encode_file_name = href.first_child().value(); char* unescaped = curl_unescape(encode_file_name.c_str(), static_cast(encode_file_name.length())); //this allocates new memory std::string resource_path{unescaped}; // the data is copied - free(unescaped); + curl_free(unescaped); auto target_path = target_urn.path(); auto target_path_without_sep = target_urn.path(); if (!target_path_without_sep.empty() && target_path_without_sep.back() == '/') diff --git a/sources/urn.cpp b/sources/urn.cpp index 1d8d787..7448ed6 100644 --- a/sources/urn.cpp +++ b/sources/urn.cpp @@ -83,7 +83,7 @@ namespace WebDAV { char * escaped = curl_easy_escape(request, name.c_str(), static_cast(name.length())); // allocates new memory string path{escaped}; // this copies the data - free(escaped); // must free the memory + curl_free(escaped); // must free the memory return path; }