linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Theodore Ts'o <tytso@mit.edu>
To: Li Xi <pkuelelixi@gmail.com>
Cc: linux-ext4@vger.kernel.org
Subject: Re: A memory-leak problem of unix_open()
Date: Sun, 16 Dec 2012 20:20:52 -0500	[thread overview]
Message-ID: <20121217012052.GA9130@thunk.org> (raw)
In-Reply-To: <CAPTn0cDC2r3upOb=PAdT_SDH3HbYmkL-Me69RUMU2hxLZjBM7g@mail.gmail.com>

On Mon, Dec 17, 2012 at 12:34:56AM +0800, Li Xi wrote:
> I think I found a memory-leak problem of e2fsprofgs while using
> valgrind to testing a tool. 'log.txt' is the output. It is a simple
> problem. I wrote a patch, and it works on my server.

Thank you very much for reporting the problem!  While I was looking at
your patch, I found some some potential fd leaks that should also be
fixed.  This is what I have checked into the e2fsprogs tree.

Regards,

							- Ted

commit 4e0bb5eb745009decac4c5836671ff4bef21ce2a
Author: Theodore Ts'o <tytso@mit.edu>
Date:   Sun Dec 16 20:14:20 2012 -0500

    libext2fs: fix memory and fd leak in error path of unix_open()
    
    Fix a potential memory leak reported by Li Xi.  In addition, there
    were possible error cases where the file descriptor would not be
    properly closed, so fix those as well while we're at it.
    
    Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
    Reported-by:  Li Xi <pkuelelixi@gmail.com>

diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c
index 02570f0..7371654 100644
--- a/lib/ext2fs/unix_io.c
+++ b/lib/ext2fs/unix_io.c
@@ -505,6 +505,7 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)
 	memset(data, 0, sizeof(struct unix_private_data));
 	data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
 	data->io_stats.num_fields = 2;
+	data->dev = -1;
 
 	open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
 	if (flags & IO_FLAG_EXCLUSIVE)
@@ -575,7 +576,6 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)
 		/* Is the block device actually writable? */
 		error = ioctl(data->dev, BLKROGET, &readonly);
 		if (!error && readonly) {
-			close(data->dev);
 			retval = EPERM;
 			goto cleanup;
 		}
@@ -621,11 +621,17 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)
 
 cleanup:
 	if (data) {
+		if (data->dev >= 0)
+			close(data->dev);
 		free_cache(data);
 		ext2fs_free_mem(&data);
 	}
-	if (io)
+	if (io) {
+		if (io->name) {
+			ext2fs_free_mem(&io->name);
+		}
 		ext2fs_free_mem(&io);
+	}
 	return retval;
 }
 

      reply	other threads:[~2012-12-17  1:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-16 16:34 A memory-leak problem of unix_open() Li Xi
2012-12-17  1:20 ` Theodore Ts'o [this message]

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=20121217012052.GA9130@thunk.org \
    --to=tytso@mit.edu \
    --cc=linux-ext4@vger.kernel.org \
    --cc=pkuelelixi@gmail.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).