From patchwork Thu Aug 27 15:38:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byongho Lee X-Patchwork-Id: 7085771 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 431AA9F372 for ; Thu, 27 Aug 2015 15:38:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 655DB2076D for ; Thu, 27 Aug 2015 15:38:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7933C206AF for ; Thu, 27 Aug 2015 15:38:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754170AbbH0Pim (ORCPT ); Thu, 27 Aug 2015 11:38:42 -0400 Received: from mail-pa0-f45.google.com ([209.85.220.45]:33523 "EHLO mail-pa0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753897AbbH0Pil (ORCPT ); Thu, 27 Aug 2015 11:38:41 -0400 Received: by pacti10 with SMTP id ti10so29976100pac.0 for ; Thu, 27 Aug 2015 08:38:41 -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=+jbYdaP3kIUbPpxl65lY4QHDBPgYHK5lxOyqKKa6VEw=; b=Kjx/XAdXTElwEU8XIhpeayMT1AbEfyYkqa11z/lmdpnjxGRiiY851xlYyo0zBuyZK6 6gN7yZDDk25Jne+ufINxO3/nJNbyjmzojFQx7b2b5ZUik1b8eqMTz6tGQq/OJIWjnAdO s+LPB6IH4xYYrnsAIB7AGtrWirf6d26T80N+6RIDBGrw7ukLnfB8I7jYVe713durkEio E34HvZc63PS8dtBQF3jdHPW6aY5mOyORYDnq8j/e5w8iELsjiq1G8tbyQAjg2uPHH97h 5QA9xaqSVhvi5kG6TLnpj5Va7LmMOVeQqiwAONjKx/yyPA7C2aTyVR90W0dBPRUB17E9 y6KA== X-Received: by 10.68.138.200 with SMTP id qs8mr7545793pbb.19.1440689921101; Thu, 27 Aug 2015 08:38:41 -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.39 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Aug 2015 08:38:40 -0700 (PDT) From: Byongho Lee To: linux-btrfs@vger.kernel.org Subject: [PATCH 3/3] btrfs-progs: add memory allocation fail check in btrfs_add_to_fsid() Date: Fri, 28 Aug 2015 00:38:18 +0900 Message-Id: <1440689898-35178-4-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_add_to_fsid(), strdup() allocates memory to device->name, but the return value is not checked. So add the return value check and error handling code. And clean-up error handling code for ENOMEM case. Signed-off-by: Byongho Lee --- utils.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/utils.c b/utils.c index 280637d896c1..160a8c273da8 100644 --- a/utils.c +++ b/utils.c @@ -729,21 +729,18 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, struct btrfs_super_block *super = root->fs_info->super_copy; struct btrfs_device *device; struct btrfs_dev_item *dev_item; - char *buf; + char *buf = NULL; u64 total_bytes; u64 num_devs; int ret; device = kzalloc(sizeof(*device), GFP_NOFS); if (!device) - return -ENOMEM; - buf = kmalloc(sectorsize, GFP_NOFS); - if (!buf) { - kfree(device); - return -ENOMEM; - } + goto err_nomem; + buf = kzalloc(sectorsize, GFP_NOFS); + if (!buf) + goto err_nomem; BUG_ON(sizeof(*disk_super) > sectorsize); - memset(buf, 0, sectorsize); disk_super = (struct btrfs_super_block *)buf; dev_item = &disk_super->dev_item; @@ -761,6 +758,8 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, device->total_ios = 0; device->dev_root = root->fs_info->dev_root; device->name = strdup(path); + if (!device->name) + goto err_nomem; ret = btrfs_add_device(trans, root, device); BUG_ON(ret); @@ -790,6 +789,11 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, list_add(&device->dev_list, &root->fs_info->fs_devices->devices); device->fs_devices = root->fs_info->fs_devices; return 0; + +err_nomem: + kfree(device); + kfree(buf); + return -ENOMEM; } static void btrfs_wipe_existing_sb(int fd)