libfreerdp-core: fix multiple memory leaks detected by valgrind
authorMarc-André Moreau <marcandre.moreau@gmail.com>
Wed, 1 Feb 2012 23:42:20 +0000 (18:42 -0500)
committerMarc-André Moreau <marcandre.moreau@gmail.com>
Wed, 1 Feb 2012 23:42:20 +0000 (18:42 -0500)
19 files changed:
channels/cliprdr/cliprdr_format.c
channels/cliprdr/cliprdr_main.c
client/X11/xfreerdp.c
include/freerdp/codec/color.h
include/freerdp/gdi/dc.h
libfreerdp-cache/bitmap.c
libfreerdp-cache/brush.c
libfreerdp-cache/glyph.c
libfreerdp-cache/pointer.c
libfreerdp-codec/color.c
libfreerdp-core/certificate.c
libfreerdp-core/connection.c
libfreerdp-core/credssp.c
libfreerdp-core/freerdp.c
libfreerdp-core/graphics.c
libfreerdp-core/license.c
libfreerdp-core/mppc.c
libfreerdp-core/settings.c
libfreerdp-gdi/dc.c

index 4cf0ae2..c00503c 100644 (file)
@@ -91,6 +91,7 @@ void cliprdr_process_format_list_event(cliprdrPlugin* cliprdr, RDP_CB_FORMAT_LIS
                                
                s = cliprdr_packet_new(CB_FORMAT_LIST, 0, stream_get_size(body));
                stream_write(s, stream_get_head(body), stream_get_size(body));
+               stream_free(body);
        }
 
        cliprdr_packet_send(cliprdr, s);
index 6a2fc99..a2f5a79 100644 (file)
@@ -263,6 +263,11 @@ static void cliprdr_process_event(rdpSvcPlugin* plugin, RDP_EVENT* event)
 
 static void cliprdr_process_terminate(rdpSvcPlugin* plugin)
 {
+       cliprdrPlugin* cliprdr_plugin = (cliprdrPlugin*) plugin;
+
+       if (cliprdr_plugin->uniconv != NULL)
+               freerdp_uniconv_free(cliprdr_plugin->uniconv);
+
        xfree(plugin);
 }
 
index 0cc6eba..1d14774 100644 (file)
@@ -547,11 +547,7 @@ boolean xf_pre_connect(freerdp* instance)
 
        xf_kbd_init(xfi);
 
-       xfi->clrconv = xnew(CLRCONV);
-       xfi->clrconv->alpha = true;
-       xfi->clrconv->invert = false;
-       xfi->clrconv->rgb555 = false;
-       xfi->clrconv->palette = xnew(rdpPalette);
+       xfi->clrconv = freerdp_clrconv_new(CLRCONV_ALPHA);
 
        instance->context->cache = cache_new(instance->settings);
 
@@ -647,21 +643,7 @@ boolean xf_post_connect(freerdp* instance)
                xfi->srcBpp = instance->settings->color_depth;
                xf_gdi_register_update_callbacks(instance->update);
 
-               xfi->hdc = gdi_GetDC();
-               xfi->hdc->bitsPerPixel = xfi->bpp;
-               xfi->hdc->bytesPerPixel = xfi->bpp / 8;
-
-               xfi->hdc->alpha = xfi->clrconv->alpha;
-               xfi->hdc->invert = xfi->clrconv->invert;
-               xfi->hdc->rgb555 = xfi->clrconv->rgb555;
-
-               xfi->hdc->hwnd = (HGDI_WND) malloc(sizeof(GDI_WND));
-               xfi->hdc->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0);
-               xfi->hdc->hwnd->invalid->null = 1;
-
-               xfi->hdc->hwnd->count = 32;
-               xfi->hdc->hwnd->cinvalid = (HGDI_RGN) malloc(sizeof(GDI_RGN) * xfi->hdc->hwnd->count);
-               xfi->hdc->hwnd->ninvalid = 0;
+               xfi->hdc = gdi_CreateDC(xfi->clrconv, xfi->bpp);
 
                xfi->primary_buffer = (uint8*) xzalloc(xfi->width * xfi->height * xfi->bpp);
 
