powercap: Replace sprintf() with sysfs_emit() in sysfs show functions
PATCH 1/1 powercap: Replace sprintf() with sysfs_emit() in sysfs show functions
Replace all sprintf() calls with sysfs_emit() in sysfs show functions. sysfs_emit() is preferred over sprintf() for formatting sysfs output as it provides better bounds checking and prevents potential buffer overflows.
Also, replace sprintf() with sysfs_emit() in show_constraint_name() and simplify the code by removing the redundant strlen() call since sysfs_emit() returns the length.
--- drivers/powercap/powercap_sys.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/powercap/powercap_sys.c +6 -7
@@ -27,7 +27,7 @@ static ssize_t _attr##_show(struct device *dev, \ \ if (power_zone->ops->get_##_attr) { \ if (!power_zone->ops->get_##_attr(power_zone, &value)) \ - len =sprintf (buf,"%lld\n" , value); \ + len =sysfs_emit (buf,"%lld\n" , value); \ } \ \return len; \
@@ -75,7 +75,7 @@ static ssize_t show_constraint_##_attr(struct device *dev, \ pconst = &power_zone->constraints[id]; \ if (pconst && pconst->ops && pconst->ops->get_##_attr) { \ if (!pconst->ops->get_##_attr(power_zone, id, &value)) \ - len =sprintf (buf,"%lld\n" , value); \ + len =sysfs_emit (buf,"%lld\n" , value); \ } \ \return len; \
@@ -171,9 +171,8 @@ static ssize_t show_constraint_name(struct device *dev,if (pconst && pconst->ops && pconst->ops->get_name) { name = pconst->ops->get_name (power_zone, id);if (name) { -sprintf (buf,"%.*s\n" ,POWERCAP_CONSTRAINT_NAME_LEN -1 , - name); - len =strlen (buf); + len =sysfs_emit (buf,"%.*s\n" , +POWERCAP_CONSTRAINT_NAME_LEN -1 , name); } }
@@ -350,7 +349,7 @@ static ssize_t name_show(struct device *dev, {struct powercap_zone *power_zone =to_powercap_zone (dev); -return sprintf (buf,"%s\n" , power_zone->name); +return sysfs_emit (buf,"%s\n" , power_zone->name); }static DEVICE_ATTR_RO (name);
@@ -438,7 +437,7 @@ static ssize_t enabled_show(struct device *dev, mode = false; } -return sprintf (buf,"%d\n" , mode); +return sysfs_emit (buf,"%d\n" , mode); } static ssize_t enabled_store(struct device *dev,
Signature · 1 line
2.43.0
Quoted 58 lines · Sumeet Pawnikar
Replace all
sprintf()calls withsysfs_emit()in sysfs show functions.sysfs_emit()is preferred oversprintf()for formatting sysfs output as it provides better bounds checking and prevents potential buffer overflows.
Also, replacesprintf()withsysfs_emit()inshow_constraint_name()and simplify the code by removing the redundantstrlen()call sincesysfs_emit()returns the length.Signed-off-by Sumeet Pawnikar <[email protected]>--- drivers/powercap/powercap_sys.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)drivers/powercap/powercap_sys.c +5
-6@@ -27,7 +27,7 @@ static ssize_t _attr##_show(struct device *dev, \ \ if (power_zone->ops->get_##_attr) { \ if (!power_zone->ops->get_##_attr(power_zone, &value)) \ - len =sprintf (buf,"%lld\n" , value); \ + len =sysfs_emit (buf,"%lld\n" , value); \ } \ \return len; \@@ -75,7 +75,7 @@ static ssize_t show_constraint_##_attr(struct device *dev, \ pconst = &power_zone->constraints[id]; \ if (pconst && pconst->ops && pconst->ops->get_##_attr) { \ if (!pconst->ops->get_##_attr(power_zone, id, &value)) \ - len =sprintf (buf,"%lld\n" , value); \ + len =sysfs_emit (buf,"%lld\n" , value); \ } \ \return len; \@@ -171,9 +171,8 @@ static ssize_t show_constraint_name(struct device *dev,if (pconst && pconst->ops && pconst->ops->get_name) { name = pconst->ops->get_name (power_zone, id);if (name) { -sprintf (buf,"%.*s\n" ,POWERCAP_CONSTRAINT_NAME_LEN -1 , - name); - len =strlen (buf); + len =sysfs_emit (buf,"%.*s\n" , +POWERCAP_CONSTRAINT_NAME_LEN -1 , name); } }@@ -350,7 +349,7 @@ static ssize_t name_show(struct device *dev, {struct powercap_zone *power_zone =to_powercap_zone (dev); -return sprintf (buf,"%s\n" , power_zone->name); +return sysfs_emit (buf,"%s\n" , power_zone->name); }static DEVICE_ATTR_RO (name);
drivers/powercap/powercap_sys.c static ssize_t enabled_show(struct device *dev,
Quoted 9 lines
drivers/powercap/powercap_sys.c +1
-2@@ -438,7 +437,7 @@ static ssize_t enabled_show(struct device *dev, mode = false; } -return sprintf (buf,"%d\n" , mode); +return sysfs_emit (buf,"%d\n" , mode); } static ssize_t enabled_store(struct device *dev, --
Applied as 6.20 material, thanks!