博客
关于我
【代码质量】C/C++静态检测/静态分析|TscanCode|cppcheck
阅读量:498 次
发布时间:2019-03-06

本文共 5342 字,大约阅读时间需要 17 分钟。

目录


TscanCode

TscanCode支持以下类型规则扫描:

  1. 空指针检查,包含可疑的空指针,判空后解引用比如Crash等共3类subid检查。
  2. 数据越界,Sprintf_S越界共1类subid检查。
  3. 内存泄漏,分配和释放不匹配同1类subid检查。
  4. 逻辑错误,重复的代码分支,bool类型和INT进行比较,表达式永远True或者false等共18类检查。
  5. 可疑代码检查,if判断中含有可疑的=号,自由变量返回局部变量等共计15类检查。
  6. 运算错误,判断无符号数小于0,对bool类型进行++自增等,共计11类检查。

链接:https://www.jianshu.com/p/f99d9a5d25ac

Windows下的安装与使用

TscanCode是腾讯静态分析团队开发的一款开源免费的C/C++静态分析工具,由于其比较简单实用,准确率较高,并且扫描C/C++代码不需要进行编译,所以个人觉得对C/C++项目开发挺有帮助的,就简单介绍一下该工具的安装与使用。

1.Tscancode下载安装

下载完成,点击下一步,一直到安装完成即可,因为Tscancode有腾讯团队维护,所以,建议使用TscanCode。安装完成后的界面如下图所示,简介明了。

2.扫描项目,导入即可,即可以导入一个文件夹,也可以导入单个文件。

3.可以进行规则配置,点击设置,

4.点击扫描,结果如图所示,关闭之前需要保存结果文件为xml,以便于下次查看。

5.点击具体某项列表,可以进行详细信息查看

Linux下的安装与使用

linux下不需要安装,下载tscancode 程序所在的目录,然后根据源码所在的地址,执行下面的命令即可

./tscancode    /源码地址/

例如:

[root@lrw888 TscanCodeV2.14.2395.linux]# ./tscancode ../../../../samples/cpp/[Preprocess] [1/94, 0%] [0] [Done] /usr/TscanCode/samples/cpp/UnintentionalOverflow.cpp[Preprocess] [2/94, 2%] [0] [Done] /usr/TscanCode/samples/cpp/arrayIndexCheckDefect.cpp[Preprocess] [3/94, 3%] [0] [Done] /usr/TscanCode/samples/cpp/arrayindexoutofbounds.cpp[Preprocess] [4/94, 4%] [0] [Done] /usr/TscanCode/samples/cpp/arrayindexthencheck.cpp[Preprocess] [5/94, 5%] [0] [Done] /usr/TscanCode/samples/cpp/assignif.cpp[Preprocess] [6/94, 6%] [0] [Done] /usr/TscanCode/samples/cpp/assignmentinassert.cpp[Preprocess] [7/94, 7%] [0] [Done] /usr/TscanCode/samples/cpp/autovar.cpp[Preprocess] [8/94, 8%] [0] [Done] /usr/TscanCode/samples/cpp/bitwiseonboolean.cpp[Preprocess] [9/94, 9%] [0] [Done] /usr/TscanCode/samples/cpp/boolfuncreturn.cpp[Preprocess] [10/94, 10%] [0] [Done] [/usr/TscanCode/samples/cpp/UnintentionalOverflow.cpp:4]: (Warning) Potentially overflowing expression around operator [ * ], which is evaluated using 4 byte arithmetic, and then used in a context that expects an expression of 8 bytes type.[/usr/TscanCode/samples/cpp/arrayIndexCheckDefect.cpp:10]: (Critical) Array buf[10] is accessed at index [i] may out of bound, as the index range check defect at line 6 may be wrong, mind the boundary value.[/usr/TscanCode/samples/cpp/arrayindexoutofbounds.cpp:11]: (Critical) Array 'a[10]' accessed at index 10, which is out of bounds.[/usr/TscanCode/samples/cpp/arrayindexthencheck.cpp:5]: (Critical) Array index 'index' is used before limits check.[/usr/TscanCode/samples/cpp/assignif.cpp:3] -> [/usr/TscanCode/samples/cpp/assignif.cpp:4]: (Warning) Mismatching assignment and comparison, comparison 'y==3' is always false.[/usr/TscanCode/samples/cpp/autovar.cpp:5]: (Warning) Pointer to local array variable returned.[/usr/TscanCode/samples/cpp/bufferaccessoutofbounds.cpp:5]: (Serious) Buffer is accessed out of bounds: sz[/usr/TscanCode/samples/cpp/checkNullDefect.cpp:4]: (Critical) It's wrong way to check if [npSt] is null, this is usually caused by misusing && and ||, e.g. if(p != NULL || p->a == 42)[/usr/TscanCode/samples/cpp/dereferenceAfterCheck.cpp:9]: (Serious) Comparing [obj] to null at line 3 implies that [obj ] might be null.Dereferencing null pointer [obj].[/usr/TscanCode/samples/cpp/dereferenceBeforeCheck.cpp:6]: (Serious) Null - checking [obj] suggests that it may be null, but it has already been dereferenced at line 4.[/usr/TscanCode/samples/cpp/dereferenceifnull.cpp:6]: (Critical) [npSt] is null dereferenced here, as codes at line 3 make it a null pointer.[/usr/TscanCode/samples/cpp/duplicateif.cpp:8]: (Warning) Expression is always false because 'else if' condition matches previous condition at line 4.[/usr/TscanCode/samples/cpp/explicitnulldereference.cpp:7]: (Critical) Dereferencing null pointer [npSt], [npSt] is assigned with null at line 3.[/usr/TscanCode/samples/cpp/funcretlengthasindex.cpp:5]: (Serious) Index [length] which is returned form [read] may cause index out of bound.[/usr/TscanCode/samples/cpp/funcretnullstatistic.cpp:54]: (Serious) return value of function [func_ret_null] [p] isn't checked-null, however [5] times checked-null elsewhere.[/usr/TscanCode/samples/cpp/incorrectlogicoperator.cpp:3]: (Warning) Logical disjunction always evaluates to true: a != 1 || a != 3.[/usr/TscanCode/samples/cpp/invaliddereferenceiterator.cpp:10]: (Serious) Iterator [iter] may be invalid here.[root@lrw888 TscanCodeV2.14.2395.linux]#

