提交 0d372687 编写于 作者: J Ján Tomko

Do not VIR_STRDUP the string in udevGetDeviceProperty

Two out of three callers free it right after converting it to a number.

Also change the comment at the beginning of the function, because
the comment inside the function told me to.
上级 fde3a38e
...@@ -58,36 +58,16 @@ struct _udevPrivate { ...@@ -58,36 +58,16 @@ struct _udevPrivate {
}; };
/* This function allocates memory from the heap for the property static const char *udevGetDeviceProperty(struct udev_device *udev_device,
* value. That memory must be later freed by some other code. */ const char *property_key)
static int udevGetDeviceProperty(struct udev_device *udev_device,
const char *property_key,
char **property_value)
{ {
const char *udev_value = NULL; const char *ret = NULL;
int ret = PROPERTY_FOUND;
udev_value = udev_device_get_property_value(udev_device, property_key); ret = udev_device_get_property_value(udev_device, property_key);
if (udev_value == NULL) {
VIR_DEBUG("udev reports device '%s' does not have property '%s'",
udev_device_get_sysname(udev_device), property_key);
ret = PROPERTY_MISSING;
goto out;
}
/* If this allocation is changed, the comment at the beginning VIR_DEBUG("Found property key '%s' value '%s' for device with sysname '%s'",
* of the function must also be changed. */ property_key, NULLSTR(ret), udev_device_get_sysname(udev_device));
if (VIR_STRDUP(*property_value, udev_value) < 0) {
ret = PROPERTY_ERROR;
goto out;
}
VIR_DEBUG("Found property key '%s' value '%s' "
"for device with sysname '%s'",
property_key, *property_value,
udev_device_get_sysname(udev_device));
out:
return ret; return ret;
} }
...@@ -96,7 +76,11 @@ static int udevGetStringProperty(struct udev_device *udev_device, ...@@ -96,7 +76,11 @@ static int udevGetStringProperty(struct udev_device *udev_device,
const char *property_key, const char *property_key,
char **value) char **value)
{ {
return udevGetDeviceProperty(udev_device, property_key, value); if (VIR_STRDUP(*value,
udevGetDeviceProperty(udev_device, property_key)) < 0)
return PROPERTY_ERROR;
return *value == NULL ? PROPERTY_MISSING : PROPERTY_FOUND;
} }
...@@ -105,20 +89,15 @@ static int udevGetIntProperty(struct udev_device *udev_device, ...@@ -105,20 +89,15 @@ static int udevGetIntProperty(struct udev_device *udev_device,
int *value, int *value,
int base) int base)
{ {
char *udev_value = NULL; const char *str = NULL;
int ret = PROPERTY_FOUND;
ret = udevGetDeviceProperty(udev_device, property_key, &udev_value); str = udevGetDeviceProperty(udev_device, property_key);
if (ret == PROPERTY_FOUND) { if (str && virStrToLong_i(str, NULL, base, value) < 0) {
if (virStrToLong_i(udev_value, NULL, base, value) < 0) { VIR_ERROR(_("Failed to convert '%s' to int"), str);
VIR_ERROR(_("Failed to convert '%s' to int"), udev_value); return PROPERTY_ERROR;
ret = PROPERTY_ERROR;
}
} }
return str == NULL ? PROPERTY_MISSING : PROPERTY_FOUND;
VIR_FREE(udev_value);
return ret;
} }
...@@ -127,20 +106,15 @@ static int udevGetUintProperty(struct udev_device *udev_device, ...@@ -127,20 +106,15 @@ static int udevGetUintProperty(struct udev_device *udev_device,
unsigned int *value, unsigned int *value,
int base) int base)
{ {
char *udev_value = NULL; const char *str = NULL;
int ret = PROPERTY_FOUND;
ret = udevGetDeviceProperty(udev_device, property_key, &udev_value); str = udevGetDeviceProperty(udev_device, property_key);
if (ret == PROPERTY_FOUND) { if (str && virStrToLong_ui(str, NULL, base, value) < 0) {
if (virStrToLong_ui(udev_value, NULL, base, value) < 0) { VIR_ERROR(_("Failed to convert '%s' to int"), str);
VIR_ERROR(_("Failed to convert '%s' to unsigned int"), udev_value); return PROPERTY_ERROR;
ret = PROPERTY_ERROR;
}
} }
return str == NULL ? PROPERTY_MISSING : PROPERTY_FOUND;
VIR_FREE(udev_value);
return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册