From patchwork Mon Oct 17 04:37:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasanth KSR X-Patchwork-Id: 9378443 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 54E8760487 for ; Mon, 17 Oct 2016 04:39:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 52E7528E61 for ; Mon, 17 Oct 2016 04:39:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 46E5A28E7C; Mon, 17 Oct 2016 04:39:10 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 C6A4428E61 for ; Mon, 17 Oct 2016 04:39:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755145AbcJQEiV (ORCPT ); Mon, 17 Oct 2016 00:38:21 -0400 Received: from mail-pa0-f66.google.com ([209.85.220.66]:35939 "EHLO mail-pa0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755358AbcJQEiU (ORCPT ); Mon, 17 Oct 2016 00:38:20 -0400 Received: by mail-pa0-f66.google.com with SMTP id os4so4688864pac.3 for ; Sun, 16 Oct 2016 21:38:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=JDrgjeQxx18d5DNhGOHvdhdf4+YAID0ckYshY1Aq8ag=; b=OSRCSy4Ks5eN+wPC/2ZaMTES0ccOAaS2S9fC+qvNPfj81UyeDhQFRiIruUfYl76v0z BfbtU/4RKlyXROIWQLRdN8mgcpp1Nnd8Gknc6OP42GjaYfgIwQDGhAR7ODsO4stNTXdP qqdea8x6msizXdqLYlTRpHD6aIbupEy/mFvpqQ/96I/PEX3fH/7CmDrYTo80rntLiRRO hM7U8rUu/gBbFJoYfjWkrAyxIjeKeD+w+W21qT1Z6bIGcQfMiSR6m1NtMi2ShXfZjgqV smoEhraEbhmRHo+CibwC1JKf5Mo7/u14XOyKraKxTKijD2vVj2kRLTR94El8XCuFAkHN R0ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=JDrgjeQxx18d5DNhGOHvdhdf4+YAID0ckYshY1Aq8ag=; b=PZennHt86BgN9oocKKEnaKZAmHZ5fX0PgCeJNG7TIIsUaQ5iRkVtQmHpz0bNurw9qD yw0hL4edCXCAqqFsNvbta0VlV768ADUEtAXGrVcVfkSWMZPoKe5/3nfZHVkgeqtwc++5 59GWNbrsNFNdJuASVPuWpKW5hQ23YL/aQC5WlKbtVWtPv1ftxWBA2IwBeEC1FjUvVKBO Y1rOz6hD76ls552kjnNRFMfK35mSBmqthH999b1r9AAFW24z7qThkiZc7NRc2Pf4loyf 3tWgbmECb9p4db+hURu2kkxarsc6AdUISV1enkwCTqRCP5GjZ84CmYIBxQjuxYxstyv2 nrIg== X-Gm-Message-State: AA6/9Rmys/LX0uYBXqKOWXPiIi2ZzXQLEAJ7GldS0FtUkuvkl6Y+fn2efaCUdG4Y0UcSRg== X-Received: by 10.66.229.100 with SMTP id sp4mr28233310pac.148.1476679093513; Sun, 16 Oct 2016 21:38:13 -0700 (PDT) Received: from linux-eq9s.suse ([106.51.242.29]) by smtp.gmail.com with ESMTPSA id a88sm43744610pfe.21.2016.10.16.21.38.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 16 Oct 2016 21:38:13 -0700 (PDT) From: Prasanth K S R To: linux-btrfs@vger.kernel.org, dsterba@suse.com Cc: Prasanth K S R Subject: [PATCH V2] Btrfs-progs: subvol_uuid_search: Return error code on memory allocation failure Date: Mon, 17 Oct 2016 10:07:17 +0530 Message-Id: <1476679037-2485-1-git-send-email-kosigiprasanth@gmail.com> X-Mailer: git-send-email 2.6.6 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 From: Prasanth K S R This commit fixes coverity defect CID 1328695. Signed-off-by: Prasanth K S R --- cmds-receive.c | 10 +++++----- cmds-send.c | 18 +++++++++--------- send-utils.c | 22 ++++++++++++++++++---- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/cmds-receive.c b/cmds-receive.c index d0525bf..40f64de 100644 --- a/cmds-receive.c +++ b/cmds-receive.c @@ -283,12 +283,12 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid, parent_subvol = subvol_uuid_search(&r->sus, 0, parent_uuid, parent_ctransid, NULL, subvol_search_by_received_uuid); - if (!parent_subvol) { + if (IS_ERR(parent_subvol)) { parent_subvol = subvol_uuid_search(&r->sus, 0, parent_uuid, parent_ctransid, NULL, subvol_search_by_uuid); } - if (!parent_subvol) { - ret = -ENOENT; + if (IS_ERR(parent_subvol)) { + ret = PTR_ERR(parent_subvol); error("cannot find parent subvolume"); goto out; } @@ -744,13 +744,13 @@ static int process_clone(const char *path, u64 offset, u64 len, si = subvol_uuid_search(&r->sus, 0, clone_uuid, clone_ctransid, NULL, subvol_search_by_received_uuid); - if (!si) { + if (IS_ERR(si)) { if (memcmp(clone_uuid, r->cur_subvol.received_uuid, BTRFS_UUID_SIZE) == 0) { /* TODO check generation of extent */ subvol_path = strdup(r->cur_subvol_path); } else { - ret = -ENOENT; + ret = PTR_ERR(si); error("clone: did not find source subvol"); goto out; } diff --git a/cmds-send.c b/cmds-send.c index 74d0128..b773b40 100644 --- a/cmds-send.c +++ b/cmds-send.c @@ -68,8 +68,8 @@ static int get_root_id(struct btrfs_send *s, const char *path, u64 *root_id) si = subvol_uuid_search(&s->sus, 0, NULL, 0, path, subvol_search_by_path); - if (!si) - return -ENOENT; + if (IS_ERR(si)) + return PTR_ERR(si); *root_id = si->root_id; free(si->path); free(si); @@ -83,8 +83,8 @@ static struct subvol_info *get_parent(struct btrfs_send *s, u64 root_id) si_tmp = subvol_uuid_search(&s->sus, root_id, NULL, 0, NULL, subvol_search_by_root_id); - if (!si_tmp) - return NULL; + if (IS_ERR(si_tmp)) + return si_tmp; si = subvol_uuid_search(&s->sus, 0, si_tmp->parent_uuid, 0, NULL, subvol_search_by_uuid); @@ -104,8 +104,8 @@ static int find_good_parent(struct btrfs_send *s, u64 root_id, u64 *found) int i; parent = get_parent(s, root_id); - if (!parent) { - ret = -ENOENT; + if (IS_ERR(parent)) { + ret = PTR_ERR(parent); goto out; } @@ -119,7 +119,7 @@ static int find_good_parent(struct btrfs_send *s, u64 root_id, u64 *found) for (i = 0; i < s->clone_sources_count; i++) { parent2 = get_parent(s, s->clone_sources[i]); - if (!parent2) + if (IS_ERR(parent2)) continue; if (parent2->root_id != parent->root_id) { free(parent2->path); @@ -133,8 +133,8 @@ static int find_good_parent(struct btrfs_send *s, u64 root_id, u64 *found) parent2 = subvol_uuid_search(&s->sus, s->clone_sources[i], NULL, 0, NULL, subvol_search_by_root_id); - if (!parent2) { - ret = -ENOENT; + if (IS_ERR(parent2)) { + ret = PTR_ERR(parent2); goto out; } tmp = parent2->ctransid - parent->ctransid; diff --git a/send-utils.c b/send-utils.c index a85fa08..87b8559 100644 --- a/send-utils.c +++ b/send-utils.c @@ -27,6 +27,7 @@ #include "send-utils.h" #include "ioctl.h" #include "btrfs-list.h" +#include "utils.h" static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len, u64 subvol_id); @@ -474,6 +475,11 @@ struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s, goto out; info = calloc(1, sizeof(*info)); + if (!info) { + error("Not enough memory"); + ret = -ENOMEM; + goto out; + } info->root_id = root_id; memcpy(info->uuid, root_item.uuid, BTRFS_UUID_SIZE); memcpy(info->received_uuid, root_item.received_uuid, BTRFS_UUID_SIZE); @@ -486,15 +492,23 @@ struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s, info->path = strdup(path); } else { info->path = malloc(PATH_MAX); + if (!info->path) { + error("Not enough memory"); + ret = -ENOMEM; + goto out; + } ret = btrfs_subvolid_resolve(s->mnt_fd, info->path, PATH_MAX, root_id); } out: - if (ret && info) { - free(info->path); - free(info); - info = NULL; + if (ret) { + if (info) { + free(info->path); + free(info); + info = NULL; + } + return ERR_PTR(ret); } return info;