Author: pfg
Date: Tue Aug  7 15:24:19 2018
New Revision: 337422
URL: https://svnweb.freebsd.org/changeset/base/337422

Log:
  libc: fix cases of undefined behavior.
  
  These were found by the Undefined Behavious  GsoC project at NetBSD:
  
  Avoid undefined behavior in ftok(3)
  
  Do not change the signedness bit with a left shift operation.
  Cast to unsigned integer to prevent this.
  
  ftok.c:56:10, left shift of 123456789 by 24 places cannot be represented
  in type 'int'
  ftok.c:56:10, left shift of 4160 by 24 places cannot be represented in
  type 'int'
  
  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.
  
  Obtained from:        NetBSD
  MFC after:    2 weeks

Modified:
  head/lib/libc/gen/ftok.c
  head/lib/libc/inet/inet_addr.c

Modified: head/lib/libc/gen/ftok.c
==============================================================================
--- head/lib/libc/gen/ftok.c    Tue Aug  7 15:04:53 2018        (r337421)
+++ head/lib/libc/gen/ftok.c    Tue Aug  7 15:24:19 2018        (r337422)
@@ -42,5 +42,6 @@ ftok(const char *path, int id)
        if (stat(path, &st) < 0)
                return (key_t)-1;
 
-       return (key_t) (id << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 
0xffff));
+       return ((key_t)((unsigned int)id << 24 | (st.st_dev & 0xff) << 16 |
+           (st.st_ino & 0xffff)));
 }

Modified: head/lib/libc/inet/inet_addr.c
==============================================================================
--- head/lib/libc/inet/inet_addr.c      Tue Aug  7 15:04:53 2018        
(r337421)
+++ head/lib/libc/inet/inet_addr.c      Tue Aug  7 15:24:19 2018        
(r337422)
@@ -184,19 +184,20 @@ inet_aton(const char *cp, struct in_addr *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)
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to