[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 阅读(279) 评论(0) 编辑 收藏 举报