From: Dan Carpenter Date: Thu, 16 Sep 2021 13:21:54 +0000 (+0300) Subject: HID: hid-debug: clean up snprintf() checks in hid_resolv_usage() X-Git-Tag: v6.1-rc5~2717^2~5^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a68f3bd13994b315f47ec7e4da8d1c39ba0a2bb4;p=platform%2Fkernel%2Flinux-starfive.git 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 Signed-off-by: Jiri Kosina --- diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c index e6f18a1..7a92e2a 100644 --- a/drivers/hid/hid-debug.c +++ b/drivers/hid/hid-debug.c @@ -487,8 +487,7 @@ char *hid_resolv_usage(unsigned usage, struct seq_file *f) { if (!f) { len = strlen(buf); - snprintf(buf+len, max(0, HID_DEBUG_BUFSIZE - len), "."); - len++; + len += scnprintf(buf + len, HID_DEBUG_BUFSIZE - len, "."); } else { seq_printf(f, "."); @@ -499,7 +498,7 @@ char *hid_resolv_usage(unsigned usage, struct seq_file *f) { if (p->usage == (usage & 0xffff)) { if (!f) snprintf(buf + len, - max(0,HID_DEBUG_BUFSIZE - len - 1), + HID_DEBUG_BUFSIZE - len, "%s", p->description); else seq_printf(f, @@ -510,8 +509,8 @@ char *hid_resolv_usage(unsigned usage, struct seq_file *f) { break; } if (!f) - snprintf(buf + len, max(0, HID_DEBUG_BUFSIZE - len - 1), - "%04x", usage & 0xffff); + snprintf(buf + len, HID_DEBUG_BUFSIZE - len, "%04x", + usage & 0xffff); else seq_printf(f, "%04x", usage & 0xffff); return buf;