[libvirt] [PATCH] Use virMacAddrCompare() in xs_internal.c

# HG changeset patch # User john.levon@sun.com # Date 1233800565 28800 # Node ID 379763c63798c9f0c426facb9b9b61e34e6477e2 # Parent e6b17082b9b9440ce14ad76bd2f4c003ae2404db Use virMacAddrCompare() in xs_internal.c This solves a failure to look up a net device. Signed-off-by: John Levon <john.levon@sun.com> diff --git a/src/xs_internal.c b/src/xs_internal.c --- a/src/xs_internal.c +++ b/src/xs_internal.c @@ -915,7 +915,7 @@ char * char * xenStoreDomainGetNetworkID(virConnectPtr conn, int id, const char *mac) { char dir[80], path[128], **list = NULL, *val = NULL; - unsigned int maclen, len, i, num; + unsigned int len, i, num; char *ret = NULL; xenUnifiedPrivatePtr priv; @@ -926,9 +926,6 @@ xenStoreDomainGetNetworkID(virConnectPtr if (priv->xshandle == NULL) return (NULL); if (mac == NULL) - return (NULL); - maclen = strlen(mac); - if (maclen <= 0) return (NULL); snprintf(dir, sizeof(dir), "/local/domain/0/backend/vif/%d", id); @@ -937,18 +934,20 @@ xenStoreDomainGetNetworkID(virConnectPtr return(NULL); for (i = 0; i < num; i++) { snprintf(path, sizeof(path), "%s/%s/%s", dir, list[i], "mac"); - val = xs_read(priv->xshandle, 0, path, &len); - if (val == NULL) + if ((val = xs_read(priv->xshandle, 0, path, &len)) == NULL) break; - if ((maclen != len) || memcmp(val, mac, len)) { - free(val); - } else { + + if (virMacAddrCompare(val, mac) == 0) { ret = strdup(list[i]); - free(val); - break; + goto out; } - } - free(list); + + VIR_FREE(val); + } + +out: + VIR_FREE(val); + VIR_FREE(list); return(ret); }

john.levon@sun.com wrote:
Use virMacAddrCompare() in xs_internal.c
This solves a failure to look up a net device.
Please take the time to describe the failure scenario in enough detail that someone (maybe me) can reproduce it. IMHO, a bug-fix patch like this is best accompanied by a test case that fails before the patch and succeeds with it. The patch looks ok, but doesn't need the new label. Nor does it need to free "val" outside the loop. I've included a suggested incremental patch below.
diff --git a/src/xs_internal.c b/src/xs_internal.c --- a/src/xs_internal.c +++ b/src/xs_internal.c @@ -915,7 +915,7 @@ char * char * xenStoreDomainGetNetworkID(virConnectPtr conn, int id, const char *mac) { char dir[80], path[128], **list = NULL, *val = NULL;
Also, though I know it's not your code, both of the above initializations are useless and should be removed. That would be obvious if each of the variables was declared at first use.
- unsigned int maclen, len, i, num; + unsigned int len, i, num; char *ret = NULL; xenUnifiedPrivatePtr priv;
@@ -926,9 +926,6 @@ xenStoreDomainGetNetworkID(virConnectPtr if (priv->xshandle == NULL) return (NULL); if (mac == NULL) - return (NULL); - maclen = strlen(mac); - if (maclen <= 0) return (NULL);
snprintf(dir, sizeof(dir), "/local/domain/0/backend/vif/%d", id); @@ -937,18 +934,20 @@ xenStoreDomainGetNetworkID(virConnectPtr return(NULL); for (i = 0; i < num; i++) { snprintf(path, sizeof(path), "%s/%s/%s", dir, list[i], "mac"); - val = xs_read(priv->xshandle, 0, path, &len); - if (val == NULL) + if ((val = xs_read(priv->xshandle, 0, path, &len)) == NULL) break; - if ((maclen != len) || memcmp(val, mac, len)) { - free(val); - } else { + + if (virMacAddrCompare(val, mac) == 0) { ret = strdup(list[i]); - free(val); - break; + goto out; } - } - free(list); + + VIR_FREE(val); + } + +out: + VIR_FREE(val); + VIR_FREE(list); return(ret); }
diff --git a/src/xs_internal.c b/src/xs_internal.c index ce707e9..1b4284f 100644 --- a/src/xs_internal.c +++ b/src/xs_internal.c @@ -937,16 +937,14 @@ xenStoreDomainGetNetworkID(virConnectPtr conn, int id, const char *mac) { if ((val = xs_read(priv->xshandle, 0, path, &len)) == NULL) break; - if (virMacAddrCompare(val, mac) == 0) { + bool match = (virMacAddrCompare(val, mac) == 0); + VIR_FREE(val); + if (match) { ret = strdup(list[i]); - goto out; + break; } - - VIR_FREE(val); } -out: - VIR_FREE(val); VIR_FREE(list); return(ret); }

On Thu, Feb 05, 2009 at 08:00:04AM +0100, Jim Meyering wrote:
Use virMacAddrCompare() in xs_internal.c
This solves a failure to look up a net device.
Please take the time to describe the failure scenario in enough detail that someone (maybe me) can reproduce it.
OK. The problem was one of case, when I tried to remove an interface from a running domain, the xenstore entry would not match as one was in lower-case and the other in upper-case.
I've included a suggested incremental patch below.
I'll use your changes. thanks john

On Wed, Feb 04, 2009 at 08:41:59PM -0800, john.levon@sun.com wrote:
# HG changeset patch # User john.levon@sun.com # Date 1233800565 28800 # Node ID 379763c63798c9f0c426facb9b9b61e34e6477e2 # Parent e6b17082b9b9440ce14ad76bd2f4c003ae2404db Use virMacAddrCompare() in xs_internal.c
This solves a failure to look up a net device.
Signed-off-by: John Levon <john.levon@sun.com>
ACK 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 :|
participants (4)
-
Daniel P. Berrange
-
Jim Meyering
-
John Levon
-
john.levon@sun.com