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
This commit is contained in:
Pedro F. Giffuni 2018-08-07 15:24:19 +00:00
parent 5102499601
commit fee1489eb1
2 changed files with 6 additions and 4 deletions

View File

@ -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)));
}

View File

@ -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)