2018-01-09 12:18:27

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH] HID: uhid: use strlcpy() instead of strncpy()

From: Xiongfeng Wang <[email protected]>

gcc-8 reports

drivers/hid/uhid.c: In function 'uhid_dev_create2':
./include/linux/string.h:245:9: warning: '__builtin_strncpy' output may
be truncated copying 127 bytes from a string of length 127
[-Wstringop-truncation]

The compiler require that the input param 'len' of strncpy() should be
greater than the length of the src string, so that '\0' is copied as
well. We can just use strlcpy() to avoid this warning.

Signed-off-by: Xiongfeng Wang <[email protected]>
---
drivers/hid/uhid.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c
index 6f819f1..0315ddd 100644
--- a/drivers/hid/uhid.c
+++ b/drivers/hid/uhid.c
@@ -496,12 +496,12 @@ static int uhid_dev_create2(struct uhid_device *uhid,
goto err_free;
}

- len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
- strncpy(hid->name, ev->u.create2.name, len);
- len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
- strncpy(hid->phys, ev->u.create2.phys, len);
- len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
- strncpy(hid->uniq, ev->u.create2.uniq, len);
+ len = min(sizeof(hid->name), sizeof(ev->u.create2.name));
+ strlcpy(hid->name, ev->u.create2.name, len);
+ len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys));
+ strlcpy(hid->phys, ev->u.create2.phys, len);
+ len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq));
+ strlcpy(hid->uniq, ev->u.create2.uniq, len);

hid->ll_driver = &uhid_hid_driver;
hid->bus = ev->u.create2.bus;
--
1.8.3.1


2018-02-16 19:06:56

by Jiri Kosina

[permalink] [raw]
Subject: Re: [PATCH] HID: uhid: use strlcpy() instead of strncpy()

On Tue, 9 Jan 2018, Xiongfeng Wang wrote:

> From: Xiongfeng Wang <[email protected]>
>
> gcc-8 reports
>
> drivers/hid/uhid.c: In function 'uhid_dev_create2':
> ./include/linux/string.h:245:9: warning: '__builtin_strncpy' output may
> be truncated copying 127 bytes from a string of length 127
> [-Wstringop-truncation]
>
> The compiler require that the input param 'len' of strncpy() should be
> greater than the length of the src string, so that '\0' is copied as
> well. We can just use strlcpy() to avoid this warning.

Applied to for-4.17/upstream.

--
Jiri Kosina
SUSE Labs