使用 CppDepend 无缝实现 Cert C++ 合规
使用 CppDepend 无缝实现 Cert C++ 合规
在当今竞争激烈的软件开发环境中,维护高质量和安全的代码至关重要。Cert C++ 编码标准是创建可靠且安全的 C++ 应用程序的指南。但开发人员如何在其项目中高效地实现 Cert C++ 合规?
CppDepend 登场了,这是一款专为帮助您应对 Cert C++ 标准复杂性而设计的综合工具。通过利用静态分析技术,CppDepend 不仅引导您实现合规,还能帮助您提高整体代码质量和安全性。
要了解 CERT C++ 标准,您可以打印这份 SEI CERT C++编码标准文档 作者Aaron Ballman。
在CppDepend中使用CERT C++规则
创建新的 CppDepend 项目时,会弹出一个窗口,显示您想要包含在分析中的所有编码标准。要包含 Cert C++ 编码标准,请勾选 Cert 选项并选择 Cert C++。
分析项目后,导航到 Queries and Rules Explorer 部分,从左侧菜单中选择 Cert C++ 规则(在下图中以红色突出显示)。这将在右侧显示所有相关的 Cert C++ 规则。单击某条规则时,将自动生成相应的 CQLinq 查询和相关源代码。此外,Metrics View 部分提供问题位置和重要性的可视化。
将 CERT C++ 规则纳入 HTML 报告
要在生成的 HTML 报告中包含 Cert C++ 规则(在 DevOps Edition 中可用),只需右键单击 Cert C++ 组并选择“List Code Queries of this Group in a dedicated section in Report”。
CERT C++规则列表
CppDepend 支持 38 个 Cert C++ 标准查询。
要更深入地了解每条规则及其功能,请参阅 SEI CERT C++编码标准文档 作者Aaron Ballman。
声明和初始化
DCL50-CPP: Do not define a C-style variadic function:
此编码标准通过以下查询计算:
warnif **count** > **0**
from m in **JustMyCode**.Methods where m.IsVariadic
select mDCL51-CPP: Do not declare or define a reserved identifier:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_pp_macro_is_reserved_id" select
new { issue,issue.FilePath,Line=issue.BeginLine}DCL52-CPP: Never qualify a reference type with const or volatile:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="err_invalid_reference_qualifier_application" select
new { issue,issue.FilePath,Line=issue.BeginLine}DCL53-CPP: Do not write syntactically ambiguous declarations:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_empty_parens_are_function_decl" select
new { issue,issue.FilePath,Line=issue.BeginLine}DCL54-CPP: Overload allocation and deallocation functions as a pair in the same scope:
此编码标准通过以下查询计算:
warnif **count** > **0**
from n in **JustMyCode**.Methods.Where(m=>m.SimpleName=="operatornew") where **JustMyCode**.Methods.Where(m=>m.SimpleName=="operatordelete").Count()==**0**
select nDCL57-CPP: Do not let exceptions escape from destructors or deallocation functions:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="exceptThrowInDestructor" select
new { issue,issue.FilePath,Line=issue.BeginLine}DCL58-CPP: Do not modify the standard namespaces:
此编码标准通过以下查询计算:
warnif **count** > **0** from codeElement in **JustMyCode**.CodeElements where codeElement.Parents.Where(t=>t.IsNamespace && t.Name=="std").Count()>**0**
select codeElementDCL59-CPP: Do not define an unnamed namespace in a header file:
此编码标准通过以下查询计算:
warnif **count**>**0** from n in **Namespaces**
where n.Name.StartsWith("anonymous_namespace") && n.Name.Contains(".h")
select n表达式 (Exp)
EXP50-CPP: Do not depend on the order of evaluation for side effects:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_unsequenced_mod_use" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP52-CPP: Do not rely on side effects in unevaluated operands:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_side_effects_unevaluated_context" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP53-CPP: 不要读取未初始化的内存:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && (issue.Type=="uninitvar" || issue.Type=="uninitdata") select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP54-CPP: Do not access an object outside of its lifetime:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_ret_stack_addr_ref" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP55-CPP: Do not access a cv-qualified object through a cvunqualified type:
此编码标准通过以下查询计算:
warnif **count** > **0**
from m in **JustMyCode**.Methods where
m.IsUsing("Keywords.const_cast".AllowNoMatch()) select mEXP57-CPP: Do not cast or delete pointers to incomplete classes:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_delete_incomplete" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP58-CPP: Pass an object of the correct type to va\_start:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_va_start_type_is_undefined" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP61-CPP: A lambda object must not outlive any of its reference captured objects:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="returnDanglingLifetime"
&& issue.Description.Contains(("lambda")) select new { issue,issue.FilePath,Line=issue.BeginLine}EXP62-CPP: Do not access the bits of an object representation that are not part of the object’s value representation:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_dyn_class_memaccess" select
new { issue,issue.FilePath,Line=issue.BeginLine}内存管理 (MEM)
MEM50-CPP: 不要访问已释放的内存:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="nullPointer"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MEM51-CPP:Properly deallocate dynamically allocated resources:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_mismatched_delete_new"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MEM53-CPP. Explicitly construct and destruct objects when manually managing object lifetime:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="uninitdata"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MEM56-CPP:Do not store an already-owned pointer value in an unrelated smart pointer:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="doubleFree"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MEM57-CPP: Avoid using default operator new for over-aligned types:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-mem57-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}异常和错误处理 (ERR)
ERR51-CPP: 处理所有异常:
此编码标准通过以下查询计算:
warnif **count** > **0**
from m in **JustMyCode**.Methods where m.IsEntryPoint
&& m.IsUsing("Keywords.generic_catch".AllowNoMatch()) select mERR52-CPP: 不要使用setjmp()或longjmp():
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="cert-err52-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR53-CPP: Do not reference base classes or class data members in a constructor or destructor function-try-block handler:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang"
&& issue.Type=="warn_cdtor_function_try_handler_mem_expr"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR54-CPP: Catch handlers should order their parameter types from most derived to least derived:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang"
&& issue.Type=="warn_exception_caught_by_earlier_handler"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR58-CPP. Handle all exceptions thrown before main() begins executing:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-err58-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR60-CPP: Exception objects must be nothrow copynconstructible:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-err60-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR61-CPP: Catch exceptions by lvalue reference:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="catchExceptionByValue"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}面向对象编程 (OOP)
OOP50-CPP: Do not invoke virtual functions from constructors or destructors:
此编码标准通过以下查询计算:
from m in **JustMyCode**.Methods.Where(m=>(m.IsConstructor || m.IsDestructor)
&& m.MethodsCalled.Where(s=>s.IsVirtual).Count()>**0**)
select mOOP52-CPP: Do not delete a polymorphic object without a virtual destructor:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_delete_non_virtual_dtor"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}OOP53-CPP: Write constructor member initializers in the canonical order:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_initializer_out_of_order"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}OOP54-CPP: Gracefully handle self-copy assignment:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="operatorEqToSelf"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}OOP57-CPP. Prefer special member functions and overloaded operators to C Standard Library functions:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-oop57-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}OOP58-CPP: Copy operations must not mutate the source object:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-oop58-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}杂项 (MSC)
MSC50-CPP: Do not use std::rand() for generating pseudorandom numbers:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-msc50-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MSC51-CPP. Ensure your random number generator is properlyseeded:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-msc51-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}容器
CTR50-CPP: Guarantee that container indices and iterators are within the valid range:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_array_index_exceeds_bounds"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}