Source-Changes-HG archive

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

[src/trunk]: src/sys/fs/msdosfs Avoid undefined behavior semantics in msdosfs...



details:   https://anonhg.NetBSD.org/src/rev/7f921817155c
branches:  trunk
changeset: 834060:7f921817155c
user:      kamil <kamil%NetBSD.org@localhost>
date:      Wed Jul 25 22:07:59 2018 +0000

description:
Avoid undefined behavior semantics in msdosfs_fat.c

Do not change signedness bit with left shift.
While there avoid signed integer overflow.
Address both issues with using unsigned type.

msdosfs_fat.c:512:42, left shift of 1 by 31 places cannot be represented in type 'int'
msdosfs_fat.c:521:44, left shift of 1 by 31 places cannot be represented in type 'int'
msdosfs_fat.c:744:14, left shift of 1 by 31 places cannot be represented in type 'int'
msdosfs_fat.c:744:24, signed integer overflow: -2147483648 - 1 cannot be represented in type 'int [20]'
msdosfs_fat.c:840:13, left shift of 1 by 31 places cannot be represented in type 'int'
msdosfs_fat.c:840:36, signed integer overflow: -2147483648 - 1 cannot be represented in type 'int [20]'

Detected with micro-UBSan in the user mode.

diffstat:

 sys/fs/msdosfs/msdosfs_fat.c |  16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)

diffs (72 lines):

diff -r 8333984fa1b9 -r 7f921817155c sys/fs/msdosfs/msdosfs_fat.c
--- a/sys/fs/msdosfs/msdosfs_fat.c      Wed Jul 25 22:00:32 2018 +0000
+++ b/sys/fs/msdosfs/msdosfs_fat.c      Wed Jul 25 22:07:59 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msdosfs_fat.c,v 1.32 2018/01/27 03:54:01 sevan Exp $   */
+/*     $NetBSD: msdosfs_fat.c,v 1.33 2018/07/25 22:07:59 kamil Exp $   */
 
 /*-
  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
@@ -52,7 +52,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: msdosfs_fat.c,v 1.32 2018/01/27 03:54:01 sevan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: msdosfs_fat.c,v 1.33 2018/07/25 22:07:59 kamil Exp $");
 
 /*
  * kernel include files.
@@ -409,7 +409,7 @@
 
                if (pmp->pm_freeclustercount
                    && (pmp->pm_inusemap[cn / N_INUSEBITS]
-                       & (1 << (cn % N_INUSEBITS)))) {
+                       & (1U << (cn % N_INUSEBITS)))) {
                        /*
                         * The cluster indicated in FSInfo isn't free
                         * any longer.  Got get a new free one.
@@ -509,7 +509,7 @@
 usemap_alloc(struct msdosfsmount *pmp, u_long cn)
 {
 
-       pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
+       pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
        pmp->pm_freeclustercount--;
 }
 
@@ -518,7 +518,7 @@
 {
 
        pmp->pm_freeclustercount++;
-       pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
+       pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS));
 }
 
 int
@@ -741,7 +741,7 @@
        idx = start / N_INUSEBITS;
        start %= N_INUSEBITS;
        map = pmp->pm_inusemap[idx];
-       map &= ~((1 << start) - 1);
+       map &= ~((1U << start) - 1);
        if (map) {
                len = ffs(map) - 1 - start;
                return (len > count ? count : len);
@@ -837,7 +837,7 @@
        for (cn = newst; cn <= pmp->pm_maxcluster;) {
                idx = cn / N_INUSEBITS;
                map = pmp->pm_inusemap[idx];
-               map |= (1 << (cn % N_INUSEBITS)) - 1;
+               map |= (1U << (cn % N_INUSEBITS)) - 1;
                if (map != (u_int)-1) {
                        cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
                        if ((l = chainlength(pmp, cn, count)) >= count)
@@ -854,7 +854,7 @@
        for (cn = 0; cn < newst;) {
                idx = cn / N_INUSEBITS;
                map = pmp->pm_inusemap[idx];
-               map |= (1 << (cn % N_INUSEBITS)) - 1;
+               map |= (1U << (cn % N_INUSEBITS)) - 1;
                if (map != (u_int)-1) {
                        cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
                        if ((l = chainlength(pmp, cn, count)) >= count)



Home | Main Index | Thread Index | Old Index