Skip to content

Commit

Permalink
[APFloat] Improve asserts in isSignificandAllOnes and isSignificandAl…
Browse files Browse the repository at this point in the history
…lZeros so they protect shift operations from undefined behavior.

For example, the assert in isSignificandAllZeros allowed NumHighBits
to be integerPartWidth. But since it is used directly as a shift amount
it must be less than integerPartWidth.
  • Loading branch information
topperc committed Oct 1, 2020
1 parent d4a1db4 commit 12bdd42
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions llvm/lib/Support/APFloat.cpp
Expand Up @@ -850,8 +850,8 @@ bool IEEEFloat::isSignificandAllOnes() const {
// Set the unused high bits to all ones when we compare.
const unsigned NumHighBits =
PartCount*integerPartWidth - semantics->precision + 1;
assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
"fill than integerPartWidth");
assert(NumHighBits <= integerPartWidth && NumHighBits > 0 &&
"Can not have more high bits to fill than integerPartWidth");
const integerPart HighBitFill =
~integerPart(0) << (integerPartWidth - NumHighBits);
if (~(Parts[PartCount - 1] | HighBitFill))
Expand All @@ -870,9 +870,10 @@ bool IEEEFloat::isSignificandAllZeros() const {
if (Parts[i])
return false;

// Compute how many bits are used in the final word.
const unsigned NumHighBits =
PartCount*integerPartWidth - semantics->precision + 1;
assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
assert(NumHighBits < integerPartWidth && "Can not have more high bits to "
"clear than integerPartWidth");
const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;

Expand Down

0 comments on commit 12bdd42

Please sign in to comment.