diff mbox

Btrfs-progs: fix memory leak in open_file_or_dir()

Message ID 1373410908-2274-1-git-send-email-wangshilong1991@gmail.com (mailing list archive)
State Under Review, archived
Headers show

Commit Message

Wang Shilong July 9, 2013, 11:01 p.m. UTC
From: Wang Shilong <wangsl.fnst@cn.fujitsu.com>

After calling opendir() successfully, closedir() should be
also called to free memory. Otherwise, memory leak happens.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
---
 utils.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Anand Jain July 10, 2013, 3:43 a.m. UTC | #1
Thanks.


Reviewed-by: Anand Jain <anand.jain@oracle.com>


On 07/10/2013 07:01 AM, Wang Shilong wrote:
> From: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
>
> After calling opendir() successfully, closedir() should be
> also called to free memory. Otherwise, memory leak happens.
>
> Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
> ---
>   utils.c | 9 +++++----
>   1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/utils.c b/utils.c
> index 7b4cd74..0afff55 100644
> --- a/utils.c
> +++ b/utils.c
> @@ -1480,7 +1480,7 @@ int open_file_or_dir(const char *fname)
>   {
>   	int ret;
>   	struct stat st;
> -	DIR *dirstream;
> +	DIR *dirstream = NULL;
>   	int fd;
>
>   	ret = stat(fname, &st);
> @@ -1496,9 +1496,10 @@ int open_file_or_dir(const char *fname)
>   	} else {
>   		fd = open(fname, O_RDWR);
>   	}
> -	if (fd < 0) {
> -		return -3;
> -	}
> +	if (fd < 0)
> +		fd = -3;
> +	if (dirstream)
> +		closedir(dirstream);
>   	return fd;
>   }
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Anand Jain July 14, 2013, 8:33 a.m. UTC | #2
I am sorry that this has missed my eyes.
  There is a regression in this patch and most
  the commands are failing.

---
# btrfs fi df /btrfs
ERROR: couldn't get space info on '/btrfs' - Bad file descriptor
---

 >> +    if (fd < 0)
 >> +        fd = -3;
 >> +    if (dirstream)
 >> +        closedir(dirstream);

   We have to closedir only when fd < 0.


Thanks, Anand


On 10/07/2013 11:43, Anand Jain wrote:
>
>   Thanks.
>
>
> Reviewed-by: Anand Jain <anand.jain@oracle.com>
>
>
> On 07/10/2013 07:01 AM, Wang Shilong wrote:
>> From: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
>>
>> After calling opendir() successfully, closedir() should be
>> also called to free memory. Otherwise, memory leak happens.
>>
>> Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
>> ---
>>   utils.c | 9 +++++----
>>   1 file changed, 5 insertions(+), 4 deletions(-)
>>
>> diff --git a/utils.c b/utils.c
>> index 7b4cd74..0afff55 100644
>> --- a/utils.c
>> +++ b/utils.c
>> @@ -1480,7 +1480,7 @@ int open_file_or_dir(const char *fname)
>>   {
>>       int ret;
>>       struct stat st;
>> -    DIR *dirstream;
>> +    DIR *dirstream = NULL;
>>       int fd;
>>
>>       ret = stat(fname, &st);
>> @@ -1496,9 +1496,10 @@ int open_file_or_dir(const char *fname)
>>       } else {
>>           fd = open(fname, O_RDWR);
>>       }
>> -    if (fd < 0) {
>> -        return -3;
>> -    }
>> +    if (fd < 0)
>> +        fd = -3;
>> +    if (dirstream)
>> +        closedir(dirstream);
>>       return fd;
>>   }
>>
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/utils.c b/utils.c
index 7b4cd74..0afff55 100644
--- a/utils.c
+++ b/utils.c
@@ -1480,7 +1480,7 @@  int open_file_or_dir(const char *fname)
 {
 	int ret;
 	struct stat st;
-	DIR *dirstream;
+	DIR *dirstream = NULL;
 	int fd;
 
 	ret = stat(fname, &st);
@@ -1496,9 +1496,10 @@  int open_file_or_dir(const char *fname)
 	} else {
 		fd = open(fname, O_RDWR);
 	}
-	if (fd < 0) {
-		return -3;
-	}
+	if (fd < 0)
+		fd = -3;
+	if (dirstream)
+		closedir(dirstream);
 	return fd;
 }