snd_card_get_name uses strdup (Valgrind).

Michael Stefaniuc mstefani at redhat.de
Wed Feb 1 07:57:08 CST 2012


Hello Joerg,

On Wed, Feb 01, 2012 at 01:37:44PM +0100, Joerg-Cyril.Hoehle at t-systems.com wrote:
> there's a small memory leak in winealsa, tentatively plugged by
> patches #83353, #83334 and #82900.
> 
> It would be nice if somebody who feels like struggling with const warnings in gcc
> would get a fix accepted in git.
> 
> The dilemma: a literal "foo" is a const char*, free() does not want a const
> and casts are the root of all evil in C.
something like the attached patch should be more palatable.

bye
	michael
-------------- next part --------------
>From 6befb1255dd8217c038a07f5da476a3a0e2ad870 Mon Sep 17 00:00:00 2001
From: Michael Stefaniuc <mstefani at redhat.de>
Date: Wed, 1 Feb 2012 14:45:25 +0100
Subject: winealsa.drv: Clean up the sound card name retrieval code.

Fixes a memleak found by Valgrind.
---
 dlls/winealsa.drv/mmdevdrv.c |   28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/dlls/winealsa.drv/mmdevdrv.c b/dlls/winealsa.drv/mmdevdrv.c
index 0bedcc5..46adb56 100644
--- a/dlls/winealsa.drv/mmdevdrv.c
+++ b/dlls/winealsa.drv/mmdevdrv.c
@@ -363,8 +363,9 @@ static HRESULT alsa_enum_devices(EDataFlow flow, WCHAR **ids, char **keys,
     for(err = snd_card_next(&card); card != -1 && err >= 0;
             err = snd_card_next(&card)){
         char cardpath[64];
-        const char *cardname;
+        char *cardname;
         WCHAR *cardnameW;
+        WCHAR szcardname[] = {'U','n','k','n','o','w','n',' ','s','o','u','n','d','c','a','r','d'};
         snd_ctl_t *ctl;
         DWORD len;
 
@@ -376,24 +377,27 @@ static HRESULT alsa_enum_devices(EDataFlow flow, WCHAR **ids, char **keys,
             continue;
         }
 
-        if((err = snd_card_get_name(card, (char **)&cardname)) < 0){
+        if(!(err = snd_card_get_name(card, &cardname))){
+            len = strlen(cardname);
+            cardnameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
+            if(!cardnameW){
+                free(cardname);
+                snd_ctl_close(ctl);
+                return E_OUTOFMEMORY;
+            }
+            MultiByteToWideChar(CP_UNIXCP, 0, cardname, len, cardnameW, len);
+            free(cardname);
+        }else{
             WARN("Unable to get card name for ALSA device %s: %d (%s)\n",
                     cardpath, err, snd_strerror(err));
             /* FIXME: Should be localized */
-            cardname = "Unknown soundcard";
-        }
-
-        len = MultiByteToWideChar(CP_UNIXCP, 0, cardname, -1, NULL, 0);
-        cardnameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
-        if(!cardnameW){
-            snd_ctl_close(ctl);
-            return E_OUTOFMEMORY;
+            cardnameW = szcardname;
         }
-        MultiByteToWideChar(CP_UNIXCP, 0, cardname, -1, cardnameW, len);
 
         alsa_get_card_devices(stream, ids, keys, num, ctl, card, cardnameW);
 
-        HeapFree(GetProcessHeap(), 0, cardnameW);
+        if (cardnameW != szcardname)
+            HeapFree(GetProcessHeap(), 0, cardnameW);
 
         snd_ctl_close(ctl);
     }
-- 
1.7.4.1



More information about the wine-patches mailing list