免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10462 | 回复: 55
打印 上一主题 下一主题

程序中有这样的隐患,怎么查出来。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-19 20:09 |只看该作者 |倒序浏览
#include<stdio.h>
int main()
{
    unsigned char a;
    unsigned short b=500;
    for(a=0;a<b;a++)
        printf("hello\n");
}
显然这是一个死循环的编码,怎么让编译器或者其他工具查出这样的错误呢。

论坛徽章:
0
2 [报告]
发表于 2006-09-19 20:10 |只看该作者
你会写编译器么

论坛徽章:
0
3 [报告]
发表于 2006-09-19 20:20 |只看该作者
原帖由 langue 于 2006-9-19 20:10 发表
你会写编译器么

什么意思哦。怎么感觉怪怪的味道?

论坛徽章:
0
4 [报告]
发表于 2006-09-19 20:25 |只看该作者
有一个叫做lint的静态检查工具,就是干这个的。

不知道你用的什么发行版,如果是debian的话。aptitude install splint.如果是其它发行版,应当有类似的工具。

论坛徽章:
0
5 [报告]
发表于 2006-09-19 20:35 |只看该作者
原帖由 coldwarm 于 2006-9-19 20:25 发表
有一个叫做lint的静态检查工具,就是干这个的。

不知道你用的什么发行版,如果是debian的话。aptitude install splint.如果是其它发行版,应当有类似的工具。

但是pclint需要配置各种规则的。不知道针对以上错误,规则应该如果配。

论坛徽章:
0
6 [报告]
发表于 2006-09-19 20:56 |只看该作者
原帖由 mingjwan 于 2006-9-19 20:09 发表
#include<stdio.h>
int main()
{
    unsigned char a;
    unsigned short b=500;
    for(a=0;a<b;a++)
        printf("hello\n";
}
显然这是一个死循环的编码,怎么让编译器或者 ...


死循环?

论坛徽章:
0
7 [报告]
发表于 2006-09-19 21:04 |只看该作者
我也没看出来是死循环....

论坛徽章:
0
8 [报告]
发表于 2006-09-19 21:04 |只看该作者
没用过pclint,但每个lint工具都应当带一些默认规则。除非你要定制自己的lint规则库,不过我觉得默认规则应当够用了。

  1. Splint 3.1.1 --- 21 Apr 2006

  2. type.c: (in function main)
  3. type.c:6:6: Assignment of int to unsigned char: a = 0
  4.   Types are incompatible. (Use -type to inhibit warning)
  5. type.c:6:10: Operands of < have incompatible types (unsigned char, unsigned
  6.                 short int): a < b
  7.   To make char and int types equivalent, use +charint.
  8. type.c:6:14: Operand of ++ is non-numeric (unsigned char): a
  9. type.c:6:10: Incompatible types for < (unsigned char, unsigned short int) (in
  10.                 post loop test): a < b
  11. type.c:8:2: Path with no return in function declared to return int
  12.   There is a path through a function declared to return a value on which there
  13.   is no return statement. This means the execution may fall through without
  14.   returning a meaningful result to the caller. (Use -noret to inhibit warning)

  15. Finished checking --- 5 code warnings
复制代码


你的代码的问题应当已经暴露出来了。虽然这样错误还是不明显。

论坛徽章:
0
9 [报告]
发表于 2006-09-19 21:25 |只看该作者
lint好像是BSD上面的?

bash-2.05# lint
lint [-abceghprvxzHF] [-s|-t] [-i|-nu] [-Dname[=def]] [-Uname]
     [-Idirectory] [-Ldirectory] [-llibrary] [-ooutputfile] file ...

lint [-abceghprvzHF] [-s|-t] -Clibrary [-Dname[=def]]
     [-Idirectory] [-Uname] file ...
bash-2.05#

论坛徽章:
0
10 [报告]
发表于 2006-09-19 21:28 |只看该作者
原帖由 converse 于 2006-9-19 20:56 发表


死循环?



char比short小,何年何月才会加至500?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP