Fix compiler warnings

Including a potential nullptr dereference
This commit is contained in:
Alfonso Gregory 2021-07-09 14:11:12 -04:00 committed by GitHub
commit 6d1cb73c53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 52 deletions

View file

@ -85,7 +85,7 @@ namespace
if (ullOperand <= UINT32_MAX)
{
*pulResult = (uint32_t)ullOperand;
*pulResult = static_cast<uint32_t>(ullOperand);
hr = S_OK;
}
@ -200,7 +200,7 @@ PNUMBER _createnum(_In_ uint32_t size)
if (SUCCEEDED(Calc_ULongAdd(size, 1, &cbAlloc)) && SUCCEEDED(Calc_ULongMult(cbAlloc, sizeof(MANTTYPE), &cbAlloc))
&& SUCCEEDED(Calc_ULongAdd(cbAlloc, sizeof(NUMBER), &cbAlloc)))
{
pnumret = (PNUMBER)zmalloc(cbAlloc);
pnumret = reinterpret_cast<PNUMBER>(zmalloc(cbAlloc));
if (pnumret == nullptr)
{
throw(CALC_E_OUTOFMEMORY);
@ -230,9 +230,7 @@ PNUMBER _createnum(_In_ uint32_t size)
PRAT _createrat(void)
{
PRAT prat = nullptr;
prat = (PRAT)zmalloc(sizeof(RAT));
PRAT prat = reinterpret_cast<PRAT>(zmalloc(sizeof(RAT)));
if (prat == nullptr)
{
@ -310,9 +308,9 @@ PNUMBER nRadixxtonum(_In_ PNUMBER a, uint32_t radix, int32_t precision)
// limit the digits to the minimum of the existing precision or the
// requested precision.
uint32_t cdigits = precision + 1;
if (cdigits > (uint32_t)a->cdigit)
if (cdigits > static_cast<uint32_t>(a->cdigit))
{
cdigits = (uint32_t)a->cdigit;
cdigits = static_cast<uint32_t>(a->cdigit);
}
// scale by the internal base to the internal exponent offset of the LSD
@ -972,7 +970,7 @@ uint64_t rattoUi64(_In_ PRAT prat, uint32_t radix, int32_t precision)
destroyrat(prat32);
destroyrat(pint);
return (((uint64_t)hi << 32) | lo);
return (((static_cast<uint64_t>(hi)) << 32) | lo);
}
//-----------------------------------------------------------------------------
@ -1032,12 +1030,12 @@ bool stripzeroesnum(_Inout_ PNUMBER pnum, int32_t starting)
// point pmant to the LSD
if (cdigits > starting)
{
pmant += cdigits - starting;
pmant = pmant + cdigits - starting;
cdigits = starting;
}
// Check we haven't gone too far, and we are still looking at zeros.
while ((cdigits > 0) && !(*pmant))
while ((cdigits > 0) && (*pmant == 0))
{
// move to next significant digit and keep track of digits we can
// ignore later.
@ -1050,7 +1048,7 @@ bool stripzeroesnum(_Inout_ PNUMBER pnum, int32_t starting)
if (fstrip)
{
// Remove them.
memmove(pnum->mant, pmant, (int)(cdigits * sizeof(MANTTYPE)));
memmove(pnum->mant, pmant, static_cast<size_t>(cdigits) * sizeof(MANTTYPE)));
// And adjust exponent and digit count accordingly.
pnum->exp += (pnum->cdigit - cdigits);
pnum->cdigit = cdigits;

View file

@ -340,7 +340,7 @@ void remnum(_Inout_ PNUMBER* pa, _In_ PNUMBER b, uint32_t radix)
}
// Subtract the working remainder from the remainder holder.
tmp->sign = -1 * (*pa)->sign;
tmp->sign = -(*pa)->sign;
addnum(pa, tmp, radix);
destroynum(tmp);
@ -418,12 +418,11 @@ void _divnum(PNUMBER* pa, PNUMBER b, uint32_t radix, int32_t precision)
}
destroynum(tmp);
int32_t digit;
int32_t cdigits = 0;
while (cdigits++ < thismax && !zernum(rem))
int32_t cdigits;
for (cdigits = 0; cdigits < thismax && !zernum(rem); cdigits++)
{
digit = radix - 1;
PNUMBER multiple = nullptr;
uint32_t digit = radix - 1;
PNUMBER multiple = rem; // In case first number in the list causes the loop to exit early
for (const auto& num : numberList)
{
if (!lessnum(rem, num) || !--digit)
@ -440,13 +439,12 @@ void _divnum(PNUMBER* pa, PNUMBER b, uint32_t radix, int32_t precision)
multiple->sign *= -1;
}
rem->exp++;
*ptrc-- = (MANTTYPE)digit;
*ptrc-- = static_cast<MANTTYPE>(digit);
}
cdigits--;
if (c->mant != ++ptrc)
{
memmove(c->mant, ptrc, (int)(cdigits * sizeof(MANTTYPE)));
memmove(c->mant, ptrc, static_cast<size_t>(cdigits) * sizeof(MANTTYPE)));
}
// Cleanup table structure
@ -505,9 +503,7 @@ bool equnum(_In_ PNUMBER a, _In_ PNUMBER b)
// If the exponents are different, these are different numbers.
return false;
}
else
{
if (diff > 0)
else if (diff > 0)
{
// If the exponents are different, these are different numbers.
return false;
@ -538,7 +534,6 @@ bool equnum(_In_ PNUMBER a, _In_ PNUMBER b)
return true;
}
}
}
//---------------------------------------------------------------------------
//
@ -602,20 +597,19 @@ bool lessnum(_In_ PNUMBER a, _In_ PNUMBER b)
bool zernum(_In_ PNUMBER a)
{
int32_t length;
MANTTYPE* pcha;
length = a->cdigit;
pcha = a->mant;
MANTTYPE* pcha = a->mant;
// loop over all the digits until you find a nonzero or until you run
// out of digits
while (length-- > 0)
for (int32_t length = a->cdigit; length > 0; length--)
{
if (*pcha++)
if (*pcha)
{
// One of the digits isn't zero, therefore the number isn't zero
return false;
}
pcha++;
}
// All of the digits are zero, therefore the number is zero
return true;