From nobody Sun Feb 8 12:43:29 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1691059016; cv=none; d=zohomail.com; s=zohoarc; b=g4ilTvAegy4rMplTR+y0s400nwqsqkhfKxRgeGDsqp1dIAn4lr+vwh+9VtHHM2GkjiK+CxGgqnMMIv/TYkSjWpHd44F39on8mqLNj/qzcieObs3wG76noU9eKILMYyador7ND+LM6KfJGLjOJA8sz8oiOE1VtvAQVfXgr9GO4AU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1691059016; h=Content-Type:Content-Transfer-Encoding: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=J9hoECHcqKFuaAYAk2eWU6l8bhfmMKfHyK2h/793oGs=; b=G8wI8sCquFNT7B+BDMilW7hIHeTYQ9/I7y6f59f5szxnIG6yidzNnY84m7pyG+6bwnRPhjCQsiEnMLsY8xBoUKX1i1MvWuAwH6JGoHMVR2Wfi16u3mgOQe2irsYOe+/D2ncCa4KMyxgp2WS2Gz0cVpawW4ZdVuDF5OAY5PMTNW8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 169105901657352.791188797692485; Thu, 3 Aug 2023 03:36:56 -0700 (PDT) Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-691-1yk1J2oNODeYwOt8eucyXQ-1; Thu, 03 Aug 2023 06:36:50 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8E9282815E21; Thu, 3 Aug 2023 10:36:48 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 77D51112132E; Thu, 3 Aug 2023 10:36:48 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 5DBE71947217; Thu, 3 Aug 2023 10:36:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id EE6EB1947F00 for ; Thu, 3 Aug 2023 10:36:30 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id D2150F7FA6; Thu, 3 Aug 2023 10:36:30 +0000 (UTC) Received: from localhost.localdomain (unknown [10.43.2.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 792E0F77BD for ; Thu, 3 Aug 2023 10:36:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691059015; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=J9hoECHcqKFuaAYAk2eWU6l8bhfmMKfHyK2h/793oGs=; b=ehgCbdiuUgJ2OAo0s6HPJm+dJJ8x+zB5V53esmCkFenBYq7bqPKDuwlzKz9HKzHDltgLGT jBm+YW0v++WLImyFgO4hqudLtYUlpf2x6Qcxz20GvIcHEMA4g+pgnIhN/WIJSnlGe5JpKI tzMWEsmXeRIX34MSOut4ABY9/YS7wiw= X-MC-Unique: 1yk1J2oNODeYwOt8eucyXQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 13/13] lib: Prefer sizeof(variable) instead of sizeof(type) in memset Date: Thu, 3 Aug 2023 12:36:20 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1691059018051100001 Content-Type: text/plain; charset="utf-8"; x-default="true" If one of previous commits taught us something, it's that: sizeof(variable) and sizeof(type) are not the same. Especially because for live enough code the type might change (e.g. as we use autoptr more). And since we don't get any warnings when an incorrect length is passed to memset() it is easy to mess up. But with sizeof(variable) instead, it's not as easy. Therefore, switch to using memset(variable, 0, sizeof(*variable)), or its alternatives, depending on level of pointers. Signed-off-by: Michal Privoznik Reviewed-by: Claudio Fontana --- src/qemu/qemu_monitor_json.c | 4 ++-- src/qemu/qemu_process.c | 2 +- src/remote/remote_driver.c | 2 +- src/storage/storage_driver.c | 2 +- src/test/test_driver.c | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 70536028ab..34c4b543e8 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -7112,7 +7112,7 @@ qemuMonitorJSONSetIOThread(qemuMonitor *mon, =20 #define VIR_IOTHREAD_SET_PROP_UL(propName, propVal) \ if (iothreadInfo->set_##propVal) { \ - memset(&prop, 0, sizeof(qemuMonitorJSONObjectProperty)); \ + memset(&prop, 0, sizeof(prop)); \ prop.type =3D QEMU_MONITOR_OBJECT_PROPERTY_ULONG; \ prop.val.ul =3D iothreadInfo->propVal; \ if (qemuMonitorJSONSetObjectProperty(mon, path, propName, &prop) <= 0) \ @@ -7145,7 +7145,7 @@ qemuMonitorJSONSetIOThread(qemuMonitor *mon, =20 #define VIR_IOTHREAD_SET_PROP_INT(propName, propVal) \ if (iothreadInfo->set_##propVal) { \ - memset(&prop, 0, sizeof(qemuMonitorJSONObjectProperty)); \ + memset(&prop, 0, sizeof(prop)); \ prop.type =3D QEMU_MONITOR_OBJECT_PROPERTY_INT; \ prop.val.iv =3D iothreadInfo->propVal; \ if (qemuMonitorJSONSetObjectProperty(mon, path, propName, &prop) <= 0) \ diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 572b842349..0644f80161 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -5068,7 +5068,7 @@ qemuProcessGraphicsSetupListen(virQEMUDriver *driver, * *_auto_unix_socket set we should use unix socket as * default instead of tcp listen. */ if (useSocket) { - memset(glisten, 0, sizeof(virDomainGraphicsListenDef)); + memset(glisten, 0, sizeof(*glisten)); glisten->socket =3D g_strdup_printf("%s/%s.sock", priv= ->libDir, type); glisten->fromConfig =3D true; diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 8e78af0ea0..faad7292ed 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -2138,7 +2138,7 @@ remoteDomainGetVcpus(virDomainPtr domain, goto cleanup; } =20 - memset(info, 0, sizeof(virVcpuInfo) * maxinfo); + memset(info, 0, sizeof(*info) * maxinfo); memset(cpumaps, 0, maxinfo * maplen); =20 for (i =3D 0; i < ret.info.info_len; ++i) { diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c index c19abdd2f3..5a9dcbd193 100644 --- a/src/storage/storage_driver.c +++ b/src/storage/storage_driver.c @@ -1279,7 +1279,7 @@ storagePoolGetInfo(virStoragePoolPtr pool, if (virStorageBackendForType(def->type) =3D=3D NULL) goto cleanup; =20 - memset(info, 0, sizeof(virStoragePoolInfo)); + memset(info, 0, sizeof(*info)); if (virStoragePoolObjIsActive(obj)) info->state =3D VIR_STORAGE_POOL_RUNNING; else diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 3767908d9d..4b8e02c684 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -4826,7 +4826,7 @@ static int testDomainGetDiskErrors(virDomainPtr dom, =20 if (errors) { /* sanitize input */ - memset(errors, 0, sizeof(virDomainDiskError) * nerrors); + memset(errors, 0, sizeof(*errors) * nerrors); =20 for (i =3D 0; i < nerrors; i++) { errors[i].disk =3D g_strdup(vm->def->disks[i]->dst); @@ -6878,7 +6878,7 @@ testStoragePoolGetInfo(virStoragePoolPtr pool, return -1; def =3D virStoragePoolObjGetDef(obj); =20 - memset(info, 0, sizeof(virStoragePoolInfo)); + memset(info, 0, sizeof(*info)); if (virStoragePoolObjIsActive(obj)) info->state =3D VIR_STORAGE_POOL_RUNNING; else --=20 2.41.0