Menu

How to deal with VERIFY macro causing Either the condition is redundant or there is possible null pointer dereference

2021-03-10
2021-03-10
  • Timo K Suoranta

    Timo K Suoranta - 2021-03-10

    Here is my code:

    #if _MSC_VER
    #define FATAL(format, ...) do { fmt::print("{}:{} " format, __FILE__, __LINE__, ##__VA_ARGS__); DebugBreak(); abort(); } while (1)
    #define VERIFY(expression) do { if (!(expression)) { FATAL("assert {} failed in {}", #expression, __func__); } } while (0)
    
    #else
    
    #define FATAL(format, ...) do { fmt::print("{}:{} " format, __FILE__, __LINE__, ##__VA_ARGS__); __builtin_trap(); __builtin_unreachable(); } while (1)
    #define VERIFY(expression) do { if (!(expression)) { FATAL("assert {} failed in {}", #expression, __func__); } } while (0)
    
    #endif
    
    void Menu::render()
    {
        Expects(m_application);
        auto* surface = m_application->get_context_window();
        VERIFY(surface != nullptr);
        int iw = surface->get_width();
        int ih = surface->get_height();
    

    The last three lines seem to trigger cppcheck to complain that either check is redundant, or there can be a nullpointer dereference. What do I need to do to fix this? Thanks!

    Actual code and cppcheck report: https://app.codacy.com/gh/tksuoran/erhe/issues?categoryType=ErrorProne

     
  • Éric Malenfant

    Éric Malenfant - 2021-03-10

    A guess: This is probably because CppCheck does not "know" that __builtin_trap() does not return. You may try using the --library option to point it to gnu.cfg

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.