testlint.c: (in function foo2)testlint.c:6:7: Variable p declared but not usedA variable is declared but never used. Use /*@unused@*/in front ofdeclaration to suppress message. (Use -varuse to inhibit warning)
bool.c:4:5: Test expression forifis assignment expression: i = 3
The condition 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 inhibit warning)
// 错误原因: if语句中的条件表达式是一个赋值语句。
bool.c:4:5: Test expression forif not boolean, type int: i = 3
Test expression type is not boolean or int. (Use -predboolint to inhibit
warning)
// 错误原因: if语句中的条件表达式的返回值,不是布尔型,而是整型。
bool.c:4:8: Return value type bool does not match declared type int: b1
Types are incompatible. (Use -type to inhibit warning)
// 错误原因: 返回值是布尔型,而不是整型。
bool.c:5:6: Operand of ! is non-boolean (int): !i
The operand of a boolean operatoris not a boolean. Use +ptrnegate to allow !
to be used on pointers. (Use -boolops to inhibit warning)
// 错误原因: "!"操作符的操作数不是布尔型,而是整型i.
bool.c:5:11: Right operand of || is non-boolean (char *): !i || s
// 错误原因: "||"操作符的右操作数不是布尔型,而是字符指针.
bool.c:7:5: Use of == with boolean variables (risks inconsistency because of
multiple true values): b1 == b2
Two bool values are compared directly using a C primitive. This may produce
unexpected results since all non-zero values are considered true, so
different true values may not be equal. The file bool.h (included in
splint/lib) provides bool_equal for safe bool comparisons. (Use -boolcompare
to inhibit warning)
// 错误原因: 使用"=="对两个布尔型进行比较.应该使用"&&".
Finished checking --- 6 code warnings
示例2:
//malloc1.c
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *some_mem;
int size1=1048576;
some_mem=(char *)malloc(size1);
printf("Malloed 1M Memory!/n");
free(some_mem);
exit(EXIT_SUCCESS);
}
//END
使用splint检查malloc1.c
$ splint malloc1.c
Splint 3.1.1 --- 28 Apr 2005
malloc1.c: (in function main)
malloc1.c:9:25: Function malloc expects arg 1 to be size_t gets int: size1
To allow arbitrary integral types to match any integral type, use
#include<stdio.h>#include<stdlib.h>int main(int argc, char **argv){printf("Hello,Splint/n");return 0;}这段代码大家肯定很熟悉吧,下面用Splint来检测.下面是检测结果:Splint 3.0.1.6 --- 11 Feb 2002hello.c: (in function main)hello.c(4,14): Parameter argc not usedA 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)hello.c(4,27): Parameter argv not usedFinished checking --- 2 code warnings 从输出结果中可以看出,有两个警告,提示是变量argc和argv没有使用,并且提示可以加入/*@unused@*/忽略这个警告。更改后的代码:#include<stdio.h>#include<stdlib.h>int main(/*@unused@*/int argc, /*@unused@*/char **argv){printf("Hello,Splint/n");return 0;}然后我们再来检测:Splint 3.0.1.6 --- 11 Feb 2002Finished checking --- no warnings提示没有警告,这样也就是lint clean.
2、看一下下面的代码(当然包括错误,以检验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)
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变量声明了,但是没有使用。这个可以加入/*@unused@*/忽略这个警告。 小心使用splint,应该对于c语言的程序编写有非常大的辅助作用!