[pve-devel] [PATCH spiceterm] fix memory leaks when using g_hashtable

Dominik Csapak d.csapak at proxmox.com
Fri May 26 14:28:12 CEST 2017


when generating a bitmap for a character whose codepoint was above 255,
we used a cache_id of 0, malloc'd a CachedImage and inserted it into a
g_hashtable, without freeing the one which was before inserted with cache_id 0

this is circumvented by only generating a CachedImage when having
a cache_id != 0

the second leak was also with inserting into a hashtable, but there we
give the hashtable the g_free method as a value_destroy_func

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 input.c  |  4 ++--
 screen.c | 11 +++++++----
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/input.c b/input.c
index 7fb5238..5ee82de 100644
--- a/input.c
+++ b/input.c
@@ -837,8 +837,8 @@ spiceterm_create(uint32_t width, uint32_t height, SpiceTermOptions *opts)
     SpiceCoreInterface *core = basic_event_loop_init();
     SpiceScreen *spice_screen = spice_screen_new(core, width, height, opts);
 
-    keymap = g_hash_table_new(g_int_hash, g_int_equal);
-    
+    keymap = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, g_free);
+
     if (!parse_keymap(opts->keymap ?  opts->keymap : "en-us")) {
         return NULL;
     }
diff --git a/screen.c b/screen.c
index 1fe28a5..a24fd6a 100644
--- a/screen.c
+++ b/screen.c
@@ -297,10 +297,13 @@ spice_screen_draw_char_cmd(SpiceScreen *spice_screen, int x, int y, int c,
                 dst += 4;
             }
         }
-        ce = g_new(CachedImage, 1);
-        ce->cache_id = cache_id;
-        ce->bitmap = bitmap;
-        g_hash_table_insert(spice_screen->image_cache, &ce->cache_id, ce);
+
+	if (cache_id != 0) {
+	    ce = g_new(CachedImage, 1);
+	    ce->cache_id = cache_id;
+	    ce->bitmap = bitmap;
+	    g_hash_table_insert(spice_screen->image_cache, &ce->cache_id, ce);
+	}
     }
 
     bbox.left = left; bbox.top = top;
-- 
2.11.0





More information about the pve-devel mailing list