From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jeff King Subject: [PATCH 02/68] mailsplit: fix FILE* leak in split_maildir Date: Thu, 24 Sep 2015 17:03:05 -0400 Message-ID: <20150924210305.GB30744@sigill.intra.peff.net> References: <20150924210225.GA23624@sigill.intra.peff.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Thu Sep 24 23:03:33 2015 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZfDfj-0005RQ-Sn for gcvg-git-2@plane.gmane.org; Thu, 24 Sep 2015 23:03:20 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753951AbbIXVDI (ORCPT ); Thu, 24 Sep 2015 17:03:08 -0400 Received: from cloud.peff.net ([50.56.180.127]:35896 "HELO cloud.peff.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1753272AbbIXVDH (ORCPT ); Thu, 24 Sep 2015 17:03:07 -0400 Received: (qmail 11653 invoked by uid 102); 24 Sep 2015 21:03:07 -0000 Received: from Unknown (HELO peff.net) (10.0.1.1) by cloud.peff.net (qpsmtpd/0.84) with SMTP; Thu, 24 Sep 2015 16:03:07 -0500 Received: (qmail 28814 invoked by uid 107); 24 Sep 2015 21:03:19 -0000 Received: from sigill.intra.peff.net (HELO sigill.intra.peff.net) (10.0.0.7) by peff.net (qpsmtpd/0.84) with SMTP; Thu, 24 Sep 2015 17:03:19 -0400 Received: by sigill.intra.peff.net (sSMTP sendmail emulation); Thu, 24 Sep 2015 17:03:05 -0400 Content-Disposition: inline In-Reply-To: <20150924210225.GA23624@sigill.intra.peff.net> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: If we encounter an error while splitting a maildir, we exit the function early, leaking the open filehandle. This isn't a big deal, since we exit the program soon after, but it's easy enough to be careful. Signed-off-by: Jeff King --- builtin/mailsplit.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/builtin/mailsplit.c b/builtin/mailsplit.c index 8e02ea1..9de06e3 100644 --- a/builtin/mailsplit.c +++ b/builtin/mailsplit.c @@ -150,6 +150,7 @@ static int split_maildir(const char *maildir, const char *dir, { char file[PATH_MAX]; char name[PATH_MAX]; + FILE *f = NULL; int ret = -1; int i; struct string_list list = STRING_LIST_INIT_DUP; @@ -160,7 +161,6 @@ static int split_maildir(const char *maildir, const char *dir, goto out; for (i = 0; i < list.nr; i++) { - FILE *f; snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string); f = fopen(file, "r"); if (!f) { @@ -177,10 +177,13 @@ static int split_maildir(const char *maildir, const char *dir, split_one(f, name, 1); fclose(f); + f = NULL; } ret = skip; out: + if (f) + fclose(f); string_list_clear(&list, 1); return ret; } -- 2.6.0.rc3.454.g204ad51