@@ -922,8 +904,9 @@ void xf_window_free(xfInfo* xfi)
                rfx_context_free(xfi->rfx_context);
                xfi->rfx_context = NULL;
        }
-       
-       xfree(xfi->clrconv);
+
+       freerdp_clrconv_free(xfi->clrconv);
+       gdi_DeleteDC(xfi->hdc);
 
        xf_tsmf_uninit(xfi);
        xf_cliprdr_uninit(xfi);
@@ -932,6 +915,10 @@ void xf_window_free(xfInfo* xfi)
 void xf_free(xfInfo* xfi)
 {
        xf_window_free(xfi);
+
+       if (xfi->bmp_codec_none != NULL)
+               xfree(xfi->bmp_codec_none);
+
        XCloseDisplay(xfi->display);
        xfree(xfi);
 }
index d9e7c60..14f3058 100644 (file)
@@ -254,6 +254,9 @@ FREERDP_API uint32 freerdp_color_convert_bgr_rgb(uint32 srcColor, int srcBpp, in
 FREERDP_API uint32 freerdp_color_convert_var_rgb(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
 FREERDP_API uint32 freerdp_color_convert_var_bgr(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
 
+FREERDP_API HCLRCONV freerdp_clrconv_new(uint32 flags);
+FREERDP_API void freerdp_clrconv_free(HCLRCONV clrconv);
+
 #ifdef __cplusplus
 }
 #endif
index d95f266..83fa115 100644 (file)
@@ -24,6 +24,7 @@
 #include <freerdp/gdi/gdi.h>
 
 FREERDP_API HGDI_DC gdi_GetDC();
+FREERDP_API HGDI_DC gdi_CreateDC(HCLRCONV clrconv, int bpp);
 FREERDP_API HGDI_DC gdi_CreateCompatibleDC(HGDI_DC hdc);
 FREERDP_API HGDIOBJECT gdi_SelectObject(HGDI_DC hdc, HGDIOBJECT hgdiobject);
 FREERDP_API int gdi_DeleteObject(HGDIOBJECT hgdiobject);
index 6d45e31..652a391 100644 (file)
@@ -274,7 +274,7 @@ void bitmap_cache_free(rdpBitmapCache* bitmap_cache)
                }
 
                if (bitmap_cache->bitmap != NULL)
-                       bitmap_cache->bitmap->Free(bitmap_cache->context, bitmap_cache->bitmap);
+                       Bitmap_Free(bitmap_cache->context, bitmap_cache->bitmap);
 
                xfree(bitmap_cache->cells);
                xfree(bitmap_cache);
index dabf9c0..237a63e 100644 (file)
@@ -142,10 +142,32 @@ rdpBrushCache* brush_cache_new(rdpSettings* settings)
 
 void brush_cache_free(rdpBrushCache* brush)
 {
+       int i;
+
        if (brush != NULL)
        {
-               xfree(brush->entries);
-               xfree(brush->monoEntries);
+               if (brush->entries != NULL)
+               {
+                       for (i = 0; i < brush->maxEntries; i++)
+                       {
+                               if (brush->entries[i].entry != NULL)
+                                       xfree(brush->entries[i].entry);
+                       }
+
+                       xfree(brush->entries);
+               }
+
+               if (brush->monoEntries != NULL)
+               {
+                       for (i = 0; i < brush->maxMonoEntries; i++)
+                       {
+                               if (brush->monoEntries[i].entry != NULL)
+                                       xfree(brush->monoEntries[i].entry);
+                       }
+
+                       xfree(brush->monoEntries);
+               }
+
                xfree(brush);
        }
 }
index f03415d..fc33266 100644 (file)
@@ -318,6 +318,9 @@ void update_gdi_cache_glyph(rdpContext* context, CACHE_GLYPH_ORDER* cache_glyph)
                Glyph_New(context, glyph);
 
                glyph_cache_put(cache->glyph, cache_glyph->cacheId, glyph_data->cacheIndex, glyph);
+
+               cache_glyph->glyphData[i] = NULL;
+               xfree(glyph_data);
        }
 }
 
