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

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

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

This bug was reported by UBSan runs.

lib/libusbhid/data.c:58:25
lib/libusbhid/data.c:91:7
lib/libusbhid/data.c:92:7

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.8 -r1.9 src/lib/libusbhid/data.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/data.c
diff -u src/lib/libusbhid/data.c:1.8 src/lib/libusbhid/data.c:1.9
--- src/lib/libusbhid/data.c:1.8	Thu Jan  7 19:49:45 2016
+++ src/lib/libusbhid/data.c	Sat Apr  4 21:21:35 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: data.c,v 1.8 2016/01/07 19:49:45 jakllsch Exp $	*/
+/*	$NetBSD: data.c,v 1.9 2020/04/04 21:21:35 fox Exp $	*/
 
 /*
  * Copyright (c) 1999 Lennart Augustsson <augus...@netbsd.org>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: data.c,v 1.8 2016/01/07 19:49:45 jakllsch Exp $");
+__RCSID("$NetBSD: data.c,v 1.9 2020/04/04 21:21:35 fox Exp $");
 
 #include <assert.h>
 #include <stdlib.h>
@@ -55,7 +55,7 @@ hid_get_data(const void *p, const hid_it
 	end = (hpos + hsize + 7) / 8 - offs;
 	data = 0;
 	for (i = 0; i < end; i++)
-		data |= buf[offs + i] << (i*8);
+		data |= ((uint32_t)buf[offs + i]) << (i*8);
 	data >>= hpos % 8;
 	if (hsize < 32) {
 		data &= (1 << hsize) - 1;
@@ -88,8 +88,8 @@ hid_set_data(void *p, const hid_item_t *
 	} else
 		mask = ~0;
 
-	data <<= (hpos % 8);
-	mask <<= (hpos % 8);
+	data = ((uint32_t)data) << (hpos % 8);
+	mask = ((uint32_t)mask) << (hpos % 8);
 	mask = ~mask;
 
 	offs = hpos / 8;

Reply via email to