[Commits] [SCM] claws branch, litehtml, updated. 3.17.0-153-g1b432d8

miras at claws-mail.org miras at claws-mail.org
Thu Nov 8 00:45:58 CET 2018


The branch, litehtml has been updated
       via  1b432d81b95f9d908900124cd8d7aab0b5273099 (commit)
      from  57ee5d67b1f1e8e58be3f80701eaf9dba882a58e (commit)

Summary of changes:
 src/plugins/litehtml_viewer/http.cpp |   46 +++++++++++++++++++++-------------
 src/plugins/litehtml_viewer/http.h   |    4 +--
 2 files changed, 30 insertions(+), 20 deletions(-)


- Log -----------------------------------------------------------------
commit 1b432d81b95f9d908900124cd8d7aab0b5273099
Author: Michael Rasmussen <mir at datanom.net>
Date:   Thu Nov 8 00:45:52 2018 +0100

    Cleaner code a fix memory leak
    
    Signed-off-by: Michael Rasmussen <mir at datanom.net>

diff --git a/src/plugins/litehtml_viewer/http.cpp b/src/plugins/litehtml_viewer/http.cpp
index 46882e1..2a8709a 100644
--- a/src/plugins/litehtml_viewer/http.cpp
+++ b/src/plugins/litehtml_viewer/http.cpp
@@ -1,5 +1,10 @@
 #include "http.h"
 
+struct Data {
+  char *memory;
+  size_t size;
+};
+
 http::http()
 {
     curl = curl_easy_init();
@@ -10,28 +15,30 @@ http::http()
     curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
     curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http::curl_write_data);
-    response_data = NULL;
-    response_size = 0;;
 }
 
 http::~http()
 {
     curl_easy_cleanup(curl);
-    if (response_data) {
-        g_free(response_data);
-    }
 }
 
-size_t http::curl_write_data(char* ptr, size_t size, size_t nmemb, void* data) {
-	if (!response_data)
-		response_data = (char *) malloc(size * nmemb);
-	else
-		response_data = (char *) realloc(response_data, response_size + size * nmemb);
-	if (response_data) {
-		memcpy(response_data + response_size, ptr, size * nmemb);
-		response_size += size * nmemb;
-	}
-	return size * nmemb;
+size_t http::curl_write_data(char* ptr, size_t size, size_t nmemb, void* data_ptr) {
+    struct Data* data = (struct Data *) data_ptr;
+    size_t realsize = size * nmemb;
+    
+    char *input = (char *) realloc(data->memory, data->size + realsize + 1);
+    if(ptr == NULL) {
+        /* out of memory! */
+        g_log(NULL, G_LOG_LEVEL_ERROR, "not enough memory (realloc returned NULL)");
+        return 0;
+    }
+    
+    data->memory = input;
+    memcpy(&(data->memory[data->size]), ptr, realsize);
+    data->size += realsize;
+    data->memory[data->size] = 0;
+    
+    return realsize;
 }
 
 void http::destroy_giostream(gpointer data) {
@@ -50,8 +57,11 @@ GInputStream *http::load_url(const gchar *url, GError **error)
 	gsize len;
 	gchar* content;
     GInputStream* stream = NULL;
+    struct Data data;
 
-
+    data.memory = (char *) malloc(1);
+    data.size = 0;
+    
 	if (!strncmp(url, "file:///", 8) || g_file_test(url, G_FILE_TEST_EXISTS)) {
 		gchar* newurl = g_filename_from_uri(url, NULL, NULL);
 		if (g_file_get_contents(newurl ? newurl : url, &content, &len, &_error)) {
@@ -64,11 +74,13 @@ GInputStream *http::load_url(const gchar *url, GError **error)
 		if (!curl) return NULL;
 	    curl_easy_setopt(curl, CURLOPT_URL, url);
 	    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_data);
+	    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data);
 	    res = curl_easy_perform(curl);
 	    if (res != CURLE_OK) {
 		    _error = g_error_new_literal(G_FILE_ERROR, res, curl_easy_strerror(res));
 	    } else {
-	        stream = g_memory_input_stream_new_from_data(response_data, response_size, http::destroy_giostream);
+	        stream = g_memory_input_stream_new_from_data(g_memdup(data.memory, data.size), data.size, http::destroy_giostream);
+	        g_free(data.memory);
 	    }
 	}
 
diff --git a/src/plugins/litehtml_viewer/http.h b/src/plugins/litehtml_viewer/http.h
index 4c4b03f..c8c3bee 100644
--- a/src/plugins/litehtml_viewer/http.h
+++ b/src/plugins/litehtml_viewer/http.h
@@ -11,8 +11,6 @@
 class http
 {
     CURL*           curl;
-    static gchar*   response_data;
-    static size_t   response_size;
 
 public:
     http();
@@ -21,7 +19,7 @@ public:
     GInputStream *load_url(const gchar *url, GError **error);
 
 private:
-    static size_t curl_write_data(char* ptr, size_t size, size_t nmemb, void* data);
+    static size_t curl_write_data(char* ptr, size_t size, size_t nmemb, void* data_ptr);
     static void destroy_giostream(gpointer data);
 };
 

-----------------------------------------------------------------------


hooks/post-receive
-- 
Claws Mail


More information about the Commits mailing list