[skill] strncpy里边有两个坑
以前的笔记,今日翻出了复看了一下,转过来。
------------------------------------
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 ""
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里边有两个坑的更多相关文章
- 两个坑-Linux下Network-Manager有线未托管-DNS resolv.conf文件开机被清空
Linux里面有两套管理网络连接的方案: 1./etc/network/interfaces(/etc/init.d/networking) 2.Network-Manager 两套方案是冲突的,不能 ...
- iscroll遇到的两个坑
最近移动端闪付遇到的两个坑做下总结: 1.使用iscroll后,滑动并没有生效 解决方案: 首先要查看:结构是否正确: <div id="wrapper"> //w ...
- 记自己在spring中使用redis遇到的两个坑
本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 <!-- 加载Properties文件 --> < ...
- MySql 5.7安装(随机密码,修改默认密码)两个坑
MySql 5.7安装(随机密码,修改默认密 下载了MySql 最新版本,安装的过程中,发现了很多新特性 1.data目录不见了 在进行my-default.ini配置的时候 (需要配置 # base ...
- jQueryUI Draggable 和 Droppable 配合使用时遇到的两个坑
jQueryUI 的 拖拽插件极大的方便了开发者对拖拽功能的实现,但是官方教程给的太笼统,在具体实现的时候很多地方不明确,这里说一下我遇到的两个 "小坑": 1:Draggable ...
- redis主从遇到的两个坑
最近在使用redis主从的时候做了下面两件事情: 1 希望redis主从从操作上分析,所有写操作都在master上写,所有读操作都在从上读. 2 由于redis的从是放在本地的,所以有的key的读写操 ...
- 记一次uboot升级过程的两个坑
背景 之前做过一次uboot的升级,当时留下了一些记录,本文摘录其中比较有意思的两个问题. 启动失败问题 问题简述 uboot代码中用到了一个库,考虑到库本身跟uboot版本没什么关系,就直接把旧的库 ...
- Hive改表结构的两个坑|避坑指南
Hive在大数据中可能是数据工程师使用的最多的组件,常见的数据仓库一般都是基于Hive搭建的,在使用Hive时候,遇到了两个奇怪的现象,今天给大家聊一下,以后遇到此类问题知道如何避坑! 坑一:改变字段 ...
- 使用NSTimer过程中最大的两个坑
坑1. retain cycle问题. 在一个对象中使用循环执行的nstimer时,若希望在对象的dealloc方法中释放这个nstimer,结局会让你很失望. 这个timer会导致你的对象根本不会被 ...
随机推荐
- oracle常用脚本
自动启动oracle sqlplus -S sys/123456 as sysdba <<EOF startup quit EOF lsnrctl start lsnrctl reload
- 【GoLang】golang 最佳实践汇总
最佳实践 1 包管理 1.1 使用包管理对Golang项目进行管理,如:godep/vendor等工具 1.2 main/init函数使用,init函数参考python 1.2.1 main-> ...
- aes加密C语言
/** * \file aes.h * * \brief AES block cipher * * Copyright (C) 2006-2010, Brainspark B.V. * * This ...
- iOS Waxpatch项目(动态更新)
我的iOS Waxpatch项目地址https://github.com/piaojin/iOS-WaxPatch
- Delphi多线程编程--线程同步的方法(事件、互斥、信号、计时器)简介
更详细的可以参考:http://www.cnblogs.com/xumenger/p/4450659.html 或者参考之后的博客 四个系统内核对象(事件.互斥.信号.计时器)都是线程同步的手段,从这 ...
- PIL中分离通道发生“AttributeError: 'NoneType' object has no attribute 'bands'”
解决方法: 这个貌似是属于一个bug 把Image.py中的1500行左右的split函数改成如下即可: def split(self): "Split image into bands&q ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 宿主机ping不通虚拟机cenos7
参考网址1:http://zhidao.baidu.com/link?url=2v3NXGyzPT-XTYwon8PesZLnMg02Ako6nDub3vJiJt4miSmkOA-04xLUqfu9s ...
- oracle存储过程截取字符串
declare CURSOR l_c IS select classid from cjt_class; Begin FOR i IN l_c LOOP update cjt_class t set ...
- Java日期处理
日常工作中经常遇到关于日期的处理,下面把自个写好的Java代码段分享一下,也当做自个的一个备份,同时也欢迎交流,如若分享请注明出处,谢谢. 1.返回两个时间段之间的月份: /** * 返回任意两个月份 ...