]> xenbits.xensource.com Git - libvirt.git/commitdiff
Remove extra allocation in udevGetDeviceSysfsAttr
authorJán Tomko <jtomko@redhat.com>
Fri, 3 Jun 2016 15:13:39 +0000 (17:13 +0200)
committerJán Tomko <jtomko@redhat.com>
Tue, 7 Jun 2016 10:53:37 +0000 (12:53 +0200)
Most of the code paths free it right after converting it to
an integer.

src/node_device/node_device_udev.c

index c118b65cdaa6a0783179e6520eac2d33b155ef9b..696d6037cd61e51dc3a0dcbdc234e57a2bf3b389 100644 (file)
@@ -129,38 +129,17 @@ static int udevGetUintProperty(struct udev_device *udev_device,
 }
 
 
-/* This function allocates memory from the heap for the property
- * value.  That memory must be later freed by some other code.
- * Any control characters that cannot be printed in the XML are stripped
- * from the string */
-static int udevGetDeviceSysfsAttr(struct udev_device *udev_device,
-                                  const char *attr_name,
-                                  char **attr_value)
+static const char *udevGetDeviceSysfsAttr(struct udev_device *udev_device,
+                                          const char *attr_name)
 {
-    const char *udev_value = NULL;
-    int ret = PROPERTY_FOUND;
-
-    udev_value = udev_device_get_sysattr_value(udev_device, attr_name);
-    if (udev_value == NULL) {
-        VIR_DEBUG("udev reports device '%s' does not have sysfs attr '%s'",
-                  udev_device_get_sysname(udev_device), attr_name);
-        ret = PROPERTY_MISSING;
-        goto out;
-    }
+    const char *ret = NULL;
 
-    /* If this allocation is changed, the comment at the beginning
-     * of the function must also be changed. */
-    if (VIR_STRDUP(*attr_value, udev_value) < 0) {
-        ret = PROPERTY_ERROR;
-        goto out;
-    }
+    ret = udev_device_get_sysattr_value(udev_device, attr_name);
 
     VIR_DEBUG("Found sysfs attribute '%s' value '%s' "
               "for device with sysname '%s'",
-              attr_name, *attr_value,
+              attr_name, NULLSTR(ret),
               udev_device_get_sysname(udev_device));
-
- out:
     return ret;
 }
 
@@ -169,22 +148,15 @@ static int udevGetStringSysfsAttr(struct udev_device *udev_device,
                                   const char *attr_name,
                                   char **value)
 {
-    char *tmp = NULL;
-    int ret = PROPERTY_MISSING;
-
-    ret = udevGetDeviceSysfsAttr(udev_device, attr_name, &tmp);
+    if (VIR_STRDUP(*value, udevGetDeviceSysfsAttr(udev_device, attr_name)) < 0)
+        return PROPERTY_ERROR;
 
-    virStringStripControlChars(tmp);
+    virStringStripControlChars(*value);
 
-    if (tmp != NULL && (STREQ(tmp, ""))) {
-        VIR_FREE(tmp);
-        tmp = NULL;
-        ret = PROPERTY_MISSING;
-    }
-
-    *value = tmp;
+    if (*value != NULL && (STREQ(*value, "")))
+        VIR_FREE(*value);
 
-    return ret;
+    return *value == NULL ? PROPERTY_MISSING : PROPERTY_FOUND;
 }
 
 
@@ -193,20 +165,16 @@ static int udevGetIntSysfsAttr(struct udev_device *udev_device,
                                int *value,
                                int base)
 {
-    char *udev_value = NULL;
-    int ret = PROPERTY_FOUND;
+    const char *str = NULL;
 
-    ret = udevGetDeviceSysfsAttr(udev_device, attr_name, &udev_value);
+    str = udevGetDeviceSysfsAttr(udev_device, attr_name);
 
-    if (ret == PROPERTY_FOUND) {
-        if (virStrToLong_i(udev_value, NULL, base, value) < 0) {
-            VIR_ERROR(_("Failed to convert '%s' to int"), udev_value);
-            ret = PROPERTY_ERROR;
-        }
+    if (str && virStrToLong_i(str, NULL, base, value) < 0) {
+        VIR_ERROR(_("Failed to convert '%s' to int"), str);
+        return PROPERTY_ERROR;
     }
 
-    VIR_FREE(udev_value);
-    return ret;
+    return str == NULL ? PROPERTY_MISSING : PROPERTY_FOUND;
 }
 
 
@@ -215,20 +183,16 @@ static int udevGetUintSysfsAttr(struct udev_device *udev_device,
                                 unsigned int *value,
                                 int base)
 {
-    char *udev_value = NULL;
-    int ret = PROPERTY_FOUND;
+    const char *str = NULL;
 
-    ret = udevGetDeviceSysfsAttr(udev_device, attr_name, &udev_value);
+    str = udevGetDeviceSysfsAttr(udev_device, attr_name);
 
-    if (ret == PROPERTY_FOUND) {
-        if (virStrToLong_ui(udev_value, NULL, base, value) < 0) {
-            VIR_ERROR(_("Failed to convert '%s' to unsigned int"), udev_value);
-            ret = PROPERTY_ERROR;
-        }
+    if (str && virStrToLong_ui(str, NULL, base, value) < 0) {
+        VIR_ERROR(_("Failed to convert '%s' to unsigned int"), str);
+        return PROPERTY_ERROR;
     }
 
-    VIR_FREE(udev_value);
-    return ret;
+    return str == NULL ? PROPERTY_MISSING : PROPERTY_FOUND;
 }
 
 
@@ -236,20 +200,16 @@ static int udevGetUint64SysfsAttr(struct udev_device *udev_device,
                                   const char *attr_name,
                                   unsigned long long *value)
 {
-    char *udev_value = NULL;
-    int ret = PROPERTY_FOUND;
+    const char *str = NULL;
 
-    ret = udevGetDeviceSysfsAttr(udev_device, attr_name, &udev_value);
+    str = udevGetDeviceSysfsAttr(udev_device, attr_name);
 
-    if (ret == PROPERTY_FOUND) {
-        if (virStrToLong_ull(udev_value, NULL, 0, value) < 0) {
-            VIR_ERROR(_("Failed to convert '%s' to unsigned long long"), udev_value);
-            ret = PROPERTY_ERROR;
-        }
+    if (str && virStrToLong_ull(str, NULL, 0, value) < 0) {
+        VIR_ERROR(_("Failed to convert '%s' to unsigned long long"), str);
+        return PROPERTY_ERROR;
     }
 
-    VIR_FREE(udev_value);
-    return ret;
+    return str == NULL ? PROPERTY_MISSING : PROPERTY_FOUND;
 }