主要内容

MISRA C++:2023 Rule 4.1.1

A program shall conform to ISO/IEC 14882:2017 (C++17)

自 R2024b 起

描述

A program shall conform to ISO/IEC 14882:2017 (C++17). 1

理由

MISRA™ C++:2023 规范基于 C++17 标准 (ISO/IEC 14882:2017)。超出该标准范围的语言扩展不在这些规范的覆盖范围内。如果您根据 MISRA C++:2023 规范进行安全编码,则必须严格遵守 C++17 标准,以便这些规范覆盖您的代码。

Polyspace 实现

规则检查项会报告不属于 C++17 标准严格定义范围内的语言扩展。这些扩展可能包含在更高版本的标准中,或是特定方言(如 GCC 或 Microsoft® Visual C++®)的一部分。违规会被报告为编译错误,报告方式与严格遵循 C++17 标准的编译器一致。

为了有效使用此规则,请确保为 Polyspace® Bug Finder™ 分析启用 C++17 标准。有关详细信息,请参阅 C++ 标准版本 (-cpp-version)

故障排除

如果您预期会出现违规,而 Polyspace 未报告该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

void AccessMemory() {
    int* ptr = nullptr; // Intentionally creating a null pointer

    __try {  // Noncompliant 
        *ptr = 42; // This line will cause an access violation exception
    }
    __finally {
        // Code here is executed whether an exception occurred or not
        if (AbnormalTermination()) {
            // Code here is executed if the try block threw an exception
        } else {
            // Code here is executed on success
        }
    }
}

此示例使用了 Microsoft Visual C++ 语言扩展 __try__finally,这违反了此规则。可以使用标准的 C++ try-catch 机制来捕获异常,并且可以使用 C++ RAII 惯用法来释放资源,即使存在异常也是如此。

检查信息

组:一般原则
类别:必需
PQL 名称:std.misra_cpp_2023.R4_1_1

版本历史记录

在 R2024b 中推出


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.