本文共 5342 字,大约阅读时间需要 17 分钟。
目录
TscanCode支持以下类型规则扫描:
链接:https://www.jianshu.com/p/f99d9a5d25ac
TscanCode是腾讯静态分析团队开发的一款开源免费的C/C++静态分析工具,由于其比较简单实用,准确率较高,并且扫描C/C++代码不需要进行编译,所以个人觉得对C/C++项目开发挺有帮助的,就简单介绍一下该工具的安装与使用。
1.Tscancode下载安装
下载完成,点击下一步,一直到安装完成即可,因为Tscancode有腾讯团队维护,所以,建议使用TscanCode。安装完成后的界面如下图所示,简介明了。
2.扫描项目,导入即可,即可以导入一个文件夹,也可以导入单个文件。
3.可以进行规则配置,点击设置,
4.点击扫描,结果如图所示,关闭之前需要保存结果文件为xml,以便于下次查看。
5.点击具体某项列表,可以进行详细信息查看
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 是一个静态代码检查工具,支持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
更多参数:
运行cppcheck -h可以得到参数的提示,这里直介绍我用到的参数
cppcheck -j 3 --enable=all search/*
-j参数指定的是检查线程的个数,如果需要检查代码的量很大,-j参数还是很有用的
--enable指定当前指定的检查级别,可选的参数有all,style,information等
如果是Windows的版本呢,就这样使用:
1、将Cppcheck绿色版cppcheck.rar解压;
2、打开cppcheckgui.exe;
3、菜单 check->directory;
4、选择源代码目录,确认;
5、Cppcheck自动开始走查;
转载地址:http://wgndz.baihongyu.com/