赛迪网 > IT技术 开源空间 > 文章
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

Ubuntu下面的C语言代码检查工具 Splint

发布时间:2008.01.24 06:21     来源:赛迪网    作者:skid

看一下下面的代码(当然包括错误,以检验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语言的程序编写有非常大的辅助作用!

(责任编辑:云子)


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· 新闻观察 第四届红帽高峰会正式开始征稿 (01-23) · 新闻:Apache三大分支最新版本同时发布 (01-23)
· 新手学堂:在Linux系统下mail命令的测试 (01-23) · 如何来清除Linux操作系统命令的历史记录 (01-23)
· Linux和Windows最大可存储的单文件容量 (01-23) · 红帽很强大 新任CEO坚称企业不会被收购 (01-23)
· 最新版Ubuntu Linux 6.06.2 LTS现已发布 (01-23) · 虚拟机Linux系统下连接ARM开发板的方法 (01-22)
· 用NFS服务开发ARM Linux程序的方法介绍 (01-22) · 新手学堂:开启和关闭Shell特性的小技巧 (01-22)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
· 工信部“三定”公布 总编制731名设24司局
· 北京发电子商务监管意见 营利性网店须办照
· 直播 08中国城市信息化高峰论坛 案例点评
· 烽火网络校园解决方案 移民安置信息管理系统
  IT博客 ·曾剑秋·项立刚·Java学习·网管
  IT技术论坛 ·开发·网管·安全·数据库·系统