Date: Tue, 7 Aug 2018 15:24:19 +0000 (UTC) From: "Pedro F. Giffuni" <pfg@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r337422 - in head/lib/libc: gen inet Message-ID: <201808071524.w77FOJif090562@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
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)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201808071524.w77FOJif090562>