linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: anand jain <anand.jain@oracle.com>
To: Wang Shilong <wangshilong1991@gmail.com>
Cc: "linux-btrfs@vger.kernel.org Btrfs" <linux-btrfs@vger.kernel.org>,
	David Sterba <dsterba@suse.cz>,
	"wangsl.fnst@cn.fujitsu.com" <wangsl.fnst@cn.fujitsu.com>
Subject: Re: [PATCH v2] Btrfs-progs: fix memory leak in open_file_or_dir()
Date: Sun, 14 Jul 2013 22:40:49 +0800	[thread overview]
Message-ID: <51E2B871.5000801@oracle.com> (raw)
In-Reply-To: <4C2AE033-0C93-419A-9FD2-C8E0B0A7A942@gmail.com>




On 14/07/2013 21:58, Wang Shilong wrote:
> Hello Anand and David,
> 
> I use the tool valgrind to whether there exists memory leak:
> 
> valgrind  --tool=memcheck  --trace-origins=yes --leak-chek=full btrfs sub list /mnt
> There still exists memory leak related to open_file_or_dir() (After applying this patch).
> 
> I guess it is because we should call closedir(dirstram) rather than close(fd) if opening a directory.
> Maybe we can make open_file_or_dir() something like:

> open_file_or_dir(char *name, DIR **dirstream)
> 
> if we opened a directory, we close it by calling closeidr(dirstream).
> elsewise, close(fd) is ok!..
> 
> I am wondering why close(fd) can not just finish this work……

 now I get your broader point of view. For some reason it
 gave me an impression you are trying to handle a proper
 cleanup operation if when dirfd(dirstream) fails and below
 patch would just address that.

 Now when open_file_or_dir() is successful we still need a
 proper closing code.  May be its a good idea to write a new
 patch to include that.. it touches a lot of places.

Thanks, Anand


> Thanks,
> Wang
>> 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: Anand Jain <anand.jain@oracle.com>
>> Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
>> Reviewed-by: Anand Jain <anand.jain@oracle.com>
>> ---
>> utils.c |    6 ++++--
>> 1 files changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/utils.c b/utils.c
>> index d3bec9b..4b3c778 100644
>> --- a/utils.c
>> +++ b/utils.c
>> @@ -1503,7 +1503,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);
>> @@ -1520,7 +1520,9 @@ int open_file_or_dir(const char *fname)
>> 		fd = open(fname, O_RDWR);
>> 	}
>> 	if (fd < 0) {
>> -		return -3;
>> +		fd = -3;
>> +		if (dirstream)
>> +			closedir(dirstream);
>> 	}
>> 	return fd;
>> }
>> -- 
>> 1.7.7.6
>>
>> --
>> 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
> 

  reply	other threads:[~2013-07-14 14:41 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-09 23:01 [PATCH] Btrfs-progs: fix memory leak in open_file_or_dir() Wang Shilong
2013-07-10  3:43 ` Anand Jain
2013-07-14  8:33   ` anand jain
2013-07-14  8:46 ` [PATCH] From: Wang Shilong <wangsl.fnst@cn.fujitsu.com> Anand Jain
2013-07-14  8:48 ` [PATCH v2] Btrfs-progs: fix memory leak in open_file_or_dir() Anand Jain
2013-07-14 13:58   ` Wang Shilong
2013-07-14 14:40     ` anand jain [this message]
2013-07-14 14:51       ` Wang Shilong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=51E2B871.5000801@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=wangshilong1991@gmail.com \
    --cc=wangsl.fnst@cn.fujitsu.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).