#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)
这些是检查存在数组越界,因为吧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
这些表明argc和argv变量声明了,但是没有使用。这个不是什么问题。
如果小心使用splint,应该对于c语言的程序编写有非常大的辅助作用!
本文来自 280文秘网(https://www.it280.com),转载请保留网址和出处
【Ubuntu系统下C语言代码检查工具(Splint)】相关文章:
给哥哥的新年祝福语精编02-07
海军招收飞行学员工作简介02-07
小学一年级英语课件:Unit 5 Feeling单元知识重点02-07
2016年多人合伙协议书范本下载02-07
2021年贵州遵义中考成绩查询、查分入口已开通【附普通高中最低控制线】02-07
小学语文学生的期末评语02-07
13年河南职称日语考试报名条件02-07
杭州市教育局出台2016年浙江杭州中考招生工作通知02-07
2021安徽蚌埠固镇县面向社区工作者招聘事业单位人员笔试成绩公告02-07
关于国庆节英语的简介02-07
