Commit 1b6599f7 authored by Yang Yingliang's avatar Yang Yingliang Committed by Rafael J. Wysocki

powercap: fix possible name leak in powercap_register_zone()

In the error path after calling dev_set_name(), the device
name is leaked. To fix this, calling dev_set_name() before
device_register(), and call put_device() if it returns error.

All the resources is released in powercap_release(), so it
can return from powercap_register_zone() directly.

Fixes: 75d2364e ("PowerCap: Add class driver")
Signed-off-by: default avatarYang Yingliang <yangyingliang@huawei.com>
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent 5dc4c995
...@@ -530,9 +530,6 @@ struct powercap_zone *powercap_register_zone( ...@@ -530,9 +530,6 @@ struct powercap_zone *powercap_register_zone(
power_zone->name = kstrdup(name, GFP_KERNEL); power_zone->name = kstrdup(name, GFP_KERNEL);
if (!power_zone->name) if (!power_zone->name)
goto err_name_alloc; goto err_name_alloc;
dev_set_name(&power_zone->dev, "%s:%x",
dev_name(power_zone->dev.parent),
power_zone->id);
power_zone->constraints = kcalloc(nr_constraints, power_zone->constraints = kcalloc(nr_constraints,
sizeof(*power_zone->constraints), sizeof(*power_zone->constraints),
GFP_KERNEL); GFP_KERNEL);
...@@ -555,9 +552,16 @@ struct powercap_zone *powercap_register_zone( ...@@ -555,9 +552,16 @@ struct powercap_zone *powercap_register_zone(
power_zone->dev_attr_groups[0] = &power_zone->dev_zone_attr_group; power_zone->dev_attr_groups[0] = &power_zone->dev_zone_attr_group;
power_zone->dev_attr_groups[1] = NULL; power_zone->dev_attr_groups[1] = NULL;
power_zone->dev.groups = power_zone->dev_attr_groups; power_zone->dev.groups = power_zone->dev_attr_groups;
dev_set_name(&power_zone->dev, "%s:%x",
dev_name(power_zone->dev.parent),
power_zone->id);
result = device_register(&power_zone->dev); result = device_register(&power_zone->dev);
if (result) if (result) {
goto err_dev_ret; put_device(&power_zone->dev);
mutex_unlock(&control_type->lock);
return ERR_PTR(result);
}
control_type->nr_zones++; control_type->nr_zones++;
mutex_unlock(&control_type->lock); mutex_unlock(&control_type->lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment