From patchwork Wed Nov 6 23:15:45 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 3149781 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 5875ABEEB2 for ; Wed, 6 Nov 2013 23:17:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4C9F620304 for ; Wed, 6 Nov 2013 23:17:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 66ABB20303 for ; Wed, 6 Nov 2013 23:17:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752448Ab3KFXRA (ORCPT ); Wed, 6 Nov 2013 18:17:00 -0500 Received: from sandeen.net ([63.231.237.45]:56122 "EHLO sandeen.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751556Ab3KFXP6 (ORCPT ); Wed, 6 Nov 2013 18:15:58 -0500 Received: by sandeen.net (Postfix, from userid 500) id 56AF2654E644; Wed, 6 Nov 2013 17:15:57 -0600 (CST) From: Eric Sandeen To: linux-btrfs@vger.kernel.org Subject: [PATCH 06/16] btrfs-progs: fix leak of "buf" in make_btrfs() error paths Date: Wed, 6 Nov 2013 17:15:45 -0600 Message-Id: <1383779755-18228-7-git-send-email-sandeen@redhat.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1383779755-18228-1-git-send-email-sandeen@redhat.com> References: <1383779755-18228-1-git-send-email-sandeen@redhat.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=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, 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 If any pwrite failed we leaked the allocated "buf" on return from the function. "goto out" takes care of those paths. Resolves-Coverity-CID: 1125938 Signed-off-by: Eric Sandeen --- utils.c | 62 ++++++++++++++++++++++++++++++++------------------------------ 1 files changed, 32 insertions(+), 30 deletions(-) diff --git a/utils.c b/utils.c index c784345..d8ce153 100644 --- a/utils.c +++ b/utils.c @@ -210,10 +210,10 @@ int make_btrfs(int fd, const char *device, const char *label, csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); ret = pwrite(fd, buf->data, leafsize, blocks[1]); - if (ret < 0) - return -errno; - else if (ret != leafsize) - return -EIO; + if (ret != leafsize) { + ret = (ret < 0 ? -errno : -EIO); + goto out; + } /* create the items for the extent tree */ memset(buf->data+sizeof(struct btrfs_header), 0, @@ -269,10 +269,10 @@ int make_btrfs(int fd, const char *device, const char *label, btrfs_set_header_nritems(buf, nritems); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); ret = pwrite(fd, buf->data, leafsize, blocks[2]); - if (ret < 0) - return -errno; - else if (ret != leafsize) - return -EIO; + if (ret != leafsize) { + ret = (ret < 0 ? -errno : -EIO); + goto out; + } /* create the chunk tree */ memset(buf->data+sizeof(struct btrfs_header), 0, @@ -356,10 +356,10 @@ int make_btrfs(int fd, const char *device, const char *label, btrfs_set_header_nritems(buf, nritems); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); ret = pwrite(fd, buf->data, leafsize, blocks[3]); - if (ret < 0) - return -errno; - else if (ret != leafsize) - return -EIO; + if (ret != leafsize) { + ret = (ret < 0 ? -errno : -EIO); + goto out; + } /* create the device tree */ memset(buf->data+sizeof(struct btrfs_header), 0, @@ -395,10 +395,10 @@ int make_btrfs(int fd, const char *device, const char *label, btrfs_set_header_nritems(buf, nritems); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); ret = pwrite(fd, buf->data, leafsize, blocks[4]); - if (ret < 0) - return -errno; - else if (ret != leafsize) - return -EIO; + if (ret != leafsize) { + ret = (ret < 0 ? -errno : -EIO); + goto out; + } /* create the FS root */ memset(buf->data+sizeof(struct btrfs_header), 0, @@ -408,11 +408,10 @@ int make_btrfs(int fd, const char *device, const char *label, btrfs_set_header_nritems(buf, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); ret = pwrite(fd, buf->data, leafsize, blocks[5]); - if (ret < 0) - return -errno; - else if (ret != leafsize) - return -EIO; - + if (ret != leafsize) { + ret = (ret < 0 ? -errno : -EIO); + goto out; + } /* finally create the csum root */ memset(buf->data+sizeof(struct btrfs_header), 0, leafsize-sizeof(struct btrfs_header)); @@ -421,10 +420,10 @@ int make_btrfs(int fd, const char *device, const char *label, btrfs_set_header_nritems(buf, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); ret = pwrite(fd, buf->data, leafsize, blocks[6]); - if (ret < 0) - return -errno; - else if (ret != leafsize) - return -EIO; + if (ret != leafsize) { + ret = (ret < 0 ? -errno : -EIO); + goto out; + } /* and write out the super block */ BUG_ON(sizeof(super) > sectorsize); @@ -433,13 +432,16 @@ int make_btrfs(int fd, const char *device, const char *label, buf->len = sectorsize; csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); ret = pwrite(fd, buf->data, sectorsize, blocks[0]); - if (ret < 0) - return -errno; - else if (ret != sectorsize) - return -EIO; + if (ret != sectorsize) { + ret = (ret < 0 ? -errno : -EIO); + goto out; + } + + ret = 0; +out: free(buf); - return 0; + return ret; } u64 btrfs_device_size(int fd, struct stat *st)