Patchwork [v2,08/10] vnc: remove memory leaks in zlib and tight encoding

login
register
mail settings
Submitter Corentin Chary
Date May 18, 2010, 12:49 p.m.
Message ID <1274186986-26878-9-git-send-email-corentincj@iksaif.net>
Download mbox | patch
Permalink /patch/52877/
State New
Headers show

Comments

Corentin Chary - May 18, 2010, 12:49 p.m.
Free buffers used by tight and zlib encodings, lso introduce
a new helper to free buffers: buffer_free()

Signed-off-by: Corentin Chary <corentincj@iksaif.net>
---
 vnc-encoding-tight.c |   11 +++++++++++
 vnc-encoding-zlib.c  |    8 ++++++++
 vnc.c                |   23 ++++++++++++++---------
 vnc.h                |    4 +++-
 4 files changed, 36 insertions(+), 10 deletions(-)

Patch

diff --git a/vnc-encoding-tight.c b/vnc-encoding-tight.c
index 229927e..44984f9 100644
--- a/vnc-encoding-tight.c
+++ b/vnc-encoding-tight.c
@@ -499,6 +499,17 @@  static int find_large_solid_color_rect(VncState *vs, int x, int y,
     return n + send_rect_simple(vs, x, y, w, h);
 }
 
+void vnc_tight_clear(VncState *vs)
+{
+    int i;
+    for (i=0; i<ARRAY_SIZE(vs->tight_stream); i++)
+        if (vs->tight_stream[i].opaque)
+            deflateEnd(&vs->tight_stream[i]);
+
+    buffer_free(&vs->tight);
+    buffer_free(&vs->tight_zlib);
+}
+
 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
                                       int w, int h)
 {
diff --git a/vnc-encoding-zlib.c b/vnc-encoding-zlib.c
index 6b68540..e5a431e 100644
--- a/vnc-encoding-zlib.c
+++ b/vnc-encoding-zlib.c
@@ -143,3 +143,11 @@  int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
 
     return 1;
 }
+
+void vnc_zlib_clear(VncState *vs)
+{
+    if (vs->zlib_stream.opaque)
+        deflateEnd(&vs->zlib_stream);
+
+    buffer_free(&vs->zlib);
+}
diff --git a/vnc.c b/vnc.c
index f660c10..0dac736 100644
--- a/vnc.c
+++ b/vnc.c
@@ -503,7 +503,15 @@  uint8_t *buffer_end(Buffer *buffer)
 
 void buffer_reset(Buffer *buffer)
 {
-        buffer->offset = 0;
+    buffer->offset = 0;
+}
+
+void buffer_free(Buffer *buffer)
+{
+    qemu_free(buffer->buffer);
+    buffer->offset = 0;
+    buffer->capacity = 0;
+    buffer->buffer = NULL;
 }
 
 void buffer_append(Buffer *buffer, const void *data, size_t len)
@@ -922,17 +930,14 @@  static void vnc_disconnect_finish(VncState *vs)
 {
     vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED);
 
-    if (vs->input.buffer) {
-        qemu_free(vs->input.buffer);
-        vs->input.buffer = NULL;
-    }
-    if (vs->output.buffer) {
-        qemu_free(vs->output.buffer);
-        vs->output.buffer = NULL;
-    }
+    buffer_free(&vs->input);
+    buffer_free(&vs->output);
 
     qobject_decref(vs->info);
 
+    vnc_tight_clear(vs);
+    vnc_zlib_clear(vs);
+
 #ifdef CONFIG_VNC_TLS
     vnc_tls_client_cleanup(vs);
 #endif /* CONFIG_VNC_TLS */
diff --git a/vnc.h b/vnc.h
index ea6e70b..18a4e8b 100644
--- a/vnc.h
+++ b/vnc.h
@@ -383,7 +383,7 @@  int buffer_empty(Buffer *buffer);
 uint8_t *buffer_end(Buffer *buffer);
 void buffer_reset(Buffer *buffer);
 void buffer_append(Buffer *buffer, const void *data, size_t len);
-
+void buffer_free(Buffer *buffer);
 
 /* Misc helpers */
 
@@ -403,10 +403,12 @@  int vnc_hextile_send_framebuffer_update(VncState *vs, int x,
                                          int y, int w, int h);
 void vnc_hextile_set_pixel_conversion(VncState *vs, int generic);
 
+void vnc_zlib_clear(VncState *vs);
 void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size);
 void vnc_zlib_zfree(void *x, void *addr);
 int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
 
+void vnc_tight_clear(VncState *vs);
 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
 
 #endif /* __QEMU_VNC_H */