cppcheck null指针取消引用:m_buffer-否则,针对null进行检查是多余的

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

本文关键字:null 针对 进行检查 多余 否则 取消 指针 引用 cppcheck buffer-      更新时间:2023-10-16

代码工作正常,但当我在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;
}

错误为:可能的null指针取消引用:message_to_execute-否则,根据null进行检查是多余的。

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

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

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

CPPCheck是对的,如果它是一个nullptr,它将是一个nullptr解引用,如果不是,你将检查它是否是?