@@ -343,6 +346,9 @@ void update_gdi_cache_glyph_v2(rdpContext* context, CACHE_GLYPH_V2_ORDER* cache_
                Glyph_New(context, glyph);
 
                glyph_cache_put(cache->glyph, cache_glyph_v2->cacheId, glyph_data->cacheIndex, glyph);
+
+               cache_glyph_v2->glyphData[i] = NULL;
+               xfree(glyph_data);
        }
 }
 
@@ -484,6 +490,7 @@ void glyph_cache_free(rdpGlyphCache* glyph_cache)
                                rdpGlyph* glyph;
 
                                glyph = glyph_cache->glyphCache[i].entries[j];
+
                                if (glyph != NULL)
                                {
                                        Glyph_Free(glyph_cache->context, glyph);
index d731e54..b34b97f 100644 (file)
@@ -139,17 +139,7 @@ void pointer_cache_free(rdpPointerCache* pointer_cache)
                        pointer = pointer_cache->entries[i];
 
                        if (pointer != NULL)
-                       {
-                               pointer->Free(pointer_cache->update->context, pointer);
-
-                               if (pointer->xorMaskData != NULL)
-                                       xfree(pointer->xorMaskData);
-
-                               if (pointer->andMaskData != NULL)
-                                       xfree(pointer->andMaskData);
-
-                               xfree(pointer);
-                       }
+                               Pointer_Free(pointer_cache->update->context, pointer);
                }
 
                xfree(pointer_cache->entries);
index 1ba42f7..97187f8 100644 (file)
@@ -1036,3 +1036,26 @@ void freerdp_image_swap_color_order(uint8* data, int width, int height)
                }
        }
 }
+
+HCLRCONV freerdp_clrconv_new(uint32 flags)
+{
+       HCLRCONV clrconv = xnew(CLRCONV);
+
+       clrconv->alpha = (flags & CLRCONV_ALPHA) ? true : false;
+       clrconv->invert = (flags & CLRCONV_INVERT) ? true : false;
+       clrconv->rgb555 = (flags & CLRCONV_RGB555) ? true : false;
+       clrconv->palette = xnew(rdpPalette);
+
+       return clrconv;
+}
+
+void freerdp_clrconv_free(HCLRCONV clrconv)
+{
+       if (clrconv != NULL)
+       {
+               if (clrconv->palette != NULL)
+                       xfree(clrconv->palette);
+
+               xfree(clrconv);
+       }
+}
index 32574ee..44062e6 100644 (file)
@@ -402,6 +402,7 @@ boolean certificate_read_server_x509_certificate_chain(rdpCertificate* certifica
                        DEBUG_CERTIFICATE("License Server Certificate");
                        certificate_read_x509_certificate(&certificate->x509_cert_chain->array[i], &cert_info);
                        DEBUG_LICENSE("modulus length:%d", cert_info.modulus.length);
+                       freerdp_blob_free(&cert_info.modulus);
                }
                else if (numCertBlobs - i == 1)
                {
@@ -486,6 +487,10 @@ void certificate_free(rdpCertificate* certificate)
        if (certificate != NULL)
        {
                certificate_free_x509_certificate_chain(certificate->x509_cert_chain);
+
+               if (certificate->cert_info.modulus.data != NULL)
+                       freerdp_blob_free(&(certificate->cert_info.modulus));
+
                xfree(certificate);
        }
 }
index 0cf06df..5de0a5d 100644 (file)
@@ -149,30 +149,30 @@ boolean rdp_client_redirect(rdpRdp* rdp)
                if (redirection->flags & LB_TARGET_NET_ADDRESS)
                {
                        xfree(settings->hostname);
-                       settings->hostname = redirection->targetNetAddress.ascii;
+                       settings->hostname = xstrdup(redirection->targetNetAddress.ascii);
                }
                else if (redirection->flags & LB_TARGET_FQDN)
                {
                        xfree(settings->hostname);
-                       settings->hostname = redirection->targetFQDN.ascii;
+                       settings->hostname = xstrdup(redirection->targetFQDN.ascii);
                }
                else if (redirection->flags & LB_TARGET_NETBIOS_NAME)
                {
                        xfree(settings->hostname);
-                       settings->hostname = redirection->targetNetBiosName.ascii;
+                       settings->hostname = xstrdup(redirection->targetNetBiosName.ascii);
                }
        }
 
        if (redirection->flags & LB_USERNAME)
        {
                xfree(settings->username);
-               settings->username = redirection->username.ascii;
+               settings->username = xstrdup(redirection->username.ascii);
        }
 
        if (redirection->flags & LB_DOMAIN)
        {
                xfree(settings->domain);
-               settings->domain = redirection->domain.ascii;
+               settings->domain = xstrdup(redirection->domain.ascii);
        }
 
        if (redirection->flags & LB_PASSWORD)
