diff mbox series

[v2] powercap: Adjust printing the constraint name with new line

Message ID 20201117134759.26797-1-lukasz.luba@arm.com (mailing list archive)
State Mainlined, archived
Headers show
Series [v2] powercap: Adjust printing the constraint name with new line | expand

Commit Message

Lukasz Luba Nov. 17, 2020, 1:47 p.m. UTC
The constrain name has limit of size 30, which sometimes might be hit.
When this happens the new line might get lost. Prevent this and set the
max limit for name string length equal 29. This would result is proper
string clamping (when needed) and storing '\n' at index 29 and '\0' at 30,
so similarly as desired originally.

Signed-off-by: Lukasz Luba <lukasz.luba@arm.com>
---
Hi Rafael,

It is based on top of you current pm/linux-next branch.

Change log:
v2:
- switched to sprintf() with "%.*s\n" pattern
v1 [1]

Regards,
Lukasz

[1] https://lore.kernel.org/linux-pm/20201109172452.6923-1-lukasz.luba@arm.com/


 drivers/powercap/powercap_sys.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Rafael J. Wysocki Nov. 23, 2020, 5:29 p.m. UTC | #1
On Tue, Nov 17, 2020 at 2:48 PM Lukasz Luba <lukasz.luba@arm.com> wrote:
>
> The constrain name has limit of size 30, which sometimes might be hit.
> When this happens the new line might get lost. Prevent this and set the
> max limit for name string length equal 29. This would result is proper
> string clamping (when needed) and storing '\n' at index 29 and '\0' at 30,
> so similarly as desired originally.
>
> Signed-off-by: Lukasz Luba <lukasz.luba@arm.com>
> ---
> Hi Rafael,
>
> It is based on top of you current pm/linux-next branch.
>
> Change log:
> v2:
> - switched to sprintf() with "%.*s\n" pattern
> v1 [1]
>
> Regards,
> Lukasz
>
> [1] https://lore.kernel.org/linux-pm/20201109172452.6923-1-lukasz.luba@arm.com/
>
>
>  drivers/powercap/powercap_sys.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/powercap/powercap_sys.c b/drivers/powercap/powercap_sys.c
> index 3f0b8e2ef3d4..f0654a932b37 100644
> --- a/drivers/powercap/powercap_sys.c
> +++ b/drivers/powercap/powercap_sys.c
> @@ -170,9 +170,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) {
> -                       snprintf(buf, POWERCAP_CONSTRAINT_NAME_LEN,
> -                                                               "%s\n", name);
> -                       buf[POWERCAP_CONSTRAINT_NAME_LEN] = '\0';
> +                       sprintf(buf, "%.*s\n", POWERCAP_CONSTRAINT_NAME_LEN - 1,
> +                               name);
>                         len = strlen(buf);
>                 }
>         }
> --

Applied as 5.11 material, thanks!
diff mbox series

Patch

diff --git a/drivers/powercap/powercap_sys.c b/drivers/powercap/powercap_sys.c
index 3f0b8e2ef3d4..f0654a932b37 100644
--- a/drivers/powercap/powercap_sys.c
+++ b/drivers/powercap/powercap_sys.c
@@ -170,9 +170,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) {
-			snprintf(buf, POWERCAP_CONSTRAINT_NAME_LEN,
-								"%s\n", name);
-			buf[POWERCAP_CONSTRAINT_NAME_LEN] = '\0';
+			sprintf(buf, "%.*s\n", POWERCAP_CONSTRAINT_NAME_LEN - 1,
+				name);
 			len = strlen(buf);
 		}
 	}