From 6d1cb73c53677dc5c542c978e61f0335ebc52bbf Mon Sep 17 00:00:00 2001 From: Alfonso Gregory <83477269+gAlfonso-bit@users.noreply.github.com> Date: Fri, 9 Jul 2021 14:11:12 -0400 Subject: [PATCH] Fix compiler warnings Including a potential nullptr dereference --- src/CalcManager/Ratpack/conv.cpp | 20 ++++----- src/CalcManager/Ratpack/num.cpp | 76 +++++++++++++++----------------- 2 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/CalcManager/Ratpack/conv.cpp b/src/CalcManager/Ratpack/conv.cpp index 4f7609de..d7aa8e2a 100644 --- a/src/CalcManager/Ratpack/conv.cpp +++ b/src/CalcManager/Ratpack/conv.cpp @@ -85,7 +85,7 @@ namespace if (ullOperand <= UINT32_MAX) { - *pulResult = (uint32_t)ullOperand; + *pulResult = static_cast(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(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(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(a->cdigit)) { - cdigits = (uint32_t)a->cdigit; + cdigits = static_cast(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(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(cdigits) * sizeof(MANTTYPE))); // And adjust exponent and digit count accordingly. pnum->exp += (pnum->cdigit - cdigits); pnum->cdigit = cdigits; diff --git a/src/CalcManager/Ratpack/num.cpp b/src/CalcManager/Ratpack/num.cpp index 6055e41d..e3a1d176 100644 --- a/src/CalcManager/Ratpack/num.cpp +++ b/src/CalcManager/Ratpack/num.cpp @@ -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(digit); } - cdigits--; if (c->mant != ++ptrc) { - memmove(c->mant, ptrc, (int)(cdigits * sizeof(MANTTYPE))); + memmove(c->mant, ptrc, static_cast(cdigits) * sizeof(MANTTYPE))); } // Cleanup table structure @@ -505,38 +503,35 @@ bool equnum(_In_ PNUMBER a, _In_ PNUMBER b) // If the exponents are different, these are different numbers. return false; } + else if (diff > 0) + { + // If the exponents are different, these are different numbers. + return false; + } else { - if (diff > 0) - { - // If the exponents are different, these are different numbers. - return false; - } - else - { - // OK the exponents match. - pa = a->mant; - pb = b->mant; - pa += a->cdigit - 1; - pb += b->cdigit - 1; - cdigits = max(a->cdigit, b->cdigit); - ccdigits = cdigits; + // OK the exponents match. + pa = a->mant; + pb = b->mant; + pa += a->cdigit - 1; + pb += b->cdigit - 1; + cdigits = max(a->cdigit, b->cdigit); + ccdigits = cdigits; - // Loop over all digits until we run out of digits or there is a - // difference in the digits. - for (; cdigits > 0; cdigits--) + // Loop over all digits until we run out of digits or there is a + // difference in the digits. + for (; cdigits > 0; cdigits--) + { + da = ((cdigits > (ccdigits - a->cdigit)) ? *pa-- : 0); + db = ((cdigits > (ccdigits - b->cdigit)) ? *pb-- : 0); + if (da != db) { - da = ((cdigits > (ccdigits - a->cdigit)) ? *pa-- : 0); - db = ((cdigits > (ccdigits - b->cdigit)) ? *pb-- : 0); - if (da != db) - { - return false; - } + return false; } - - // In this case, they are equal. - return true; } + + // In this case, they are equal. + 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;