index c57b78f..d8f136d 100644 (file)
@@ -188,6 +188,7 @@ int credssp_authenticate(rdpCredssp* credssp)
        credssp->negoToken.length = stream_get_length(s);
        credssp_encrypt_public_key(credssp, &credssp->pubKeyAuth);
        credssp_send(credssp, &credssp->negoToken, NULL, &credssp->pubKeyAuth);
+       freerdp_blob_free(&credssp->pubKeyAuth);
 
        /* Encrypted Public Key +1 */
        if (credssp_recv(credssp, &credssp->negoToken, NULL, &credssp->pubKeyAuth) < 0)
@@ -206,6 +207,7 @@ int credssp_authenticate(rdpCredssp* credssp)
        credssp_encode_ts_credentials(credssp);
        credssp_encrypt_ts_credentials(credssp, &credssp->authInfo);
        credssp_send(credssp, NULL, &credssp->authInfo, NULL);
+       freerdp_blob_free(&credssp->authInfo);
 
        xfree(s);
 
@@ -528,6 +530,7 @@ void credssp_send(rdpCredssp* credssp, rdpBlob* negoToken, rdpBlob* authInfo, rd
        }
 
        transport_write(credssp->transport, s);
+       stream_free(s);
 }
 
 /**
index a78f83a..fbf2494 100644 (file)
@@ -187,6 +187,10 @@ void freerdp_context_new(freerdp* instance)
 void freerdp_context_free(freerdp* instance)
 {
        IFCALL(instance->ContextFree, instance, instance->context);
+
+       rdp_free(instance->context->rdp);
+       graphics_free(instance->context->graphics);
+       xfree(instance->context);
 }
 
 uint32 freerdp_error_info(freerdp* instance)
@@ -213,7 +217,7 @@ void freerdp_free(freerdp* freerdp)
 {
        if (freerdp)
        {
-               rdp_free(freerdp->context->rdp);
+               freerdp_context_free(freerdp);
                xfree(freerdp);
        }
 }
index 1950479..f6b3bab 100644 (file)
@@ -108,7 +108,18 @@ void Pointer_New(rdpContext* context, rdpPointer* pointer)
 
 void Pointer_Free(rdpContext* context, rdpPointer* pointer)
 {
+       if (pointer != NULL)
+       {
+               pointer->Free(context, pointer);
+
+               if (pointer->xorMaskData)
+                       xfree(pointer->xorMaskData);
 
+               if (pointer->andMaskData)
+                       xfree(pointer->andMaskData);
+
+               xfree(pointer);
+       }
 }
 
 /* static method */
@@ -207,6 +218,7 @@ void graphics_free(rdpGraphics* graphics)
        {
                xfree(graphics->Bitmap_Prototype);
                xfree(graphics->Pointer_Prototype);
+               xfree(graphics->Glyph_Prototype);
                xfree(graphics);
        }
 }
index 830043c..60b9f93 100644 (file)
@@ -832,6 +832,7 @@ void license_send_platform_challenge_response_packet(rdpLicense* license)
        buffer = (uint8*) xmalloc(HWID_LENGTH);
        rc4 = crypto_rc4_init(license->licensing_encryption_key, LICENSING_ENCRYPTION_KEY_LENGTH);
        crypto_rc4(rc4, HWID_LENGTH, license->hwid, buffer);
+       crypto_rc4_free(rc4);
 
 #ifdef WITH_DEBUG_LICENSE
        printf("Licensing Encryption Key:\n");
