[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   Lifen, Song  阅读(284)  评论(0)    收藏  举报

编辑推荐:
· MySQL索引完全指南:让你的查询速度飞起来
· 一个字符串替换引发的性能血案:正则回溯与救赎之路
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
· 一个自认为理想主义者的程序员,写了5年公众号、博客的初衷
阅读排行:
· 我用这13个工具,让开发效率提升了5倍!
· Coze工作流实战:一键生成鸡汤视频——厉害的人,早已戒掉情绪
· 商品中心—15.库存分桶扣减的技术文档
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
· C++服务开发环境-万事开头难
点击右上角即可分享
微信分享提示