Re: [PATCH] Documentation: laptops: freefall.c: Cleaning up missing null-terminate in conjunction with strncpy

From: Randy Dunlap
Date: Sun Jul 27 2014 - 19:03:57 EST


On 07/27/14 07:40, Rickard Strandqvist wrote:
> Added a guaranteed null-terminate after call to strncpy.
>
> Signed-off-by: Rickard Strandqvist <rickard_strandqvist@xxxxxxxxxxxxxxxxxx>
> ---
> Documentation/laptops/freefall.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/laptops/freefall.c b/Documentation/laptops/freefall.c
> index aab2ff0..113d004 100644
> --- a/Documentation/laptops/freefall.c
> +++ b/Documentation/laptops/freefall.c
> @@ -33,8 +33,10 @@ static int set_unload_heads_path(char *device)
>
> if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
> return -EINVAL;
> - strncpy(devname, device + 5, sizeof(devname) - 1);
> - strncpy(device_path, device, sizeof(device_path) - 1);

Maybe I am overlooking something here, but what was wrong with
the - 1 (2 times) above? and then just add the 2 lines below that set the
last byte to '\0'?

> + strncpy(devname, device + 5, sizeof(devname));
> + devname[sizeof(devname) - 1] = '\0';
> + strncpy(device_path, device, sizeof(device_path));
> + device_path[sizeof(device_path) - 1] = '\0';
>
> snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
> "/sys/block/%s/device/unload_heads", devname);
>


--
~Randy
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/