Re: [PATCH] test_power: add missing newlines when printing parameters by sysfs

From: Joe Perches
Date: Mon Oct 05 2020 - 00:19:20 EST


On Mon, 2020-10-05 at 01:30 +0000, Harley A.W. Lorenzo wrote:
> Here is the updated patch using sprintf, diffing from the original patch by Xiongfeng Wang.
>
> [PATCH] test_power: revise parameter printing to use sprintf
>
> Signed-off-by: Harley A.W. Lorenzo <hl1998@xxxxxxxxxxxxxx>
> Suggested-by: Joe Perches <joe@xxxxxxxxxxx>

I did not suggest this.

> diff --git a/drivers/power/supply/test_power.c b/drivers/power/supply/test_power.c
[]
> @@ -352,8 +352,8 @@ static int param_set_ac_online(const char *key, const struct kernel_param *kp)
>
> static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
> {.
> - strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
> - strcat(buffer, "\n");
> + char const *out = map_get_key(map_ac_online, ac_online, "unknown");
> + sprintf(buffer, "%s\n", out);
> return strlen(buffer);
> }

No temporary is necessary nor is strlen as
that's the same as the return from sprintf.

All of these should be similar to:

static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
{
return sprintf(buffer, "%s\n",
map_get_key(map_ac_online, ac_online, "unknown"));
}