网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > linux > Linux编程 > C++编程 > Ubuntu下面的C语言代码检查工具 Splint  

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

2008-01-24  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

  看一下下面的代码(当然包括错误,以检验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;
} 网管论坛bbs_bitsCN_com 


 
  现在可以用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 网管联盟bitsCN@com 
网管网www_bitscn_com

 
  现在详细看一下结果:
 
  检查结果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) 网管u家u.bitsCN.com 

网管下载dl.bitscn.com


  这些应该是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) 
网管论坛bbs_bitsCN_com
网管联盟bitsCN_com

 
  这些是检查存在数组越界,因为吧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 

网管网www.bitscn.com


 
  这些表明argc和argv变量声明了,但是没有使用。这个不是什么问题。
 
  如果小心使用splint,应该对于c语言的程序编写有非常大的辅助作用!
  网管联盟bitsCN@com


TAGs   工具   检查   代码   面的   语言   to   the   not   Use   used   in   warning      
 上一篇:Ubuntu配置Java+Eclipse+MyEclipse环境   下一篇:Java应用:Fedora Linux 8系统下配置JDK
Ubuntu下面的C语言代码检查工具 Splint 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: