You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const char* key_value(const int key) {
const char * value = 0;
if (key != 0) {
value = "Zeus";
} else if (key != 1) {
value = "Hades";
}
if (key != 2) { // Should be 'else if' here.
value = "Poseidon"; // defect: unused_value Zeus Hades never used
}
else {
value = "Unknow
}
return result;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
TCA支持首批7条规则,重点关注资源泄露、无用值检查、数组越界、缓冲区溢出等问题。
支持语言:C/C++
注:该规则包由TCA独立工具支持,需申请授权免费使用,申请传送门:《CLS使用文档》
启用规则包
分析方案 -> 代码检查 -> C/C++代码质量缺陷规则 -> 启用/查看规则
使用手册(部分)
线程锁检查
在使用多线程对文件全局变量或类成员在进行读写时,工具会对未正确的进行上锁操作和上锁异常而引发死锁的情况进行检查。
int counter = 0;
std::mutex mtx; // 保护counter
void increase1() {
while (1) {
if (counter <= 1000)
counter++; // defect: missing_lock
else
break;
}
}
void increase2() {
while (1) {
mtx.lock(); // example_lock
if (counter <= 1000)
counter++;
else
break;
mtx.unlock(); // example_release
}
}
无效值检查
检查那些赋予给变量的值是否正确被使用,存在连续两次赋予变量值的情况,视为第一次赋予的值未被正确使用,报出错误。 两次连续赋值可能存在条件控制语句出现错误、变量名拼写错误等情况。
const char* key_value(const int key) {
const char * value = 0;
if (key != 0) {
value = "Zeus";
} else if (key != 1) {
value = "Hades";
}
if (key != 2) { // Should be 'else if' here.
value = "Poseidon"; // defect: unused_value Zeus Hades never used
}
else {
value = "Unknow
}
return result;
}
更多内容请参考:【C/C++】代码质量缺陷规则包
关注我们,持续为您的代码助力!
公众号(腾讯云静态分析)回复「进群」获取官方微信交流群传送门_
Beta Was this translation helpful? Give feedback.
All reactions