From patchwork Thu Nov 9 15:30:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 13451211 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3601EC4332F for ; Thu, 9 Nov 2023 15:31:10 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r16zW-0007rP-AM; Thu, 09 Nov 2023 10:30:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r16zS-0007iH-5T; Thu, 09 Nov 2023 10:30:42 -0500 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r16zN-0003MD-SC; Thu, 09 Nov 2023 10:30:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=MIME-Version:Content-Type:Date:Cc:To: From:Subject:Message-ID:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=YNZrzaqqF9dIWOsyly6LCz18pJYLR22j/p8dMcADsMg=; b=JsCckEeibO4jmHHALfpzRRyyfk SOnYQjZMKwxGsalgnmI7c/5cQKr8lAl+sGdhI8ufg+uK8ZCzS6FwR0gml0IgFvmvY1J80/S4cFL9Y 2wDea/EXusReOZOXrKyg8+wIRc8M0TU+eKyOkC4bmn6CMgOm1aXJj38vTotAD0NZggZI1hYV+Wawm wZYYhK1XfNrXUhIX6KZz5tLhkx6oZ46IUFDF4HpUGk1kAJxyM6YzttxUlG0ndjI/sS7YCmQYiEelZ i52jc46/lahHad9dnjVcR8m72jsF2NNtK3seL2p8GZt8uLmqoesi+cqMRUBaXOk/yiKyHhg8+HRvl dEUCN4tg==; Received: from [2001:8b0:10b:5:e60c:fbc4:6b73:8eff] (helo=u3832b3a9db3152.ant.amazon.com) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1r16zI-0080Jc-Ho; Thu, 09 Nov 2023 15:30:33 +0000 Message-ID: <1c312ba88e0928527dad6bc2e1b73d8cfe4b7f48.camel@infradead.org> Subject: [PATCH] hw/xen: clean up xen_block_find_free_vdev() to avoid Coverity false positive From: David Woodhouse To: Peter Maydell , qemu-devel@nongnu.org Cc: Stefano Stabellini , Anthony Perard , Paul Durrant , Kevin Wolf , Hanna Reitz , xen-devel@lists.xenproject.org, qemu-block@nongnu.org Date: Thu, 09 Nov 2023 15:30:32 +0000 User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Received-SPF: none client-ip=2001:8b0:10b:1236::1; envelope-from=BATV+c182118e23cc89d32fc1+7382+infradead.org+dwmw2@casper.srs.infradead.org; helo=casper.infradead.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: David Woodhouse Coverity couldn't see that nr_existing was always going to be zero when qemu_xen_xs_directory() returned NULL in the ENOENT case (CID 1523906). Perhaps more to the point, neither could Peter at first glance. Improve the code to hopefully make it clearer to Coverity and human reviewers alike. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant ---  hw/block/xen-block.c | 24 +++++++++++++++++++++---  1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/hw/block/xen-block.c b/hw/block/xen-block.c index 6d64ede94f..aed1d5c330 100644 --- a/hw/block/xen-block.c +++ b/hw/block/xen-block.c @@ -91,9 +91,27 @@ static bool xen_block_find_free_vdev(XenBlockDevice *blockdev, Error **errp)        existing_frontends = qemu_xen_xs_directory(xenbus->xsh, XBT_NULL, fe_path,                                                 &nr_existing); -    if (!existing_frontends && errno != ENOENT) { -        error_setg_errno(errp, errno, "cannot read %s", fe_path); -        return false; +    if (!existing_frontends) { +        if (errno == ENOENT) { +            /* +             * If the frontend directory doesn't exist because there are +             * no existing vbd devices, that's fine. Just ensure that we +             * don't dereference the NULL existing_frontends pointer, by +             * checking that nr_existing is zero so the loop below is not +             * entered. +             * +             * In fact this is redundant since nr_existing is initialized +             * to zero, but setting it again here makes it abundantly clear +             * to Coverity, and to the human reader who doesn't know the +             * semantics of qemu_xen_xs_directory() off the top of their +             * head. +             */ +            nr_existing = 0; +        } else { +            /* All other errors accessing the frontend directory are fatal. */ +            error_setg_errno(errp, errno, "cannot read %s", fe_path); +            return false; +        }      }        memset(used_devs, 0, sizeof(used_devs));