Module Name:    src
Committed By:   kamil
Date:           Thu Jul 26 00:20:41 UTC 2018

Modified Files:
        src/common/lib/libc/inet: inet_addr.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/common/lib/libc/inet/inet_addr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libc/inet/inet_addr.c
diff -u src/common/lib/libc/inet/inet_addr.c:1.3 src/common/lib/libc/inet/inet_addr.c:1.4
--- src/common/lib/libc/inet/inet_addr.c:1.3	Fri Mar  9 15:41:16 2012
+++ src/common/lib/libc/inet/inet_addr.c	Thu Jul 26 00:20:41 2018
@@ -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 @@ inet_aton(const char *cp, struct in_addr
 	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)

Reply via email to