使用 CppDepend 无缝实现 Cert C 合规
使用 CppDepend 无缝实现 Cert C 合规
在当今竞争激烈的软件开发环境中,维护高质量和安全的代码至关重要。Cert C 编码标准是创建可靠且安全的 C 应用程序的指南。但开发人员如何在其项目中高效地实现 Cert C 合规?
CppDepend 登场了,这是一款专为帮助您应对 Cert C 标准复杂性而设计的综合工具。通过利用静态分析技术,CppDepend 不仅引导您实现合规,还能帮助您提高整体代码质量和安全性。
要学习CERT C标准,您可以打印 SEI CERT C编码标准文档 由卡内基梅隆大学。
在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编码标准文档 由卡内基梅隆大学。
预处理器 (PRE)
PRE30-C: Do not create a universal character name through concatenation:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_ucn_escape_incomplete"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}PRE31-C: Avoid side effects in arguments to unsafe macros:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="bugprone-macro-repeated-side-effects"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}声明和初始化
DCL30-C: Declare objects with appropriate storage durations:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="danglingLifetime"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}DCL31-C: 使用前声明标识符:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="err_missing_type_specifier"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}DCL37-C: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}DCL41-C: Do not declare variables inside a switch statement before the first case label:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.Type=="Rule6-4-4"
select new { issue,issue.FilePath,Line=issue.BeginLine}表达式 (Exp)
EXP30-C: 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}EXP32-C: Do not access a volatile object through a nonvolatile reference:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang"
&& issue.Type=="err_typecheck_convert_incompatible_volatile" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP33-C: 不要读取未初始化的内存:
此编码标准通过以下查询计算:
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}EXP34-C: 不要解引用空指针:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="nullPointer" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP35-C: Do not modify objects with temporary lifetime:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang"
&& issue.Type=="err_typecheck_expression_not_modifiable_lvalue" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP36-C: Do not cast pointers into more strictly aligned pointer types:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_cast_align" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP37-C: Call functions with the correct number and type of arguments:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="err_typecheck_call_too_many_args" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP39-C: Do not access a variable through a pointer of an incompatible type:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="invalidPointerCast" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP40-C: 不要修改常量对象:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="err_typecheck_convert_incompatible_const" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP44-C: Do not rely on side effects in operands to sizeof,\_Alignof, or \_Generic:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_side_effects_unevaluated_context"
&& issue.Description.Contains(("lambda")) select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP45-C:Do not perform assignments in selection statements:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_condition_is_assignment" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP46-C. Do not use a bitwise operator with a Boolean-like operand:
此编码标准通过以下查询计算:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_precedence_bitwise_rel" select
new { issue,issue.FilePath,Line=issue.BeginLine}浮点 (FLP)
FLP30-C:Do not use floating-point variables as loop counters:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-flp30-c"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}FLP34-C: Ensure that floating-point conversions are within range of the new type:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_impcast_float_integer"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}数组 (ARR)
ARR30-C. Do not form or use out-of-bounds pointers or array subscripts:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="arrayIndexOutOfBounds"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ARR36-C: Do not subtract or compare two pointers that do not refer to the same array:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="comparePointers"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ARR39-C: Do not add or subtract a scaled integer to a pointer:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_ptr_arith_exceeds_bounds"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}字符和字符串 (STR)
STR30-C: Do not attempt to modify string literals:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="stringLiteralWrite"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}STR34-C: Cast characters to unsigned char before converting to larger integer sizes:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="bugprone-signed-char-misuse"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}内存管理 (MEM)
MEM30-C: 不要访问已释放的内存:
此编码标准通过以下查询计算:
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
}MEM31-C. Free dynamically allocated memory when no longer needed:
此编码标准通过以下查询计算:
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
}输入/输出 (FIO)
FIO38-C: 不要复制FILE对象:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-fio38-c"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}FIO42-C: Close files when they are no longer needed:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="resourceLeak"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}环境 (ENV)
ENV33-C: 不要调用system():
此编码标准通过以下查询计算:
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
}信号 (SIG)
SIG30-C: Call only asynchronous-safe functions within signal handlers:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="bugprone-signal-handler"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}错误处理 (ERR)
ERR34-C: Detect errors when converting a string to a number:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-err34-c"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}并发 (CON)
CON36-C. Wrap functions that can spuriously wake up in a loop:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="bugprone-spuriously-wake-up-functions"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}杂项 (MSC)
MSC30-C: Do not use the rand() function for generating pseudorandom numbers:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-msc30-c"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MSC32-C: Properly seed pseudorandom number generators:
此编码标准通过以下查询计算:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-msc32-c"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}