Skip to content

Commit 4d26d1d

Browse files
David HerrmannJiri Kosina
authored andcommitted
Revert "HID: uhid: use strlcpy() instead of strncpy()"
This reverts commit 336fd4f. Please note that `strlcpy()` does *NOT* do what you think it does. strlcpy() *ALWAYS* reads the full input string, regardless of the 'length' parameter. That is, if the input is not zero-terminated, strlcpy() will *READ* beyond input boundaries. It does this, because it always returns the size it *would* copy if the target was big enough, not the truncated size it actually copied. The original code was perfectly fine. The hid device is zero-initialized and the strncpy() functions copied up to n-1 characters. The result is always zero-terminated this way. This is the third time someone tried to replace strncpy with strlcpy in this function, and gets it wrong. I now added a comment that should at least make people reconsider. Signed-off-by: David Herrmann <dh.herrmann@gmail.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 8c01db7 commit 4d26d1d

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

drivers/hid/uhid.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,13 @@ static int uhid_dev_create2(struct uhid_device *uhid,
497497
goto err_free;
498498
}
499499

500-
len = min(sizeof(hid->name), sizeof(ev->u.create2.name));
501-
strlcpy(hid->name, ev->u.create2.name, len);
502-
len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys));
503-
strlcpy(hid->phys, ev->u.create2.phys, len);
504-
len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq));
505-
strlcpy(hid->uniq, ev->u.create2.uniq, len);
500+
/* @hid is zero-initialized, strncpy() is correct, strlcpy() not */
501+
len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
502+
strncpy(hid->name, ev->u.create2.name, len);
503+
len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
504+
strncpy(hid->phys, ev->u.create2.phys, len);
505+
len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
506+
strncpy(hid->uniq, ev->u.create2.uniq, len);
506507

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

0 commit comments

Comments
 (0)