index 14f7d89..8e66eb3 100644 (file)
@@ -1239,15 +1239,17 @@ struct rdp_mppc* mppc_new(rdpRdp* rdp)
 {
        struct rdp_mppc* ptr;
 
-       ptr = (struct rdp_mppc *) xmalloc(sizeof (struct rdp_mppc));
+       ptr = (struct rdp_mppc*) xmalloc(sizeof(struct rdp_mppc));
+
        if (!ptr)
        {
                printf("mppc_new(): system out of memory\n");
                return NULL;
        }
 
-       ptr->history_buf = (uint8 *) xmalloc(RDP6_HISTORY_BUF_SIZE);
-       ptr->offset_cache = (uint16 *) xzalloc(RDP6_OFFSET_CACHE_SIZE);
+       ptr->history_buf = (uint8*) xmalloc(RDP6_HISTORY_BUF_SIZE);
+       ptr->offset_cache = (uint16*) xzalloc(RDP6_OFFSET_CACHE_SIZE);
+
        if (!ptr->history_buf)
        {
                printf("mppc_new(): system out of memory\n");
@@ -1257,6 +1259,7 @@ struct rdp_mppc* mppc_new(rdpRdp* rdp)
 
        ptr->history_ptr = ptr->history_buf;
        ptr->history_buf_end = ptr->history_buf + RDP6_HISTORY_BUF_SIZE - 1;
+
        return ptr;
 }
 
@@ -1279,5 +1282,11 @@ void mppc_free(rdpRdp* rdp)
                rdp->mppc->history_buf = NULL;
                rdp->mppc->history_ptr = NULL;
        }
+
+       if (rdp->mppc->offset_cache)
+       {
+               xfree(rdp->mppc->offset_cache);
+       }
+
        xfree(rdp->mppc);
 }
index fbac191..dbafa5b 100644 (file)
@@ -209,6 +209,9 @@ void settings_free(rdpSettings* settings)
                xfree(settings->glyphCache);
                xfree(settings->fragCache);
                key_free(settings->server_key);
+               xfree(settings->config_path);
+               xfree(settings->current_path);
+               xfree(settings->development_path);
                xfree(settings);
        }
 }
index f433c4c..b4f4ced 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <freerdp/freerdp.h>
 #include <freerdp/gdi/gdi.h>
+#include <freerdp/codec/color.h>
 
 #include <freerdp/gdi/region.h>
 
@@ -48,6 +49,39 @@ HGDI_DC gdi_GetDC()
 }
 
 /**
+ * Create a device context.\n
+ * @msdn{dd144871}
+ * @return new device context
+ */
+
+HGDI_DC gdi_CreateDC(HCLRCONV clrconv, int bpp)
+{
+       HGDI_DC hDC = (HGDI_DC) malloc(sizeof(GDI_DC));
+
+       hDC->drawMode = GDI_R2_BLACK;
+       hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0);
+       hDC->clip->null = 1;
+       hDC->hwnd = NULL;
+
+       hDC->bitsPerPixel = bpp;
+       hDC->bytesPerPixel = bpp / 8;
+
+       hDC->alpha = clrconv->alpha;
+       hDC->invert = clrconv->invert;
+       hDC->rgb555 = clrconv->rgb555;
+
+       hDC->hwnd = (HGDI_WND) malloc(sizeof(GDI_WND));
+       hDC->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0);
+       hDC->hwnd->invalid->null = 1;
+
+       hDC->hwnd->count = 32;
+       hDC->hwnd->cinvalid = (HGDI_RGN) malloc(sizeof(GDI_RGN) * hDC->hwnd->count);
+       hDC->hwnd->ninvalid = 0;
+
+       return hDC;
+}
+
+/**
  * Create a new device context compatible with the given device context.\n
  * @msdn{dd183489}
  * @param hdc device context
@@ -182,8 +216,12 @@ int gdi_DeleteDC(HGDI_DC hdc)
 {
        if (hdc->hwnd)
        {
-               free(hdc->hwnd->cinvalid);
-               free(hdc->hwnd->invalid);
+               if (hdc->hwnd->cinvalid != NULL)
+                       free(hdc->hwnd->cinvalid);
+
+               if (hdc->hwnd->invalid != NULL)
+                       free(hdc->hwnd->invalid);
+
                free(hdc->hwnd);
        }