☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

注意逻辑表达式内的空指针解引用

14.2 ID_nullDerefInExp
目录 › next › previous

在逻辑表达式中,需注意逻辑关系以及运算符优先级,不可出现空指针解引用等问题。

示例(设 foo、bar 是指针 p 所指对象的非静态成员函数):

p || p->foo();  // Non-compliant

当 p 为空时执行“||”的右子表达式,恰好使空指针被解引用。

p && p->foo() || p->bar();  // Non-compliant

“&&”的优先级高于“||”,由“||”的左子表达式可知 p 可能为空,而右子表达式却没有限制,导致空指针被解引用。

p->foo() && p;  // Non-compliant

这是颠倒了对指针的判断和解引用次序,属于语言运用错误。

相关

ID_nullDerefInScp

依据

ISO/IEC 9899:1999 6.3.2.1(1)-undefined ISO/IEC 9899:1999 6.5.3.2(4)-undefined ISO/IEC 9899:2011 6.3.2.1(1)-undefined ISO/IEC 9899:2011 6.5.3.2(4)-undefined ISO/IEC 14882:2003 8.3.2(4)-undefined ISO/IEC 14882:2011 8.3.2(5)-undefined

参考

CWE-476 CWE-783 C++ Core Guidelines ES.65
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.