Avoid UBSan when negating the most negative sal_Int64 value
...as exhibited by recently added 08e566f0c9
"add
a couple of tests for tools::BigInt"
Change-Id: Icd1080f86ccd985868fce34d6f86e2706acd58df
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176677
Reviewed-by: Stephan Bergmann <stephan.bergmann@allotropia.de>
Tested-by: Jenkins
This commit is contained in:
parent
2b8394b36b
commit
39f701d540
2 changed files with 8 additions and 5 deletions
|
@ -164,10 +164,10 @@ inline BigInt::operator sal_Int64() const
|
||||||
case 1:
|
case 1:
|
||||||
return bIsNeg ? -sal_Int64(nNum[0]) : nNum[0];
|
return bIsNeg ? -sal_Int64(nNum[0]) : nNum[0];
|
||||||
case 2:
|
case 2:
|
||||||
if (sal_uInt64 n = (sal_uInt64(nNum[1]) << 32) + nNum[0]; bIsNeg && n <= maxForNegInt64)
|
if (sal_uInt64 n = (sal_uInt64(nNum[1]) << 32) + nNum[0]; bIsNeg && n < maxForNegInt64)
|
||||||
return -sal_Int64(n); // maxForNegInt64 will convert correctly
|
return -sal_Int64(n);
|
||||||
else if (!bIsNeg && n <= maxForPosInt64)
|
else if ((bIsNeg && n == maxForNegInt64) || (!bIsNeg && n <= maxForPosInt64))
|
||||||
return n;
|
return n; // maxForNegInt64 will convert correctly
|
||||||
}
|
}
|
||||||
assert(false && "out of range");
|
assert(false && "out of range");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <limits>
|
||||||
#include <span>
|
#include <span>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -461,7 +462,9 @@ BigInt::BigInt( sal_Int64 nValue )
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (sal_uInt64 n = static_cast<sal_uInt64>(bIsNeg ? -nValue : nValue); n != 0; n >>= 32)
|
for (sal_uInt64 n = static_cast<sal_uInt64>(
|
||||||
|
(bIsNeg && nValue != std::numeric_limits<sal_Int64>::min()) ? -nValue : nValue);
|
||||||
|
n != 0; n >>= 32)
|
||||||
nNum[nLen++] = static_cast<sal_uInt32>(n);
|
nNum[nLen++] = static_cast<sal_uInt32>(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue