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) if (ullOperand <= UINT32_MAX)
{ {
*pulResult = (uint32_t)ullOperand; *pulResult = static_cast<uint32_t>(ullOperand);
hr = S_OK; 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)) if (SUCCEEDED(Calc_ULongAdd(size, 1, &cbAlloc)) && SUCCEEDED(Calc_ULongMult(cbAlloc, sizeof(MANTTYPE), &cbAlloc))
&& SUCCEEDED(Calc_ULongAdd(cbAlloc, sizeof(NUMBER), &cbAlloc))) && SUCCEEDED(Calc_ULongAdd(cbAlloc, sizeof(NUMBER), &cbAlloc)))
{ {
pnumret = (PNUMBER)zmalloc(cbAlloc); pnumret = reinterpret_cast<PNUMBER>(zmalloc(cbAlloc));
if (pnumret == nullptr) if (pnumret == nullptr)
{ {
throw(CALC_E_OUTOFMEMORY); throw(CALC_E_OUTOFMEMORY);
@ -230,9 +230,7 @@ PNUMBER _createnum(_In_ uint32_t size)
PRAT _createrat(void) PRAT _createrat(void)
{ {
PRAT prat = nullptr; PRAT prat = reinterpret_cast<PRAT>(zmalloc(sizeof(RAT)));
prat = (PRAT)zmalloc(sizeof(RAT));
if (prat == nullptr) 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 // limit the digits to the minimum of the existing precision or the
// requested precision. // requested precision.
uint32_t cdigits = precision + 1; 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 // 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(prat32);
destroyrat(pint); 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 // point pmant to the LSD
if (cdigits > starting) if (cdigits > starting)
{ {
pmant += cdigits - starting; pmant = pmant + cdigits - starting;
cdigits = starting; cdigits = starting;
} }
// Check we haven't gone too far, and we are still looking at zeros. // 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 // move to next significant digit and keep track of digits we can
// ignore later. // ignore later.
@ -1050,7 +1048,7 @@ bool stripzeroesnum(_Inout_ PNUMBER pnum, int32_t starting)
if (fstrip) if (fstrip)
{ {
// Remove them. // 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. // And adjust exponent and digit count accordingly.
pnum->exp += (pnum->cdigit - cdigits); pnum->exp += (pnum->cdigit - cdigits);
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. // Subtract the working remainder from the remainder holder.
tmp->sign = -1 * (*pa)->sign; tmp->sign = -(*pa)->sign;
addnum(pa, tmp, radix); addnum(pa, tmp, radix);
destroynum(tmp); destroynum(tmp);
@ -418,12 +418,11 @@ void _divnum(PNUMBER* pa, PNUMBER b, uint32_t radix, int32_t precision)
} }
destroynum(tmp); destroynum(tmp);
int32_t digit; int32_t cdigits;
int32_t cdigits = 0; for (cdigits = 0; cdigits < thismax && !zernum(rem); cdigits++)
while (cdigits++ < thismax && !zernum(rem))
{ {
digit = radix - 1; uint32_t digit = radix - 1;
PNUMBER multiple = nullptr; PNUMBER multiple = rem; // In case first number in the list causes the loop to exit early
for (const auto& num : numberList) for (const auto& num : numberList)
{ {
if (!lessnum(rem, num) || !--digit) if (!lessnum(rem, num) || !--digit)
@ -440,13 +439,12 @@ void _divnum(PNUMBER* pa, PNUMBER b, uint32_t radix, int32_t precision)
multiple->sign *= -1; multiple->sign *= -1;
} }
rem->exp++; rem->exp++;
*ptrc-- = (MANTTYPE)digit; *ptrc-- = static_cast<MANTTYPE>(digit);
} }
cdigits--;
if (c->mant != ++ptrc) 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 // Cleanup table structure
@ -505,9 +503,7 @@ bool equnum(_In_ PNUMBER a, _In_ PNUMBER b)
// If the exponents are different, these are different numbers. // If the exponents are different, these are different numbers.
return false; return false;
} }
else else if (diff > 0)
{
if (diff > 0)
{ {
// If the exponents are different, these are different numbers. // If the exponents are different, these are different numbers.
return false; return false;
@ -538,7 +534,6 @@ bool equnum(_In_ PNUMBER a, _In_ PNUMBER b)
return true; return true;
} }
} }
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// //
@ -602,20 +597,19 @@ bool lessnum(_In_ PNUMBER a, _In_ PNUMBER b)
bool zernum(_In_ PNUMBER a) bool zernum(_In_ PNUMBER a)
{ {
int32_t length; MANTTYPE* pcha = a->mant;
MANTTYPE* pcha;
length = a->cdigit;
pcha = a->mant;
// loop over all the digits until you find a nonzero or until you run // loop over all the digits until you find a nonzero or until you run
// out of digits // 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 // One of the digits isn't zero, therefore the number isn't zero
return false; return false;
} }
pcha++;
} }
// All of the digits are zero, therefore the number is zero // All of the digits are zero, therefore the number is zero
return true; return true;