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

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

今天发现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. ffmpeg-20160926[27]-bin.7z

    ffplay 2016.09.26 开始使用 SDL 2.x , CPU 利用率比 SDL 1.x 略微好一些. ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 ...

  2. c#接口

    //接口中方法 属性 事件等默认都是public 不允许用修饰符修饰 public interface IEventInterFace { string this[int index] { get; ...

  3. Python网络编程之线程,进程

    一. 线程: 基本使用 线程锁 线程池 队列(生产者消费者模型) 二. 进程:  基本使用  进程锁 进程池 进程数据共享 三. 协程: gevent greenlet 四. 缓存: memcache ...

  4. Android杂记:genymotion与eclipse报错问题

    用eclipse启动genymotion时有时候会报 The connection to adb is down, and a severe error has occured. You must r ...

  5. 结合stack数据结构,实现不同进制转换的算法

    #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...

  6. Power BI for Office 365(一)移动端应用

    此篇来自于微软商业智能网站的官方博客团队发布的Power BI在线资料其中的一部分,完整版地址: http://office.microsoft.com/en-us/office365-sharepo ...

  7. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  8. 转-临界区对象TCriticalSection与TRTLCriticalSection的区别

    TRTLCriticalSection是一个结构体,在windows单元中定义: 是InitializeCriticalSection, EnterCriticalSection, LeaveCrit ...

  9. ACM: FZU 2102 Solve equation - 手速题

     FZU 2102   Solve equation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  10. CSS生僻问题一网打尽

    CSS生僻问题一网打尽 伪类和伪元素 伪类 何为伪类? 像类不是类,不是自己声明的类(不写样式也存在). 对伪元素的认识在早期网页上的超链接.链接(锚啊)用下划线标出来,点击后链接变紫色,鼠标悬上去变 ...