From patchwork Mon Dec 17 01:20:52 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Theodore Ts'o X-Patchwork-Id: 206764 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id B8F702C0087 for ; Mon, 17 Dec 2012 12:20:59 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751595Ab2LQBU5 (ORCPT ); Sun, 16 Dec 2012 20:20:57 -0500 Received: from li9-11.members.linode.com ([67.18.176.11]:38800 "EHLO imap.thunk.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751397Ab2LQBU5 (ORCPT ); Sun, 16 Dec 2012 20:20:57 -0500 Received: from root (helo=closure.thunk.org) by imap.thunk.org with local-esmtp (Exim 4.72) (envelope-from ) id 1TkPNr-0005Ui-Gf; Mon, 17 Dec 2012 01:20:43 +0000 Received: by closure.thunk.org (Postfix, from userid 15806) id D94CA243F28; Sun, 16 Dec 2012 20:20:52 -0500 (EST) Date: Sun, 16 Dec 2012 20:20:52 -0500 From: Theodore Ts'o To: Li Xi Cc: linux-ext4@vger.kernel.org Subject: Re: A memory-leak problem of unix_open() Message-ID: <20121217012052.GA9130@thunk.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: tytso@thunk.org X-SA-Exim-Scanned: No (on imap.thunk.org); SAEximRunCond expanded to false Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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 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" Reported-by: Li Xi --- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html 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; }