Warning C26454: Arithmetic overflow: '-' operation produces a negative unsigned result at compile time (io.5)

Andrew Truckle picture Andrew Truckle · Jul 2, 2018 · Viewed 8.4k times · Source

Code analysis:

ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_HISTORY_TYPE,
  &CAssignHistoryDlg::OnTcnSelchangeTabHistoryType)

Warning C26454:

Arithmetic overflow: '-' operation produces a negative unsigned result at compile time (io.5).

The definition of TCN_SELCHANGE is:

#define TCN_FIRST (0U-550U)
#define TCN_SELCHANGE           (TCN_FIRST - 1)

I can't see what else I can do!

Answer

Barmak Shemirani picture Barmak Shemirani · Jul 2, 2018
//windows header file:
#define TCN_FIRST (0U-550U)
#define TCN_SELCHANGE           (TCN_FIRST - 1)

//user file:
...
unsigned int i = TCN_SELCHANGE;

Above code is valid in C++, it should compile without any warning. There is no overflow, that's just meant to be -550U It would be more clear if they wrote it as #define TCN_FIRST 0xFFFFFDDA or 0xFFFFFFFFU-549U

Code Analysis seems to uses a different method and sees an overflow.

Possible solution:

Disable the warning in code:

#pragma warning( push )
#pragma warning( disable : 26454 )

BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
    ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnTcnSelchangeTabHistoryType)
END_MESSAGE_MAP()

#pragma warning( pop )

Or, disable the warning in Code Analysis rule
Use the code analysis rule set editor

enter image description here