Skip to content

Commit a68f3bd

Browse files
Dan CarpenterJiri Kosina
authored andcommitted
HID: hid-debug: clean up snprintf() checks in hid_resolv_usage()
The snprintf() limits are complicated and slightly wrong when it does: max(0, HID_DEBUG_BUFSIZE - len - 1) The "- 1" should not be there. It means we can't use the last byte of the buffer. If we change the first snprintf() to scnprintf() then we can remove the max(). At the start of the function the strlen(buf) is going always going to be < HID_DEBUG_BUFSIZE so that is safe. If it were > HID_DEBUG_BUFSIZE then that would result in a WARN(). Signed-off-by: Dan Carpenter <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent f3e8252 commit a68f3bd

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

drivers/hid/hid-debug.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,7 @@ char *hid_resolv_usage(unsigned usage, struct seq_file *f) {
487487

488488
if (!f) {
489489
len = strlen(buf);
490-
snprintf(buf+len, max(0, HID_DEBUG_BUFSIZE - len), ".");
491-
len++;
490+
len += scnprintf(buf + len, HID_DEBUG_BUFSIZE - len, ".");
492491
}
493492
else {
494493
seq_printf(f, ".");
@@ -499,7 +498,7 @@ char *hid_resolv_usage(unsigned usage, struct seq_file *f) {
499498
if (p->usage == (usage & 0xffff)) {
500499
if (!f)
501500
snprintf(buf + len,
502-
max(0,HID_DEBUG_BUFSIZE - len - 1),
501+
HID_DEBUG_BUFSIZE - len,
503502
"%s", p->description);
504503
else
505504
seq_printf(f,
@@ -510,8 +509,8 @@ char *hid_resolv_usage(unsigned usage, struct seq_file *f) {
510509
break;
511510
}
512511
if (!f)
513-
snprintf(buf + len, max(0, HID_DEBUG_BUFSIZE - len - 1),
514-
"%04x", usage & 0xffff);
512+
snprintf(buf + len, HID_DEBUG_BUFSIZE - len, "%04x",
513+
usage & 0xffff);
515514
else
516515
seq_printf(f, "%04x", usage & 0xffff);
517516
return buf;

0 commit comments

Comments
 (0)