[Support] Avoid UB in sys::fs::perms::operator~. NFC.

This was exposed in r297945 and r301220: the intermediate complement
is a 32-bit value, and casting it to 'perms' invokes UB.

llvm-svn: 301373
This commit is contained in:
Ahmed Bougacha 2017-04-26 00:48:28 +00:00
parent d91fb8c367
commit 9547aabb26
1 changed files with 3 additions and 1 deletions

View File

@ -116,7 +116,9 @@ inline perms &operator&=(perms &l, perms r) {
return l;
}
inline perms operator~(perms x) {
return static_cast<perms>(~static_cast<unsigned short>(x));
// Avoid UB by explicitly truncating the (unsigned) ~ result.
return static_cast<perms>(
static_cast<unsigned short>(~static_cast<unsigned short>(x)));
}
class UniqueID {