1.splint简介
splint是一个C语言程序静态检查工具,可检测出程序的安全漏洞和常规编程错误,如未使用的变量,类型不一致,无法执行的代码,无限循环等错误。静态语法分析在整个项目编码阶段的位置一般是程序编译通过之后,代码审查之前。
2.splint安装
3.splint使用方法
splint提供了以下三种方式进行检查控制:
- 命令行标志(flags):splint支持几百个标志来控制检查和消息报告,标志前置“+”表示打开该标志,“-”表示关闭该标志。为增强标志名的可读性,标志中的连字符(-)和下划线(_)将会被忽略,例如warn-flags和warn_flags都将被当做warnflags选项处理。
- 选项文件:若存在/.splintrc,系统默认会加载该选项文件;若工作路径下存在.splintrc文件,系统将读取该文件,并覆盖掉/.splintrc中的相应设置。而命令行标志将覆盖这两个文件中的相关设置。还可以通过-f 命令行参数指定选项文件filename。在Ubuntu系统中,选项文件格式可参考/usr/share/doc/splint/examples/splint.splintrc。
- 格式化注释:可以通过在本地C程序代码中增加格式化注释,提供关于类型、变量或函数接口的额外信息。所有的格式化注释以“/@”开头,以“@/”结尾(可以使用-commentchar 设置成其他的格式化注释标识符)。例如,/@null@/表示一个参数可能是NULL。
在Linux命令行下直接运行命令splint .c就可以对.c文件进行检测。通常会出现很多warning信息,其典型格式如下:
sample.c: (in function faucet)
sample.c:11:12: Fresh storage x not released before return
A memory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit warning)
sample.c:5:47: Fresh storage x allocated
第一行显示了出错函数的名称;第二行先给出错误所在行数和列数,后面接着警告信息的具体内存,本例中是一个内存泄漏错误。
4.splint命令行选项
5.splint使用示例
最简单的例子:
splint: splint -weak -showscan -showsummary -usestderr -posix-lib -Iinclude example.c
这个例子的作用就是: 使用splint工具来检查当前目录下的example.c文件,其中include是头文件搜索路径;-weak -showscan -showsummary -usestderr -posix-lib是传递给splint的参数。
一个较为复杂的例子(可不看):
pkg_y = pkg_m = pkg_n = DIR_ROOT ?= $(shell pwd) DIR_TMPFS ?= $(DIR_ROOT)/tmpfs include makefile.in define PKG_PATH $(filter %/$(1), $(pkg_y) $(pkg_m)) endef %_splint: if [ ! -d $(call PKG_PATH,$(@:_splint=)) ]; then \ echo "$(@:_splint=) is not built in!"; \ else \ echo ------ splint $(@:_splint=) ------; \ for x in $(shell find $(call PKG_PATH,$(@:_splint=)) -name "*.c"); \ do \ splint -f $(DIR_ROOT)/config/splint.opt -I$(DIR_TMPFS)/include \ $(addprefix -I,$(patsubst %/,%,$(sort $(dir $(shell find $(call PKG_PATH,$(@:_splint=)) -name "*.h"))))) $$x; \ done; \ fi
-weak -showscan -showsummary -usestderr -posix-lib
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/205639.html原文链接:https://javaforall.net
