TscanCode介绍
TscanCode 是腾讯研发的静态代码扫描工具,最早的版本是基于 cppcheck 二次开发。之后又重新自研,不仅支持 C++,还支持 C#,Lua 语言,在发掘 C++ 空指针、越界、未初始化、C#空引用、Lua变量未初始化等比较有效。TScanCode 比较适用于游戏开发代码扫描,有着不错的准确率和效率,其性能测试可以见:https://blog.csdn.net/wetest_tencent/article/details/51516347。
TscanCode 主要能够发现的问题如下:
1、自动变量检查: 返回自动变量(局部变量)指针;
2、越界检查:数组越界返回自动变量(局部变量)指针;
3、类检查:构造函数初始化;
4、内存泄露检查;
5、空指针检查;
6、废弃函数检查;
下载TscanCode
TscanCode 已经在 Github 上开源,地址是:https://github.com/Tencent/TscanCode。
其中项目文件夹对应的如下:
release ->编译后的二进制文件,分别有Linux、Mac、Windows平台
samples ->测试的代码样例,分别有C++、C#、Lua语言
trunk ->TscanCode源代码
为了方便起见,下载对应平台的二进制可执行文件便可以运行,其中 Win 平台是有 GUI 图形界面,Linux 中要使用命令和手动配置规则,下面介绍一下在 Win 和 Linux 平台使用 TscanCode。
Win下使用TscanCode
安装 TscanCode 后,双击打开后,主界面上选择:扫描文件夹,然后打开代码的文件夹,点击:开始扫描。
扫描完成后会出现结果:
双击可以查看代码中存在的错误,比如:
对于扫描后的结果,可以保存为 xml 配置文件,方便下一次直接在主界上直接打开:结果查看,选中该文件即可。
还可以在设置中选择扫描规则,每个规则都有对应的代码实例可供参考。
Linux下使用TscanCode
我们将 Linux 版本二进制压缩包解压,然后进入 TscanCodeV2.14.2395.linux 目录,有一个 cfg 文件夹和一个 tscancode 二进制文件,需要使用 chmod +x tscancode
对其加上可执行权限。
在 Linux 下可通过 cfg/cfg.xml 对扫描的规则进行配置,其中通过设置 value=0 则禁用,value=1 则启用,或直接使用默认的扫描规则。
需要注意的是:扫描的路径中不能包含 root 文件夹,TscanCode特殊性。
然后再执行如下命令对 samples/cpp/下的所有 C++ 文件代码进行扫描:
./tscancode --xml --enable=all -q /home/crazyang/samples/cpp/ >scan_result.xml 2>&1
执行后,就会看到在该目录下生成了 scan_result.xml 文件,文件中就是扫描的结果,如果我们需要扫描结果重定向到文件中,最好加上 -q 参数,否则会将扫描的进度信息也重定向到文件中,这些信息仅在终端中显示进度有用。
注意:如果不熟悉后面的参数,可以使用 ./tscancode -h
查看帮助文档。
$ ./tscancode -h
TscanCode - A tool for static C/C++ code analysis
Syntax:
tscancode [OPTIONS] [files or paths]
If a directory is given instead of a filename, *.cpp, *.cxx, *.cc, *.c++, *.c,
*.tpp, and *.txx files are checked recursively from the given directory.
Options:
-D<ID> Define preprocessor symbol. Unless --max-configs or
--force is used, TscanCode will only check the given
configuration when -D is used.
Example: '-DDEBUG=1 -D__cplusplus'.
-U<ID> Undefine preprocessor symbol. Use -U to explicitly
hide certain #ifdef <ID> code paths from checking.
Example: '-UDEBUG'
--enable=<id> Enable additional checks. The available ids are:
* all
Enable all checks. It is recommended to only
use --enable=all when the whole program is
scanned, because this enables unusedFunction.
* warning
Enable warning messages
* style
Enable all coding style checks. All messages
with the severities 'style', 'performance' and
'portability' are enabled.
* performance
Enable performance messages
* portability
Enable portability messages
* information
Enable information messages
* unusedFunction
Check for unused functions. It is recommend
to only enable this when the whole program is
scanned.
* missingInclude
Warn if there are missing includes. For
detailed information, use '--check-config'.
Several ids can be given if you separate them with
commas. See also --std
-h, --help Print this help.
-I <dir> Give path to search for include files. Give several -I
parameters to give several paths. First given path is
searched for contained header files first. If paths are
relative to source files, this is not needed.
-j <jobs> Start [jobs] threads to do the checking simultaneously.
-q, --quiet Do not show progress reports.
--xml Write results in xml format to error stream (stderr).
Example usage:
# Recursively check the current folder. Print the progress on the screen and
# write errors to a file:
tscancode . 2> err.txt
# Recursively check ../myproject/ and don't print progress:
tscancode --quiet ../myproject/
# Check test.cpp, enable all checks:
tscancode --enable=all test.cpp
# Check f.cpp and search include files from inc1/ and inc2/:
tscancode -I inc1/ -I inc2/ f.cpp
最后得到的 scan_result.xml 结果文件,可以下载下来使用 Excel 工具打开 XML 报告(为了处理更直观),在左侧插入一列处理情况。
开发人员根据报告对代码上下文进行分析,判断是否为工具误报。
对于确认为问题的代码,由开发人员处理后重新进行代码安全静态扫描,直到问题关闭。
参考:
https://github.com/Tencent/TscanCode
静态代码检查
C++代码质量扫描主流工具深度比较
代码扫描工具TScanCode