Source-Changes-HG archive

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

[src/trunk]: src/common/lib/libc/sys Avoid undefined behavior in an cpuset.c



details:   https://anonhg.NetBSD.org/src/rev/282562fb3042
branches:  trunk
changeset: 834068:282562fb3042
user:      kamil <kamil%NetBSD.org@localhost>
date:      Thu Jul 26 00:13:19 2018 +0000

description:
Avoid undefined behavior in an cpuset.c

Do not change the signedness bit with a left shift operation.
Switch to unsigned integer to prevent this.

cpuset.c:112:18, left shift of 1 by 31 places cannot be represented in type 'int'

Detected with micro-UBSan in the user mode.

diffstat:

 common/lib/libc/sys/cpuset.c |  10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diffs (45 lines):

diff -r 3d0e550c6c30 -r 282562fb3042 common/lib/libc/sys/cpuset.c
--- a/common/lib/libc/sys/cpuset.c      Thu Jul 26 00:05:28 2018 +0000
+++ b/common/lib/libc/sys/cpuset.c      Thu Jul 26 00:13:19 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cpuset.c,v 1.19 2018/01/04 20:57:28 kamil Exp $        */
+/*     $NetBSD: cpuset.c,v 1.20 2018/07/26 00:13:19 kamil Exp $        */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 #ifndef _STANDALONE
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: cpuset.c,v 1.19 2018/01/04 20:57:28 kamil Exp $");
+__RCSID("$NetBSD: cpuset.c,v 1.20 2018/07/26 00:13:19 kamil Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #ifdef _LIBC
@@ -97,7 +97,7 @@
                errno = EINVAL;
                return -1;
        }
-       return ((1 << (unsigned int)(i & CPUSET_MASK)) & c->bits[j]) != 0;
+       return ((1U << (unsigned int)(i & CPUSET_MASK)) & c->bits[j]) != 0;
 }
 
 int
@@ -109,7 +109,7 @@
                errno = EINVAL;
                return -1;
        }
-       c->bits[j] |= 1 << (unsigned int)(i & CPUSET_MASK);
+       c->bits[j] |= 1U << (unsigned int)(i & CPUSET_MASK);
        return 0;
 }
 
@@ -122,7 +122,7 @@
                errno = EINVAL;
                return -1;
        }
-       c->bits[j] &= ~(1 << (unsigned int)(i & CPUSET_MASK));
+       c->bits[j] &= ~(1U << (unsigned int)(i & CPUSET_MASK));
        return 0;
 }
 



Home | Main Index | Thread Index | Old Index