From patchwork Thu Aug 27 15:38:16 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byongho Lee X-Patchwork-Id: 7085751 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0E2F89F6CE for ; Thu, 27 Aug 2015 15:38:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2DBFD2076D for ; Thu, 27 Aug 2015 15:38:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 07E74209B3 for ; Thu, 27 Aug 2015 15:38:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753590AbbH0Pie (ORCPT ); Thu, 27 Aug 2015 11:38:34 -0400 Received: from mail-pa0-f44.google.com ([209.85.220.44]:35182 "EHLO mail-pa0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753457AbbH0Pic (ORCPT ); Thu, 27 Aug 2015 11:38:32 -0400 Received: by pacdd16 with SMTP id dd16so29303815pac.2 for ; Thu, 27 Aug 2015 08:38:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references; bh=OVsPGTba+jTGM0124X3JPsNECRbyKYl55/6vjgmsRwI=; b=Oo+od1n4fFpFu9SWk3dva+WTX7pfEfzpCf70RnNrnYvDsHzFxFYSIdcJVjLf07CZ9b USZVyQJF07PRVhbqbwS8nzPaTHBCNU57/bBY9kSoKih+zW3Amfm4gJ5V3JEaiwL21Het YZK0+u4J6BQF4yBAhb1qlPkTBTJ5Mst8QN7mKT6PZU80qIjEYfAlRZiI7/+9Y2gA9SuA f7jNX2KIMznMuTjWF1NeuHeCrlijqsNFMNv+i0I9mvYWGxH+MQay9CntU5HqCfXFtmb5 Wr1GlRKE6JJ0cn05piO4H2tOqS9PuPGRUURIWVfxLi0WEnfMbJIBcjZCZkhJiGRc+lE4 0luQ== X-Received: by 10.68.88.130 with SMTP id bg2mr7345549pbb.129.1440689912439; Thu, 27 Aug 2015 08:38:32 -0700 (PDT) Received: from arch-nb.localdomain ([175.118.89.137]) by smtp.gmail.com with ESMTPSA id vw6sm2844831pab.14.2015.08.27.08.38.30 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Aug 2015 08:38:31 -0700 (PDT) From: Byongho Lee To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/3] btrfs-progs: fix memory leak in btrfs-convert main() Date: Fri, 28 Aug 2015 00:38:16 +0900 Message-Id: <1440689898-35178-2-git-send-email-bhlee.kernel@gmail.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1440689898-35178-1-git-send-email-bhlee.kernel@gmail.com> References: <1440689898-35178-1-git-send-email-bhlee.kernel@gmail.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham 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 In btrfs-convert main(), strdup() allocates memory to fslabel but that memory is not freed. We could fix it by adding free() calls to every return point, but that would make the code messy because there are several return paths. So I fix it by changing the code using strdup() with local array and strncpy(). And btrfs-convert main() guarantees that string length of fslabel is not to exceed 'BTRFS_LABEL_SIZE', so it's enough to use strcpy() instead of strncpy() to copy fslabel in do_convert(). Signed-off-by: Byongho Lee --- btrfs-convert.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index 917bbc1b74d2..25ae424ea73b 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -2428,7 +2428,7 @@ static int do_convert(const char *devname, int datacsum, int packing, int noxatt fprintf(stderr, "copy label '%s'\n", root->fs_info->super_copy->label); } else if (copylabel == -1) { - strncpy(root->fs_info->super_copy->label, fslabel, BTRFS_LABEL_SIZE); + strcpy(root->fs_info->super_copy->label, fslabel); fprintf(stderr, "set label to '%s'\n", fslabel); } @@ -2868,7 +2868,7 @@ int main(int argc, char *argv[]) int usage_error = 0; int progress = 1; char *file; - char *fslabel = NULL; + char fslabel[BTRFS_LABEL_SIZE+1]; u64 features = BTRFS_MKFS_DEFAULT_FEATURES; while(1) { @@ -2910,8 +2910,9 @@ int main(int argc, char *argv[]) break; case 'l': copylabel = -1; - fslabel = strdup(optarg); - if (strlen(fslabel) > BTRFS_LABEL_SIZE) { + fslabel[BTRFS_LABEL_SIZE] = 0; + strncpy(fslabel, optarg, sizeof(fslabel)); + if (fslabel[BTRFS_LABEL_SIZE]) { fprintf(stderr, "warning: label too long, trimmed to %d bytes\n", BTRFS_LABEL_SIZE);