cppcheck 空指针取消引用:m_buffer - 否则检查空指针是多余的

cppcheck null pointer dereference: m_buffer - otherwise it is redundant to check it against null

代码工作正常,但是当我在 cppcheck 上检查它时,我发现了空指针取消引用错误,我无法理解如何解决它。任何想法将不胜感激

这是我遇到错误的代码部分

#ifdef DEBUG_LEVEL_MID
    std::clog << "STARTING FUNCTION int ConfigurationType::ExecuteMessageType()" << std::endl;
    std::clog << "message with code : " << message_to_execute->code << "will be tried o executed" << std::endl;
    #endif    

    if(!message_to_execute)
    {
        #ifdef DEBUG_LEVEL_ERROR
        std::cerr << "message_to_execute is null at: int ConfigurationType::ExecuteMessageType()" << std::endl;
        #endif    
        #ifdef DEBUG_LEVEL_MID
        std::clog << "message_to_execute is NULL at int ConfigurationType::ExecuteMessageType()" << std::endl;
        std::clog << "ENDING FUNCTION (0): int ConfigurationType::ExecuteMessageType()" << std::endl;
        #endif    
        return 0;
    }

错误是:可能的空指针取消引用:message_to_execute - 否则检查它是否为空是多余的。

在检查指针是否有效之前,您已经在访问该指针:message_to_execute->code。将其移至 if 语句中,警告将消失。

CPPCheck 是正确的,如果它是一个 nullptr,它将是一个 nullptr 解引用,如果不是,您将检查它是否是?

您在此处取消引用 message_to_executestd::clog << "message with code : " << message_to_execute->code

这意味着后面的 if (!message_to_execute) 是多余的,因为不允许取消引用空指针,因此允许编译器假定 message_to_execute 不为空,并删除测试。