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; } |