splint命令:splint typeerr.c
splint执行的结果:
Splint 3.1.2--- 03 May 2009
typeerr.c:(in function foo)
typeerr.c:3:5:Test expression for if is assignment expression: i = 3
Thecondition test is an assignment expression. Probably, you mean to use==
instead of=. If an assignment is intended, add an extra parentheses nesting
(e.g., if((a = b)) ...) to suppress this message. (Use -predassign to
inhibitwarning)
#错误类型:if语句中的条件表达式是一个赋值语句。
typeerr.c:3:5:Test expression for if not boolean, type int: i = 3
Testexpression type is not boolean or int. (Use -predboolint to inhibit
warning)
#错误类型:if语句中的条件表达式返回值不是bool类型而是int类型
typeerr.c:3:17:Return value type bool does not match declared type int: b1
Types areincompatible. (Use -type to inhibit warning)
#错误类型:!的操作数不是bool类型而是int类型的i
typeerr.c:4:6:Operand of ! is non-boolean (int): !i
Theoperand of a boolean operator is not a boolean. Use +ptrnegate toallow !
to be usedon pointers. (Use -boolops to inhibit warning)
#错误类型:||操作符的右操作数不是bool类型而是整型
typeerr.c:4:11:Right operand of || is non-boolean (char *): !i || s
##错误类型:不应该使用==对两个bool类型进行比较,而应该使用&&
typeerr.c:6:5:Use of == with boolean variables (risks inconsistency because of
multipletrue values): b1 == b2
Two boolvalues are compared directly using a C primitive. This may produce
unexpectedresults since all non-zero values are considered true, so
differenttrue values may not be equal. The file bool.h (included in
splint/lib)provides bool_equal for safe bool comparisons. (Use -boolcompare
to inhibitwarning)
Finishedchecking --- 6 code warnings
实例2.malloc1.c
/*Program: malloc1.c -- check varible type */ #include<stdlib.h> #include<stdio.h> int main(){ char * some_mem; int size1 = 1048567; //size_t size1 = 1048567; some_mem = (char*) malloc(size1); printf("Malloced 1M Memory!\n"); free(some_mem); exit(EXIT_SUCCESS); } |
splint命令:splint malloc1.c
使用是splint检查malloc1.c
Splint 3.1.2--- 03 May 2009
malloc1.c:(in function main)
malloc1.c:8:28:Function malloc expects arg 1 to be size_t gets int: size1
To allowarbitrary integral types to match any integral type, use
+matchanyintegral.
将size1的定义修改为:
size_t size1= 1048567;
再次使用splint将行检查:splintmalloc1.c
Splint 3.1.2--- 03 May 2009
Finishedchecking --- no warnings