From patchwork Wed Apr 26 01:22:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 9700165 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E55C8603F4 for ; Wed, 26 Apr 2017 01:25:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A148228497 for ; Wed, 26 Apr 2017 01:25:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 82E81284EA; Wed, 26 Apr 2017 01:25:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EDEA028497 for ; Wed, 26 Apr 2017 01:25:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1434260AbdDZBW4 (ORCPT ); Tue, 25 Apr 2017 21:22:56 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:46285 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1434256AbdDZBWz (ORCPT ); Tue, 25 Apr 2017 21:22:55 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="18159099" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 26 Apr 2017 09:22:44 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id EEED247E6342 for ; Wed, 26 Apr 2017 09:22:42 +0800 (CST) Received: from lufq.lufq.5F (10.167.225.63) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 26 Apr 2017 09:22:42 +0800 From: Lu Fengqi To: Subject: [PATCH] btrfs-progs: tests: fssum, fix memory leak Date: Wed, 26 Apr 2017 09:22:41 +0800 Message-ID: <20170426012241.29814-1-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.12.2 MIME-Version: 1.0 X-Originating-IP: [10.167.225.63] X-yoursite-MailScanner-ID: EEED247E6342.AD3B7 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: lufq.fnst@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Free the alloced memory and close dir before exit. Signed-off-by: Lu Fengqi --- tests/fssum.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/tests/fssum.c b/tests/fssum.c index 83bd4106..8be44547 100644 --- a/tests/fssum.c +++ b/tests/fssum.c @@ -420,6 +420,9 @@ check_manifest(char *fn, char *m, char *c, int last_call) while ((l = getln(line, sizeof(line), in_fp))) { rem_c = strrchr(l, ' '); if (!rem_c) { + if (checksum) + free(checksum); + /* final cs */ checksum = strdup(l); break; @@ -503,6 +506,7 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in) } ++entries; } + qsort(namelist, entries, sizeof(*namelist), namecmp); for (i = 0; i < entries; ++i) { struct stat64 st; @@ -624,7 +628,11 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in) sum_add_sum(dircs, &meta); next: free(path); + free(namelist[i]); } + + free(namelist); + closedir(d); } int @@ -636,7 +644,9 @@ main(int argc, char *argv[]) char *path; int fd; sum_t cs; + char *sumstring; char flagstring[sizeof(flchar)]; + int ret = 0; int i; int plen; int elen; @@ -736,6 +746,9 @@ main(int argc, char *argv[]) } else if ((p = strchr(l, ':'))) { *p++ = 0; parse_flags(l); + + if (checksum) + free(checksum); checksum = strdup(p); } else { fprintf(stderr, "invalid input file format\n"); @@ -798,16 +811,28 @@ main(int argc, char *argv[]) if (!gen_manifest) fprintf(out_fp, "%s:", flagstring); - fprintf(out_fp, "%s\n", sum_to_string(&cs)); + sumstring = sum_to_string(&cs); + fprintf(out_fp, "%s\n", sumstring); + free(sumstring); } else { - if (strcmp(checksum, sum_to_string(&cs)) == 0) { + sumstring = sum_to_string(&cs); + if (strcmp(checksum, sumstring) == 0) { printf("OK\n"); - exit(0); + ret = 0; } else { printf("FAIL\n"); - exit(1); + ret = 1; } + + free(checksum); + free(sumstring); } - exit(0); + if (in_fp) + fclose(in_fp); + + if (out_fp != stdout) + fclose(out_fp); + + exit(ret); }