Source-Changes-HG archive

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

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



details:   https://anonhg.NetBSD.org/src/rev/7a3b8279d121
branches:  trunk
changeset: 324893:7a3b8279d121
user:      kamil <kamil%NetBSD.org@localhost>
date:      Thu Jul 26 00:20:41 2018 +0000

description:
Avoid undefined behavior in an inet_addr.c

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

inet_addr.c:218:20, left shift of 131 by 24 places cannot be represented in type 'int'

Detected with micro-UBSan in the user mode.

diffstat:

 common/lib/libc/inet/inet_addr.c |  11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)

diffs (42 lines):

diff -r 66f9b147c252 -r 7a3b8279d121 common/lib/libc/inet/inet_addr.c
--- a/common/lib/libc/inet/inet_addr.c  Thu Jul 26 00:13:19 2018 +0000
+++ b/common/lib/libc/inet/inet_addr.c  Thu Jul 26 00:20:41 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: inet_addr.c,v 1.3 2012/03/09 15:41:16 christos Exp $   */
+/*     $NetBSD: inet_addr.c,v 1.4 2018/07/26 00:20:41 kamil Exp $      */
 
 /*
  * Copyright (c) 1983, 1990, 1993
@@ -77,7 +77,7 @@
 static const char sccsid[] = "@(#)inet_addr.c  8.1 (Berkeley) 6/17/93";
 static const char rcsid[] = "Id: inet_addr.c,v 1.2.206.2 2004/03/17 00:29:45 marka Exp";
 #else
-__RCSID("$NetBSD: inet_addr.c,v 1.3 2012/03/09 15:41:16 christos Exp $");
+__RCSID("$NetBSD: inet_addr.c,v 1.4 2018/07/26 00:20:41 kamil Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -203,19 +203,20 @@
        case 2:                         /* a.b -- 8.24 bits */
                if (val > 0xffffffU)
                        return (0);
-               val |= parts[0] << 24;
+               val |= (uint32_t)parts[0] << 24;
                break;
 
        case 3:                         /* a.b.c -- 8.8.16 bits */
                if (val > 0xffffU)
                        return (0);
-               val |= (parts[0] << 24) | (parts[1] << 16);
+               val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16);
                break;
 
        case 4:                         /* a.b.c.d -- 8.8.8.8 bits */
                if (val > 0xffU)
                        return (0);
-               val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
+               val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16) |
+                       (parts[2] << 8);
                break;
        }
        if (addr != NULL)



Home | Main Index | Thread Index | Old Index