Module Name:    src
Committed By:   fox
Date:           Sat Apr  4 21:23:04 UTC 2020

Modified Files:
        src/lib/libusbhid: usage.c

Log Message:
lib/libusbhid: Fix possible left shift changes signedness bit.

This bug was reported by UBSan runs.

lib/libusbhid/usage.c:247:27
lib/libusbhid/usage.c:244:28
lib/libusbhid/usage.c:235:13

Can result in left shift changes signedness bit as a side effect positive number
can go negative, cast it to unsigned for the operation and silence the issue.

Reviewed by: kamil@


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/lib/libusbhid/usage.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libusbhid/usage.c
diff -u src/lib/libusbhid/usage.c:1.10 src/lib/libusbhid/usage.c:1.11
--- src/lib/libusbhid/usage.c:1.10	Sat Jan  2 21:58:10 2016
+++ src/lib/libusbhid/usage.c	Sat Apr  4 21:23:04 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: usage.c,v 1.10 2016/01/02 21:58:10 christos Exp $	*/
+/*	$NetBSD: usage.c,v 1.11 2020/04/04 21:23:04 fox Exp $	*/
 
 /*
  * Copyright (c) 1999 Lennart Augustsson <augus...@netbsd.org>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: usage.c,v 1.10 2016/01/02 21:58:10 christos Exp $");
+__RCSID("$NetBSD: usage.c,v 1.11 2020/04/04 21:23:04 fox Exp $");
 
 #include <assert.h>
 #include <ctype.h>
@@ -232,7 +232,7 @@ hid_parse_usage_in_page(const char *name
 		if (strncmp(pages[k].name, name, len) == 0)
 			goto found;
 	if (sscanf(name, "%x:%x", &k, &j) == 2) {
-		return (k << 16) | j;
+		return (((uint32_t)k) << 16) | j;
 	}
 	return -1;
  found:
@@ -241,9 +241,9 @@ hid_parse_usage_in_page(const char *name
 		if (pages[k].page_contents[j].usage == -1) {
 			if (sscanf(sep, fmtcheck(
 			    pages[k].page_contents[j].name, "%u"), &l) == 1) {
-				return (pages[k].usage << 16) | l;
+				return (((uint32_t)pages[k].usage) << 16) | l;
 			}
 		} else if (strcmp(pages[k].page_contents[j].name, sep) == 0)
-			return (pages[k].usage << 16) | pages[k].page_contents[j].usage;
+			return (((uint32_t)pages[k].usage) << 16) | pages[k].page_contents[j].usage;
 	return (-1);
 }

Reply via email to