[CopyPaste] We DON'T copy-paste as a professional programmer
Yesterday I used almost half day to debug a strange problem: When we set a specific timeout for watchdog timeout via a uart port in our application, the checking of the ack from the other end will not pass. But our HW engineer has a tiny tool which can do that too will be OK.
Later I found the decoding is wrong with pppDecode ( which was copied from a blog by someone, http://blog.csdn.net/wangxg_7520/article/details/2488491) .
Let's have a look at the function:
int pppDecode(unsigned char * buf, int len) { unsigned char * pi, *po; int i, olen; unsigned char obuf[BUF_LEN]; if(len > BUF_LEN) { return -1; } memset(obuf, 0, BUF_LEN); pi = buf; po = obuf; olen = len; for(i=0; i<len; i++) { if(*pi == PPP_FRAME_ESC) { /* skip the escape byte */ pi++; olen--; /* xor the 6th bit */ *po = *pi ^ PPP_FRAME_ENC; } else { *po = *pi; } pi++; po++; }
The pi may be added twice in one loop. While the loop control is the i variable. Thus pi may exceed the buf area and the behavior depends on the data after buf...
Commented to the blog author, hope he can update soon.
Final words, as a professional programmer, never copy-paste code directly from internent without understanding it!
posted on 2012-06-30 01:54 Lifen, Song 阅读(284) 评论(0) 收藏 举报
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· MySQL索引完全指南:让你的查询速度飞起来
· 一个字符串替换引发的性能血案:正则回溯与救赎之路
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
· 一个自认为理想主义者的程序员,写了5年公众号、博客的初衷
· 我用这13个工具,让开发效率提升了5倍!
· Coze工作流实战:一键生成鸡汤视频——厉害的人,早已戒掉情绪
· 商品中心—15.库存分桶扣减的技术文档
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
· C++服务开发环境-万事开头难