From patchwork Sun Jul 14 08:48:42 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 2827207 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 0DAB3C0AB2 for ; Sun, 14 Jul 2013 08:43:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2E8102011B for ; Sun, 14 Jul 2013 08:43:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1FC5D20117 for ; Sun, 14 Jul 2013 08:43:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751852Ab3GNIns (ORCPT ); Sun, 14 Jul 2013 04:43:48 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:42028 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751775Ab3GNInr (ORCPT ); Sun, 14 Jul 2013 04:43:47 -0400 Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by aserp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id r6E8hkRf030266 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sun, 14 Jul 2013 08:43:47 GMT Received: from aserz7021.oracle.com (aserz7021.oracle.com [141.146.126.230]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r6E8hjqc028119 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 14 Jul 2013 08:43:46 GMT Received: from abhmt119.oracle.com (abhmt119.oracle.com [141.146.116.71]) by aserz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r6E8hjKb017273 for ; Sun, 14 Jul 2013 08:43:45 GMT Received: from wish.sg.oracle.com (/10.186.101.18) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sun, 14 Jul 2013 01:43:44 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org Subject: [PATCH v2] Btrfs-progs: fix memory leak in open_file_or_dir() Date: Sun, 14 Jul 2013 16:48:42 +0800 Message-Id: <1373791722-29998-1-git-send-email-anand.jain@oracle.com> X-Mailer: git-send-email 1.8.1.164.g2d0029e In-Reply-To: <1373410908-2274-1-git-send-email-wangshilong1991@gmail.com> References: <1373410908-2274-1-git-send-email-wangshilong1991@gmail.com> X-Source-IP: ucsinet21.oracle.com [156.151.31.93] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wang Shilong After calling opendir() successfully, closedir() should be also called to free memory. Otherwise, memory leak happens. Signed-off-by: Anand Jain Signed-off-by: Wang Shilong Reviewed-by: Anand Jain --- 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; }