cppcheck

简介

 cppcheck 是一个静态代码检查工具,支持c, c++ 代码;作为编译器的一种补充检查,cppcheck对产品的源代码执行严格的逻辑检查。 执行的检查包括:

   1.  自动变量检查
   2.  数组的边界检查
   3.  class类检查
   4.  过期的函数,废弃函数调用检查
   5.  异常内存使用,释放检查
   6.  内存泄漏检查,主要是通过内存引用指针
   7.  操作系统资源释放检查,中断,文件描述符等
   8.  异常STL 函数使用检查
   9.  代码格式错误,以及性能因素检查

 

 

安装:

centos 下的

yum install cppcheck

使用:

 

1默认用法

$cppcheck --enable=all test.cpp $cppcheck --enable=all ./src

2使用选项–output-file将结果存储在report.txt中

$cppcheck --enable=all --output-file=report.txt test.cpp

也可以用输出重定向

$cppcheck --enable=all test.cpp 2> report.txt

更多参数:

  1. 使能检查规则:
    1. 默认:--enable=error
    2. --enable=all 
    3. --enable=unusedFuntion path
    4. --enable=style
  2. 规则定义:
    1. error:出现的错误
    2. warning:为了预防bug防御性编程建议信息
    3. style:编码格式问题(没有使用的函数、多余的代码等)
    4. portablity:移植性警告。该部分如果移植到其他平台上,可能出现兼容性问题
    5. performance:建议优化该部分代码的性能
    6. information:一些有趣的信息,可以忽略不看的。
  3. 保存结果到文件中:重定向“>”
  4. -j 3 使用3个线程,如果代码工程太大,可以使用15-20个,自己随意发挥,不过还是参考cpuinfo

运行cppcheck -h可以得到参数的提示,这里直介绍我用到的参数

cppcheck -j 3 --enable=all  search/*

-j参数指定的是检查线程的个数,如果需要检查代码的量很大,-j参数还是很有用的

--enable指定当前指定的检查级别,可选的参数有all,style,information等

 

Windows下的安装与使用

如果是Windows的版本呢,就这样使用:

 

1、将Cppcheck绿色版cppcheck.rar解压;

2、打开cppcheckgui.exe;

3、菜单 check->directory;

4、选择源代码目录,确认;

5、Cppcheck自动开始走查;

 

cppcheck + jenkins

转载地址:http://wgndz.baihongyu.com/

你可能感兴趣的文章
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>