软件测试实验学习笔记系列2 -- lint,splint的使用

发表于:2013-8-05 11:05

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:xiajian2010    来源:51Testing软件测试网博客

  splint命令: splint null.c

  结果输出:

Splint 3.1.2--- 03 May 2009
Spec filenot found: null.lcl
null.c:2:65:Comment starts inside comment
A commentopen sequence (/*) appears within a comment. This usually means an
earliercomment was not closed. (Use -nestcomment to inhibit warning)
null.c: (infunction firstChar1)
null.c:4:10:Dereference of possibly null pointer s: *s
A possiblynull pointer is dereferenced. Value is either the result of a
functionwhich may return null (in which case, code should check it is not
null), ora global, parameter or structure field declared with the null
qualifier.(Use -nullderef to inhibit warning)
null.c:3:33:Storage s may become null
Finishedchecking --- 2 code warnings

  函数firstChar1和firstChar2都使用了null的说明,表示指针s可能是一个空指针,所以splint会对s的值使用情况进行检查,由于firstChar2中对s的值进行了判断,所以没有对firstChar2函数中s输出警告信息。

  未定义变量错误:C中使用没有定义变量会出错,/*@in@*/说明的变量表示必须先进行定义./*@out*@/表明在执行过函数后,这个变量就进行了定义。--个人感觉/*@out*@/类似与C#中的out关键字。

  实例1:usedef.c

/*Program:usedef.c -- use splint check the varible undefined error or warnings */
//out represent varible *x will be defined after execution of function
extern void setVal(/*@out@*/int *x);
//in represent varible *x has been defined before the execution.
extern int getVal(/*@in@*/ int *x);
extern int mysteryVal(int *x);
int dumbfunc(/*@out@*/int *x,int i){
 if(i>3) return *x;
 else if(i>1)
  return getVal(x);
 else if(i==0)
  return mysteryVal(x);
 else{
  setVal(x);
  return *x;
 }
}

  splint命令:splint usedef.c

  splint执行的结果:

Splint 3.1.2--- 03 May 2009
usedef.c:(in function dumbfunc)
usedef.c:8:17:Value *x used before definition
#在一些执行路径中一个右值的被使用的时候可能没被初始化,
An rvalueis used that may not be initialized to a value on some executionpath. (Use-usedef to inhibit warning)
usedef.c:10:17:Passed storage x not completely defined (*x is undefined):
getVal(x)
Storagederivable from a parameter, return value or global is not defined.
Use/*@out@*/ to denote passed or returned storage which need not bedefined.
(Use-compdef to inhibit warning)
usedef.c:12:21:Passed storage x not completely defined (*x is undefined):
mysteryVal(x)
Finishedchecking --- 3 code warnings

  错误原因:由于程序中没有对X定义,所以报出未定义的错误.但是由于setVal()使用了/*@out*@/说明,所以在语句“setVal(x)”和“returnx”中,没有报未定义错误。

  类型错误:C语言中的变量类型比较多,彼此之间有些细微的差别,splint可以对变量的类型进行检查:

  实例1.typeerr.c

/*Program: typeerr.c -- use splint to check type varible error */
int foo(int i,char *s,bool b1,bool b2){
 if(i=3) return b1;
 if(!i || s) return i;
 if(s) return 7;
 if(b1 == b2)
  return 3;
 return 2;
}

42/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号