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

不应抛出 NULL

7.25 ID_throwNULL
目录 › next › previous

虽然 NULL 表示空指针,但在相当一部分实现中 throw NULL 相当于 throw 0,无法区分指针与整数。

示例:

void foo() {
    throw NULL;  // Non-compliant
}

void bar() {
    try {
        foo();
    } catch (int) {  // Which handler?
        ....
    } catch (int*) {
        ....
    }
}

例中 throw NULL 意在抛出空指针,然而会被 catch(int) 子句捕获。

相关

ID_deprecatedNULL ID_throwNonExceptionType ID_throwPointer

依据

ISO/IEC 14882:2003 C.2.2.3(1)-implementation ISO/IEC 14882:2011 C.3.2.4(1)-implementation ISO/IEC 14882:2017 C.5.2.7(1)-implementation

参考

CWE-351 MISRA C++ 2008 15-1-2
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.