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

前端 未结 2 2020
孤独总比滥情好
孤独总比滥情好 2021-02-10 02:24

Code analysis:

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

Warning C26454:

2条回答
  •  旧巷少年郎
    2021-02-10 02:53

    //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

提交回复
热议问题