From nobody Tue Feb 10 14:15:16 2026 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1622563177; cv=none; d=zohomail.com; s=zohoarc; b=E/aySyZTTaHW2ju8cxGvHwLlLxG6w7HiOcNZx0qHSU0yjjbmb1OfdaqULHUCjWuiLL9I1YcG4RR5HVmvvNtUDmBkdFghQuzkHSdK0Vahnt7tzml39NHhtrAiS5rcjpaG0rCkr/v9JJh5XrjQAasuU++pbTBtPNs+5ZFwYiYGaJ4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622563177; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ln7dKDIdS2Ok07dRe0TxA9iBDNDE9dyXN/6L3bb8Xec=; b=kMZHeSfDj8PNmMVw5LzOr/z2Em2zAda1DiDc1Wlz+iL1xYshWRV8I1MLc7pAQM3TQsPefyqYcCAqT1k7te2/ka/9i16ezAdlW2SEA4s8R504w3UEH4TsG0m4gPbUeMjYWasVdcHXgC3BVCbTmk9lVwr0iPtaeptD3Hm5seh8KVU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162256317714622.517962181787993; Tue, 1 Jun 2021 08:59:37 -0700 (PDT) Received: from localhost ([::1]:49412 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lo6nn-0000HO-Rs for importer2@patchew.org; Tue, 01 Jun 2021 11:59:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58846) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lo6mY-00070Z-H2 for qemu-devel@nongnu.org; Tue, 01 Jun 2021 11:58:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:50189) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lo6mV-0007vW-7x for qemu-devel@nongnu.org; Tue, 01 Jun 2021 11:58:18 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-269-3YyEhxl6OOyhFaEevcNU4Q-1; Tue, 01 Jun 2021 11:58:12 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ECDEF81840B; Tue, 1 Jun 2021 15:58:10 +0000 (UTC) Received: from localhost (ovpn-113-244.ams2.redhat.com [10.36.113.244]) by smtp.corp.redhat.com (Postfix) with ESMTP id 60D2219C44; Tue, 1 Jun 2021 15:58:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622563094; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ln7dKDIdS2Ok07dRe0TxA9iBDNDE9dyXN/6L3bb8Xec=; b=dQ0BP2Zp4tz/FYfyYUb165oafGq71iDWyHwr9JegfxcNYIebpC7XQNwtB14CbckHkxF9dR 1tnZkDkBu96yFERFgOcjh4N3zPRKLlYNnZE/CNpn3Q5qrn+IXHKnnuTIiNtpnkkHtu5Nxx KT1Z3rZdtXKXfHdvfC6+lj4AYHrqUdI= X-MC-Unique: 3YyEhxl6OOyhFaEevcNU4Q-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH v2 1/2] vhost-user-blk-test: fix Coverity open(2) false positives Date: Tue, 1 Jun 2021 16:57:54 +0100 Message-Id: <20210601155755.216949-2-stefanha@redhat.com> In-Reply-To: <20210601155755.216949-1-stefanha@redhat.com> References: <20210601155755.216949-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.371, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Kevin Wolf , Thomas Huth , qemu-block@nongnu.org, Peter Maydell , Coiby Xu , Gerd Hoffmann , Stefan Hajnoczi , Paolo Bonzini Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Coverity checks that the file descriptor return value of open(2) is checked and used. Normally this is helpful in identifying real bugs but vhost-user-blk-test opens /dev/null as stdin and stdout after fork. In this case we don't need to look at the return value because these open(2) calls cannot fail in any reasonable environment. We already know their return values ahead of time since we closed stdin and stdout previously. open(2) is guaranteed to pick the lowest available fd number. Silence Coverity by introducing code that checks what we already know to be true. ** CID 1453270: Resource leaks (RESOURCE_LEAK) /qemu/tests/qtest/vhost-user-blk-test.c: 920 in start_vhost_user_blk() ___________________________________________________________________________= _____________________________ *** CID 1453270: Resource leaks (RESOURCE_LEAK) /qemu/tests/qtest/vhost-user-blk-test.c: 920 in start_vhost_user_blk() 914 * Close standard file descriptors so tap-driver.pl pipe de= tects when 915 * our parent terminates. 916 */ 917 close(0); 918 close(1); 919 open("/dev/null", O_RDONLY); >>> CID 1453270: Resource leaks (RESOURCE_LEAK) >>> Ignoring handle opened by "open("/dev/null", 1)" leaks it. 920 open("/dev/null", O_WRONLY); 921 922 execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, = NULL); 923 exit(1); 924 } 925 g_string_free(storage_daemon_command, true); ** CID 1453269: Error handling issues (NEGATIVE_RETURNS) /qemu/tests/qtest/vhost-user-blk-test.c: 829 in create_listen_socket() ___________________________________________________________________________= _____________________________ *** CID 1453269: Error handling issues (NEGATIVE_RETURNS) /qemu/tests/qtest/vhost-user-blk-test.c: 829 in create_listen_socket() 823 char *path; 824 825 /* No race because our pid makes the path unique */ 826 path =3D g_strdup_printf("/tmp/qtest-%d-sock.XXXXXX", getpid()); 827 tmp_fd =3D mkstemp(path); 828 g_assert_cmpint(tmp_fd, >=3D, 0); >>> CID 1453269: Error handling issues (NEGATIVE_RETURNS) >>> "tmp_fd" is passed to a parameter that cannot be negative. 829 close(tmp_fd); 830 unlink(path); 831 832 *fd =3D qtest_socket_server(path); 833 g_test_queue_destroy(destroy_file, path); 834 return path; ** CID 1453268: (CHECKED_RETURN) /qemu/tests/qtest/vhost-user-blk-test.c: 920 in start_vhost_user_blk() /qemu/tests/qtest/vhost-user-blk-test.c: 919 in start_vhost_user_blk() ___________________________________________________________________________= _____________________________ *** CID 1453268: (CHECKED_RETURN) /qemu/tests/qtest/vhost-user-blk-test.c: 920 in start_vhost_user_blk() 914 * Close standard file descriptors so tap-driver.pl pipe de= tects when 915 * our parent terminates. 916 */ 917 close(0); 918 close(1); 919 open("/dev/null", O_RDONLY); >>> CID 1453268: (CHECKED_RETURN) >>> Calling "open("/dev/null", 1)" without checking return value. This = library function may fail and return an error code. [Note: The source code = implementation of the function has been overridden by a builtin model.] 920 open("/dev/null", O_WRONLY); 921 922 execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, = NULL); 923 exit(1); 924 } 925 g_string_free(storage_daemon_command, true); /qemu/tests/qtest/vhost-user-blk-test.c: 919 in start_vhost_user_blk() 913 /* 914 * Close standard file descriptors so tap-driver.pl pipe de= tects when 915 * our parent terminates. 916 */ 917 close(0); 918 close(1); >>> CID 1453268: (CHECKED_RETURN) >>> Calling "open("/dev/null", 0)" without checking return value. This = library function may fail and return an error code. [Note: The source code = implementation of the function has been overridden by a builtin model.] 919 open("/dev/null", O_RDONLY); 920 open("/dev/null", O_WRONLY); 921 922 execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, = NULL); 923 exit(1); 924 } Signed-off-by: Stefan Hajnoczi Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Thomas Huth --- tests/qtest/vhost-user-blk-test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/qtest/vhost-user-blk-test.c b/tests/qtest/vhost-user-blk= -test.c index 8796c74ca4..c5c4667759 100644 --- a/tests/qtest/vhost-user-blk-test.c +++ b/tests/qtest/vhost-user-blk-test.c @@ -910,14 +910,18 @@ static void start_vhost_user_blk(GString *cmd_line, i= nt vus_instances, storage_daemon_command->str); pid_t pid =3D fork(); if (pid =3D=3D 0) { + int fd; + /* * Close standard file descriptors so tap-driver.pl pipe detects w= hen * our parent terminates. */ close(0); + fd =3D open("/dev/null", O_RDONLY); + g_assert_cmpint(fd, =3D=3D, 0); close(1); - open("/dev/null", O_RDONLY); - open("/dev/null", O_WRONLY); + fd =3D open("/dev/null", O_WRONLY); + g_assert_cmpint(fd, =3D=3D, 1); =20 execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, NULL); exit(1); --=20 2.31.1