[PATCH 13/13] lib: Prefer sizeof(variable) instead of sizeof(type) in memset

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <mprivozn@xxxxxxxxxx>
---
 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,
 
 #define VIR_IOTHREAD_SET_PROP_UL(propName, propVal) \
     if (iothreadInfo->set_##propVal) { \
-        memset(&prop, 0, sizeof(qemuMonitorJSONObjectProperty)); \
+        memset(&prop, 0, sizeof(prop)); \
         prop.type = QEMU_MONITOR_OBJECT_PROPERTY_ULONG; \
         prop.val.ul = iothreadInfo->propVal; \
         if (qemuMonitorJSONSetObjectProperty(mon, path, propName, &prop) < 0) \
@@ -7145,7 +7145,7 @@ qemuMonitorJSONSetIOThread(qemuMonitor *mon,
 
 #define VIR_IOTHREAD_SET_PROP_INT(propName, propVal) \
     if (iothreadInfo->set_##propVal) { \
-        memset(&prop, 0, sizeof(qemuMonitorJSONObjectProperty)); \
+        memset(&prop, 0, sizeof(prop)); \
         prop.type = QEMU_MONITOR_OBJECT_PROPERTY_INT; \
         prop.val.iv = 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 = g_strdup_printf("%s/%s.sock", priv->libDir,
                                                       type);
                     glisten->fromConfig = 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;
     }
 
-    memset(info, 0, sizeof(virVcpuInfo) * maxinfo);
+    memset(info, 0, sizeof(*info) * maxinfo);
     memset(cpumaps, 0, maxinfo * maplen);
 
     for (i = 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) == NULL)
         goto cleanup;
 
-    memset(info, 0, sizeof(virStoragePoolInfo));
+    memset(info, 0, sizeof(*info));
     if (virStoragePoolObjIsActive(obj))
         info->state = 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,
 
     if (errors) {
         /* sanitize input */
-        memset(errors, 0, sizeof(virDomainDiskError) * nerrors);
+        memset(errors, 0, sizeof(*errors) * nerrors);
 
         for (i = 0; i < nerrors; i++) {
             errors[i].disk = g_strdup(vm->def->disks[i]->dst);
@@ -6878,7 +6878,7 @@ testStoragePoolGetInfo(virStoragePoolPtr pool,
         return -1;
     def = virStoragePoolObjGetDef(obj);
 
-    memset(info, 0, sizeof(virStoragePoolInfo));
+    memset(info, 0, sizeof(*info));
     if (virStoragePoolObjIsActive(obj))
         info->state = VIR_STORAGE_POOL_RUNNING;
     else
-- 
2.41.0




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux