diff mbox

ossaudio: fix memory leak

Message ID 1435021270-7768-1-git-send-email-arei.gonglei@huawei.com
State New
Headers show

Commit Message

Gonglei (Arei) June 23, 2015, 1:01 a.m. UTC
From: Gonglei <arei.gonglei@huawei.com>

Variable "conf" going out of scope leaks the storage
it points to in line 856.

Signed-off-by: Gonglei <arei.gonglei@huawei.com>
---
 audio/ossaudio.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Markus Armbruster June 23, 2015, 7:35 a.m. UTC | #1
<arei.gonglei@huawei.com> writes:

> From: Gonglei <arei.gonglei@huawei.com>
>
> Variable "conf" going out of scope leaks the storage
> it points to in line 856.
>
> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> ---
>  audio/ossaudio.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/audio/ossaudio.c b/audio/ossaudio.c
> index 11e76a1..7dbe333 100644
> --- a/audio/ossaudio.c
> +++ b/audio/ossaudio.c
> @@ -853,6 +853,7 @@ static void *oss_audio_init (void)
   static void *oss_audio_init (void)
   {
       OSSConf *conf = g_malloc(sizeof(OSSConf));
       *conf = glob_conf;
>  
>      if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
>          access(conf->devpath_out, R_OK | W_OK) < 0) {
> +        g_free(conf);
>          return NULL;
>      }
>      return conf;
   }

Simpler (untested):

   static void *oss_audio_init (void)
   {
       if (access(globconf.devpath_in, R_OK | W_OK) < 0 ||
           access(globconf.devpath_out, R_OK | W_OK) < 0) {
           return NULL;
       }
       return g_memdup(&glob_conf, sizeof(glob_conf));
   }
Gonglei (Arei) June 23, 2015, 8:03 a.m. UTC | #2
On 2015/6/23 15:35, Markus Armbruster wrote:
> <arei.gonglei@huawei.com> writes:
> 
>> From: Gonglei <arei.gonglei@huawei.com>
>>
>> Variable "conf" going out of scope leaks the storage
>> it points to in line 856.
>>
>> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
>> ---
>>  audio/ossaudio.c | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/audio/ossaudio.c b/audio/ossaudio.c
>> index 11e76a1..7dbe333 100644
>> --- a/audio/ossaudio.c
>> +++ b/audio/ossaudio.c
>> @@ -853,6 +853,7 @@ static void *oss_audio_init (void)
>    static void *oss_audio_init (void)
>    {
>        OSSConf *conf = g_malloc(sizeof(OSSConf));
>        *conf = glob_conf;
>>  
>>      if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
>>          access(conf->devpath_out, R_OK | W_OK) < 0) {
>> +        g_free(conf);
>>          return NULL;
>>      }
>>      return conf;
>    }
> 
> Simpler (untested):
> 
>    static void *oss_audio_init (void)
>    {
>        if (access(globconf.devpath_in, R_OK | W_OK) < 0 ||
>            access(globconf.devpath_out, R_OK | W_OK) < 0) {
>            return NULL;
>        }
>        return g_memdup(&glob_conf, sizeof(glob_conf));
>    }
> 
I think your approach is better :)
Do you want to submit a patch? Or let me post v2 for this?

Regards,
-Gonglei
diff mbox

Patch

diff --git a/audio/ossaudio.c b/audio/ossaudio.c
index 11e76a1..7dbe333 100644
--- a/audio/ossaudio.c
+++ b/audio/ossaudio.c
@@ -853,6 +853,7 @@  static void *oss_audio_init (void)
 
     if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
         access(conf->devpath_out, R_OK | W_OK) < 0) {
+        g_free(conf);
         return NULL;
     }
     return conf;