linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > Linux入门与提高 > 使用与技巧 >
栏目导栏
资料搜索
热门文章
·VMware Tools的安装步骤
·应用Linux下两种中文输入法
·GRUB FOR DOS 引导安装LINUX和
·linux下BT软件介绍
·Linux中用ALSA驱动声卡流程详解
·Linux 170个常见问题的详细解答
·什么是ISO文件?
·虚拟机软件vmware使用教程--使
·BabyLinux制作过程详解
·Linux挂接(mount)命令的使用方
·Linux必学的网络操作命令
·ffmpeg命令使用详解
·Linux操作系统Ifconfig命令详细
·在Linux下制作工资表
·vmware下的fedora 6的vmware t
最新文章
·CentOS5安装apache和svk使用域
·Ubuntu Linux系统Sudo命令无效
·Uboot对非Linux kernel的引导
·Fedora和XP双系统中网卡Realte
·解决Linux操作系统下部分文件乱
·双系统安装Linux之后找不到Win
·Linux操作系统上安装Flash Med
·设置Tomcat在Linux操作系统中自
·Linux操作系统下创建DBCA报错的
·Redhat Enterprise Linux磁带机
·Linux系统下用一条命令批量修改
·实用技巧:Linux下命令排列、调
·关于Linux操作系统中VI编辑器字
·Linux VI编辑器中的复制与粘贴
·Linux系统下触摸屏的简单使用和
Google
 
Ubuntu下面的C语言代码检查工具 Splint
[ 作者:  加入时间:2008-01-24 10:10:09  来自:Linux联盟收集整理 ]
 

看一下下面的代码(当然包括错误,以检验splint的功能):

#include 

int main(int argc,char* argv[]){
int a=100; /*没有使用的变量*/
int b[8];
printf("Hello c\n");
b[9]=100; /*明显数组越界 */

/* 用到了两个为声明的变量c和d/
c=100;
d=10;
return 0;
}

现在可以用splint来检查一下,为了检验是否可以检测到数组越界,使用+bounds选项。

splint hi.c +bounds

输出结果:

hi.c: (in function main)
hi.c:9:2: Unrecognized identifier: c
Identifier used in code has not been declared. (Use -unrecog to inhibit
warning)
hi.c:10:2: Unrecognized identifier: d
hi.c:4:6: Variable a declared but not used
A variable is declared but never used. Use /*@unused@*/ in front of
declaration to suppress message. (Use -varuse to inhibit warning)
hi.c:7:2: Likely out-of-bounds store:
b[9]
Unable to resolve constraint:
requires 7 >= 9
needed to satisfy precondition:
requires maxSet(b @ hi.c:7:2) >= 9
A memory write may write to an address beyond the allocated buffer. (Use
-likely-boundswrite to inhibit warning)
hi.c:3:14: Parameter argc not used
A function parameter is not used in the body of the function. If the argument
is needed for type compatibility or future plans, use /*@unused@*/ in the
argument declaration. (Use -paramuse to inhibit warning)
hi.c:3:25: Parameter argv not used

Finished checking --- 6 code warnings

现在详细看一下结果:

检查结果1:

hi.c:9:2: Unrecognized identifier: c
Identifier used in code has not been declared. (Use -unrecog to inhibit
warning)
hi.c:10:2: Unrecognized identifier: d
hi.c:4:6: Variable a declared but not used
A variable is declared but never used. Use /*@unused@*/ in front of
declaration to suppress message. (Use -varuse to inhibit warning)

这些应该是splint检测到变量c和d没有声明。

检查结果2:

hi.c:7:2: Likely out-of-bounds store:
b[9]
Unable to resolve constraint:
requires 7 >= 9
needed to satisfy precondition:
requires maxSet(b @ hi.c:7:2) >= 9
A memory write may write to an address beyond the allocated buffer. (Use
-likely-boundswrite to inhibit warning)

这些是检查存在数组越界,因为吧b[8]的最大数组序号应该是7,而不是9,所以出现requires 7 >= 9;

检查结果3:

hi.c:3:14: Parameter argc not used
A function parameter is not used in the body of the function. If the argument
is needed for type compatibility or future plans, use /*@unused@*/ in the
argument declaration. (Use -paramuse to inhibit warning)
hi.c:3:25: Parameter argv not used

这些表明argc和argv变量声明了,但是没有使用。这个不是什么问题。

如果小心使用splint,应该对于c语言的程序编写有非常大的辅助作用!

Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·完美解决SATA硬盘Fedora(或Ubuntu关机异响)  (2008-01-21 11:36:08)
 ·解决ubuntu7.10不能正常播放网页上flash视频的办法  (2008-01-21 10:30:10)
 ·Ubuntu Linux系统的目录结构  (2008-01-21 10:20:05)
 ·Ubuntu配置Java+Eclipse+MyEclipse环境  (2008-01-17 11:51:35)
 ·Ubuntu Linux系统下自启动选项设置方法  (2008-01-17 11:50:00)
 ·Ubuntu Linux系统的启动过程  (2008-01-17 11:49:27)
 ·Ubuntu Linux下笔记本禁用触摸板的方法  (2008-01-17 11:48:55)
 ·Ubuntu 7.10 的扩展安装  (2008-01-16 10:46:17)
 ·Dell Optiplex 320 Ubuntu Server 7.10 安装手册  (2008-01-16 10:25:54)
 ·Ubuntu下用手机连接蓝牙局域网pan/pand  (2008-01-11 11:49:15)