Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libusbhid lib/libusbhid: Fix possible left shift changes...



details:   https://anonhg.NetBSD.org/src/rev/d835d39bffa3
branches:  trunk
changeset: 970846:d835d39bffa3
user:      fox <fox%NetBSD.org@localhost>
date:      Sat Apr 04 21:21:35 2020 +0000

description:
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@

diffstat:

 lib/libusbhid/data.c |  10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diffs (38 lines):

diff -r 1b60650e9148 -r d835d39bffa3 lib/libusbhid/data.c
--- a/lib/libusbhid/data.c      Sat Apr 04 21:20:39 2020 +0000
+++ b/lib/libusbhid/data.c      Sat Apr 04 21:21:35 2020 +0000
@@ -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 <augustss%NetBSD.org@localhost>
@@ -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 @@
        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 @@
        } 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;



Home | Main Index | Thread Index | Old Index