[libvirt] review fallout: short memset

While reviewing unrelated changes, I spotted a short memset: char **names; ... memset(names, 0, maxnames); That zeros out 1/4 or 1/8 of the memory than it should. It should be doing this: memset(names, 0, maxnames * sizeof (*names)); I checked all memset uses and found a total of 6 uses like that. This fixes them:
From 614b0064eadc3ecef0690d5c65bb531844a3091d Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Tue, 2 Dec 2008 14:49:03 +0100 Subject: [PATCH] fix inadequate initialization in storage and test drivers
* src/storage_driver.c (storageListPools): Set all "names" entries to 0. (storageListDefinedPools, storagePoolListVolumes): Likewise. * src/test.c (testStoragePoolListVolumes): Likewise. --- src/storage_driver.c | 8 ++++---- src/test.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/storage_driver.c b/src/storage_driver.c index 366820b..53388f1 100644 --- a/src/storage_driver.c +++ b/src/storage_driver.c @@ -347,7 +347,7 @@ storageListPools(virConnectPtr conn, free(names[i]); names[i] = NULL; } - memset(names, 0, nnames); + memset(names, 0, nnames * sizeof(*names)); return -1; } @@ -389,7 +389,7 @@ storageListDefinedPools(virConnectPtr conn, free(names[i]); names[i] = NULL; } - memset(names, 0, nnames); + memset(names, 0, nnames * sizeof(*names)); return -1; } @@ -880,7 +880,7 @@ storagePoolListVolumes(virStoragePoolPtr obj, return -1; } - memset(names, 0, maxnames); + memset(names, 0, maxnames * sizeof(*names)); for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) { if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) { virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY, @@ -895,7 +895,7 @@ storagePoolListVolumes(virStoragePoolPtr obj, for (n = 0 ; n < maxnames ; n++) VIR_FREE(names[i]); - memset(names, 0, maxnames); + memset(names, 0, maxnames * sizeof(*names)); return -1; } diff --git a/src/test.c b/src/test.c index 7998886..3e942da 100644 --- a/src/test.c +++ b/src/test.c @@ -1951,7 +1951,7 @@ testStoragePoolListVolumes(virStoragePoolPtr obj, POOL_IS_ACTIVE(privpool, -1); int i = 0, n = 0; - memset(names, 0, maxnames); + memset(names, 0, maxnames * sizeof(*names)); for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) { if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) { testError(obj->conn, VIR_ERR_NO_MEMORY, "%s", _("name")); @@ -1965,7 +1965,7 @@ testStoragePoolListVolumes(virStoragePoolPtr obj, for (n = 0 ; n < maxnames ; n++) VIR_FREE(names[i]); - memset(names, 0, maxnames); + memset(names, 0, maxnames * sizeof(*names)); return -1; } -- 1.6.0.4.1044.g77718

On Tue, Dec 02, 2008 at 03:02:53PM +0100, Jim Meyering wrote:
While reviewing unrelated changes, I spotted a short memset:
char **names; ... memset(names, 0, maxnames);
That zeros out 1/4 or 1/8 of the memory than it should. It should be doing this:
memset(names, 0, maxnames * sizeof (*names));
I checked all memset uses and found a total of 6 uses like that. This fixes them:
ACK to this immediate fix. As per my other mail we should consider adding a VIR_ZERO() macro for this, to avoid such errors recurring Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

On Tue, Dec 02, 2008 at 02:18:43PM +0000, Daniel P. Berrange wrote:
On Tue, Dec 02, 2008 at 03:02:53PM +0100, Jim Meyering wrote:
While reviewing unrelated changes, I spotted a short memset:
char **names; ... memset(names, 0, maxnames);
That zeros out 1/4 or 1/8 of the memory than it should. It should be doing this:
memset(names, 0, maxnames * sizeof (*names));
I checked all memset uses and found a total of 6 uses like that. This fixes them:
ACK to this immediate fix. As per my other mail we should consider adding a VIR_ZERO() macro for this, to avoid such errors recurring
+1 to the fix and VIR_ZERO() Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/
participants (3)
-
Daniel P. Berrange
-
Daniel Veillard
-
Jim Meyering