Source-Changes-HG archive

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

[src/trunk]: src/common/lib/libutil Avoid undefined behavior in snprintb.c



details:   https://anonhg.NetBSD.org/src/rev/3653098b1dc3
branches:  trunk
changeset: 363416:3653098b1dc3
user:      kamil <kamil%NetBSD.org@localhost>
date:      Thu Jul 26 00:33:26 2018 +0000

description:
Avoid undefined behavior in snprintb.c

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

snprintb.c:178:17, left shift of 1 by 31 places cannot be represented in type 'int'

Detected with micro-UBSan in the user mode.

diffstat:

 common/lib/libutil/snprintb.c |  8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diffs (36 lines):

diff -r e918e05859c0 -r 3653098b1dc3 common/lib/libutil/snprintb.c
--- a/common/lib/libutil/snprintb.c     Thu Jul 26 00:31:13 2018 +0000
+++ b/common/lib/libutil/snprintb.c     Thu Jul 26 00:33:26 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: snprintb.c,v 1.17 2017/10/14 18:41:41 ryo Exp $        */
+/*     $NetBSD: snprintb.c,v 1.18 2018/07/26 00:33:26 kamil Exp $      */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include <sys/cdefs.h>
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.17 2017/10/14 18:41:41 ryo Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.18 2018/07/26 00:33:26 kamil Exp $");
 #  endif
 
 #  include <sys/types.h>
@@ -51,7 +51,7 @@
 #  include <errno.h>
 # else /* ! _KERNEL */
 #  include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.17 2017/10/14 18:41:41 ryo Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.18 2018/07/26 00:33:26 kamil Exp $");
 #  include <sys/param.h>
 #  include <sys/inttypes.h>
 #  include <sys/systm.h>
@@ -175,7 +175,7 @@
                /* old (standard) format. */
                for (;(bit = *bitfmt) != 0;) {
                        cur_fmt = bitfmt++;
-                       if (val & (1 << (bit - 1))) {
+                       if (val & (1U << (bit - 1))) {
                                PUTSEP;
                                if (restart)
                                        continue;



Home | Main Index | Thread Index | Old Index