以前的笔记,今日翻出了复看了一下,转过来。

------------------------------------

今天发现xxxdump中使用xxx_strncpy 替换 strncpy导致的bug。
 
原因是strncpy:
 
Warning: If there is no  null byte among the first n bytes of src, the string placed in dest will not be null terminated.
 
但是xxx_strncpy会保证dest是0结尾。
 
典型的就是xxxdumpd中的xxx_info.c中的使用,原来使用strncpy没问题,但是替换了之后会导致str截断。
 
 
我们需要了解这个差异,避免以后的问题。
 
原则上,不再推荐使用原生的strncpy。
 
 
 
 
***security name***
2014-10-11
 
From: Cao Tong
Sent: Saturday, October 11, 2014 4:09 PM
Subject: Re: xxx_strncpy 推荐
 
r = xxx_strncpy(dst, src, n) 与 r =
strncpy(dst, src, n) 比较:

一: n>len(src)时,拷贝len(src)个字符后,strncpy
会将 len(src) 到 n 部分全部置零,xxx_strncpy只给 len(scr)+ 1 置零。
二:
n<=len(src)时,strncpy 只拷贝 n 个字符, xxx_strncpy拷贝n-1个,把第n个置零。
三:
返回值不同,见下边的例子。

char dst[10] =
"123456789";
0x7fffffffe3e0:    0x31   
0x32    0x33    0x34   
0x35    0x36    0x37   
0x38
0x7fffffffe3e8:    0x39    0x00
char*
src = "abcde";

1.  n = 8;

strncpy:
0x7fffffffe3e0:    0x61    0x62   
0x63    0x64    0x65   
0x00    0x00   
0x00
0x7fffffffe3e8:    0x39   
0x00
r = 0x7fffffffe3e0
"abcde"

xxx_strncpy:
0x7fffffffe3e0:    0x61    0x62   
0x63    0x64    0x65   
0x00    0x37   
0x38
0x7fffffffe3e8:    0x39    0x00
r =0x7fffffffe3e5 ""

2.  n = 2;

strncpy:
0x7fffffffe3e0:    0x61    0x62   
0x33    0x34    0x35   
0x36    0x37   
0x38
0x7fffffffe3e8:    0x39    0x00
r = 0x7fffffffe3e0
"ab3456789"

xxx_strncpy:
0x7fffffffe3e0:    0x61    0x00   
0x33    0x34    0x35   
0x36    0x37   
0x38
0x7fffffffe3e8:    0x39    0x00
r =  = 0x7fffffffe3e1 ""

-------------------
Cao Tong

-------- Original Message --------
Date:
2014/3/11 12:57

推荐使用xxx_strncpy 替代 strncpy
 
说明:
 
/*
* Apache's "replacement" for the strncpy()
function. We roll our
* own to implement these specific
changes:
*   (1) strncpy() doesn't always null terminate and we
want it to.
*   (2) strncpy() null fills, which is bogus, esp.
when copy 8byte
*       strings into 8k
blocks.
*   (3) Instead of returning the pointer to the beginning
of
*       the destination string, we return
a pointer to the
*       terminating '\0' to
allow us to "check" for truncation
*
* apr_cpystrn() follows the same
call structure as strncpy().
*/
 
char* xxx_strncpy(char *dst, const char *src,
size_t dst_size)
{
 
 
 
***security name***
2014-3-11

[skill] strncpy里边有两个坑的更多相关文章

  1. 两个坑-Linux下Network-Manager有线未托管-DNS resolv.conf文件开机被清空

    Linux里面有两套管理网络连接的方案: 1./etc/network/interfaces(/etc/init.d/networking) 2.Network-Manager 两套方案是冲突的,不能 ...

  2. iscroll遇到的两个坑

    最近移动端闪付遇到的两个坑做下总结: 1.使用iscroll后,滑动并没有生效 解决方案: 首先要查看:结构是否正确: <div id="wrapper">   //w ...

  3. 记自己在spring中使用redis遇到的两个坑

    本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 <!-- 加载Properties文件 --> < ...

  4. MySql 5.7安装(随机密码,修改默认密码)两个坑

    MySql 5.7安装(随机密码,修改默认密 下载了MySql 最新版本,安装的过程中,发现了很多新特性 1.data目录不见了 在进行my-default.ini配置的时候 (需要配置 # base ...

  5. jQueryUI Draggable 和 Droppable 配合使用时遇到的两个坑

    jQueryUI 的 拖拽插件极大的方便了开发者对拖拽功能的实现,但是官方教程给的太笼统,在具体实现的时候很多地方不明确,这里说一下我遇到的两个 "小坑": 1:Draggable ...

  6. redis主从遇到的两个坑

    最近在使用redis主从的时候做了下面两件事情: 1 希望redis主从从操作上分析,所有写操作都在master上写,所有读操作都在从上读. 2 由于redis的从是放在本地的,所以有的key的读写操 ...

  7. 记一次uboot升级过程的两个坑

    背景 之前做过一次uboot的升级,当时留下了一些记录,本文摘录其中比较有意思的两个问题. 启动失败问题 问题简述 uboot代码中用到了一个库,考虑到库本身跟uboot版本没什么关系,就直接把旧的库 ...

  8. Hive改表结构的两个坑|避坑指南

    Hive在大数据中可能是数据工程师使用的最多的组件,常见的数据仓库一般都是基于Hive搭建的,在使用Hive时候,遇到了两个奇怪的现象,今天给大家聊一下,以后遇到此类问题知道如何避坑! 坑一:改变字段 ...

  9. 使用NSTimer过程中最大的两个坑

    坑1. retain cycle问题. 在一个对象中使用循环执行的nstimer时,若希望在对象的dealloc方法中释放这个nstimer,结局会让你很失望. 这个timer会导致你的对象根本不会被 ...

随机推荐

  1. artdialog

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  2. Json---使用Jsoncpp解析与写入

    上述Json解析使用的是Jsoncpp,要使用Jsoncpp,得做如下几步的配置: 1.首先从http://sourceforge.net/projects/jsoncpp/下载,压缩包大约105k. ...

  3. Tensorflow二分类处理dense或者sparse(文本分类)的输入数据

    这里做了一些小的修改,感谢谷歌rd的帮助,使得能够统一处理dense的数据,或者类似文本分类这样sparse的输入数据.后续会做进一步学习优化,比如如何多线程处理. 具体如何处理sparse 主要是使 ...

  4. intellij idea 插件 ideaVim 用法

    intellij idea 插件 ideaVim - Genji_ - 博客园http://www.cnblogs.com/nova-/p/3535636.html IdeaVim插件使用技巧 - - ...

  5. [linux] grep awk sort uniq学习

    grep的-A-B-选项详解grep能找出带有关键字的行,但是工作中有时需要找出该行前后的行,下面是解释1. grep -A1 keyword filename找出filename中带有keyword ...

  6. ListView没有分割线怎么办?

    <ListView android:layout_width="match_parent" android:layout_height="match_parent& ...

  7. AngularJS动画

    1.AngularJS提供了动画效果,可以配合CSS使用: 2.AngularJS使用动画需要引入angular-animate.min.js库 <script src="http:/ ...

  8. OSS层基础:平台区分

    #define PLATFORM_WINDOWS 1 #define PLATFORM_MAC 2 #define PLATFORM_UNIX 3 #if defined(_WIN32) #defin ...

  9. android--访问网络权限

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

  10. 转载:持续集成之解决jenkins内存溢出问题

    在jenkins master-slave配置中,总是出现内存溢出问题,更换了机器设备仍然跑不起来: 问题如下: Status Code: 500    Exception: org.apache.c ...