Login Page - Create Account

Support Board


Date/Time: Fri, 29 Nov 2024 01:35:29 +0000



Suggestion to remove compiler warnings for sierrachart.h and SCString.h

View Count: 495

[2023-03-11 07:12:26]
User719512 - Posts: 267
Hi Sierra Engineering,

Suggestion to remove compiler warnings for sierrachart.h and SCString.h when compiling with Visual Studio.

When compiling with Visual Studio, I get warnings like this:
Warning  C26451  Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2).

Suggested fixes are as follows

sierrachart.h: Use floats for comparisons.


  int Round(float Number)
  {
    int IntegerResult = static_cast <int>(Number);

    if ((Number > 0.0f) && ((Number-IntegerResult) >= 0.5f))
      return ++IntegerResult;
    else if ((Number < 0.0f) && ((Number - IntegerResult) <= -0.5f))
      return --IntegerResult;

    return IntegerResult;

  }

  int64_t Round64(double Number)
  {
    int64_t IntegerResult = static_cast <int64_t>(Number);

    if ((Number > 0.0f) && ((Number - IntegerResult) >= 0.5f))
      return ++IntegerResult;
    else if ((Number < 0.0f) && ((Number - IntegerResult) <= -0.5f))
      return --IntegerResult;

    return IntegerResult;

  }


SCString.h: Use static_cast<rsize_t> for all 5 strncpy_s calls (1 shown below)


      strncpy_s(m_String, static_cast<rsize_t>(BufferSize), SourceString, static_cast<rsize_t>(StringLength));


SCString.h: Use static_cast<size_t> to remove lnt-arithmetic-overflow: A sub-expression may overflow before being assigned to a wider type.


  vsprintf_s
    ( NewStringBuffer + PriorLength
    , static_cast<size_t>(TargetLength) + 1
    , FormatString
    , ArgumentList
    );

Date Time Of Last Edit: 2023-03-11 07:32:06
[2023-03-13 20:38:24]
ForgivingComputers.com - Posts: 960
You can turn off warnings by adding
/W0
to the Additional Compiler Parameters.
[2023-03-13 20:47:47]
User719512 - Posts: 267
Thank you @bradh. Instead of turning off warnings, one can change the headers, as described above, to suppress the warnings and still allow for warnings for any new programming issues rather than turning off all warnings.

This is easy to do by the way by copying the Sierra headers, modifying them as needed, and building locally (which I do).

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account