strncpy, strncpy_s
Defined in header
<string.h> |
||
(1) | ||
char *strncpy( char *dest, const char *src, size_t count );
|
(until C99) | |
char *strncpy( char *restrict dest, const char *restrict src, size_t count );
|
(since C99) | |
errno_t strncpy_s(char *restrict dest, rsize_t destsz, const char *restrict src, rsize_t count); |
(2) | (since C11) |
count
characters of the character array pointed to by src
(including the terminating null character, but not any of the characters that follow the null character) to character array pointed to by dest
.count
is reached before the entire array src
was copied, the resulting character array is not null-terminated.src
, count
is not reached, additional null characters are written to dest
until the total of count
characters have been written.dest
or src
is not a pointer to a character array (including if dest
or src
is a null pointer), if the size of the array pointed to by dest
is less than count
, or if the size of the array pointed to by src
is less than count
and it does not contain a null character.count
, it stops after writing the terminating null character (if there was no null in the source, it writes one at dest[count] and then stops). Also, the following errors are detected at runtime and call the currently installed constraint handler function:
-
src
ordest
is a null pointerdestsz
orcount
is zero or greater than RSIZE_MAXcount
is greater or equaldestsz
, butdestsz
is less or equal strnlen_s(src, count), in other words, truncation would occur- overlap would occur between the source and the destination strings
dest
< strnlen_s(src, destsz) <= destsz
; in other words, an erroneous value of destsz
does not expose the impending buffer overflow. The behavior is undefined if the size of the character array pointed to by src
< strnlen_s(src, count) < destsz
; in other words, an erroneous value of count
does not expose the impending buffer overflow.
- As with all bounds-checked functions,
strncpy_s
is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before includingstring.h
.
Parameters
dest | - | pointer to the character array to copy to |
src | - | pointer to the character array to copy from |
count | - | maximum number of characters to copy |
destsz | - | the size of the destination buffer |
Return value
dest
dest
is a null pointer or destsz
is zero or greater than RSIZE_MAX) and may clobber the rest of the destination array with unspecified values.Notes
As corrected by the post-C11 DR 468, strncpy_s
, unlike strcpy_s, is only allowed to clobber the remainder of the destination array if an error occurs.
Unlike strncpy
, strncpy_s
does not pad the destination array with zeroes, This is a common source of errors when converting existing code to the bounds-checked version.
Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation for strncpy_s
, it is possible to get the truncating behavior by specifying count
equal to the size of the destination array minus one: it will copy the first count
bytes and append the null terminator as always: strncpy_s(dst, sizeof dst, src, (sizeof dst)-1);
Example
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char src[] = "hi";
char dest[6] = "abcdef"; // no null terminator
strncpy(dest, src, 5); // writes five characters 'h', 'i', '\0', '\0', '\0' to dest
printf("strncpy(dest, src, 5) to a 6-byte dest gives : ");
for(size_t n = 0; n < sizeof dest; ++n) {
char c = dest[n];
c ? printf("'%c' ", c) : printf("'\\0' ");
}
printf("\nstrncpy(dest2, src, 2) to a 2-byte dst gives : ");
char dest2[2];
strncpy(dest2, src, 2); // truncation: writes two characters 'h', 'i', to dest2
for(size_t n = 0; n < sizeof dest2; ++n) {
char c = dest2[n];
c ? printf("'%c' ", c) : printf("'\\0' ");
}
printf("\n");
#ifdef __STDC_LIB_EXT1__
set_constraint_handler_s(ignore_handler_s);
char dst1[6], src1[100] = "hello";
int r1 = strncpy_s(dst1, 6, src1, 100); // writes 0 to r1, 6 characters to dst1
printf("dst1 = \"%s\", r1 = %d\n", dst1,r1); // 'h','e','l','l','o','\0' to dst1
char dst2[5], src2[7] = {'g','o','o','d','b','y','e'};
int r2 = strncpy_s(dst2, 5, src2, 7); // copy overflows the destination array
printf("dst2 = \"%s\", r2 = %d\n", dst2,r2); // writes nonzero to r2,'\0' to dst2[0]
char dst3[5];
int r3 = strncpy_s(dst3, 5, src2, 4); // writes 0 to r3, 5 characters to dst3
printf("dst3 = \"%s\", r3 = %d\n", dst3,r3); // 'g', 'o', 'o', 'd', '\0' to dst3
#endif
}
Possible output:
strncpy(dest, src, 5) to a 6-byte dst gives : 'h' 'i' '\0' '\0' '\0' 'f'
strncpy(dest2, src, 2) to a 2-byte dst gives : 'h' 'i'
dst1 = "hello", r1 = 0
dst2 = "", r2 = 22
dst3 = "good", r3 = 0
References
- C11 standard (ISO/IEC 9899:2011):
-
- 7.24.2.4 The strncpy function (p: 363-364)
-
- K.3.7.1.4 The strncpy_s function (p: 616-617)
- C99 standard (ISO/IEC 9899:1999):
-
- 7.21.2.4 The strncpy function (p: 326-327)
- C89/C90 standard (ISO/IEC 9899:1990):
-
- 4.11.2.4 The strncpy function
From: https://en.cppreference.com/w/c/string/byte/strncpy
strncpy, strncpy_s的更多相关文章
- c/c++头文件_string
string, cstring, string.h 一.string头文件 主要包含一些字符串转换的函数 // sto* NARROW CONVERSIONS// sto* WIDE CONVERSI ...
- 字符串操作函数<string.h>相关函数strcpy,strcat,等源码。
首先说一下源码到底在哪里找. 我们在文件中包含<cstring>时,如果点击右键打开文档, 会打开cstring,我们会发现路径为: D:\Program Files\visual stu ...
- strcpy、strncpy 和安全的strncpy_s
strcpy和strncpy摘于linux 内核源码的/lib/string.c char *self_strcpy(char *dest, const char *src) { char *tmp ...
- C语言strcpy,strncpy和strlcpy讲解
前言 C风格的字符串处理函数有很多,如strcpy().strcat()等等. strcpy与strcat char* strcpy (char* dest, const char* src); ch ...
- strcpy/strncpy/strcpy_s比较
转载自:http://blog.csdn.net/caomiao2006/article/details/4766416 strcpy()是依据源串的/0作为结束判断的,不检查copy先的Buffer ...
- strncpy函数使用
strncpy()函数原型:extern char *strncpy(char *dest, char *src, int n); 用法:#include <string.h> ...
- Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解
strlen(返回字符串长度) 表头文件 #include <string.h> 定义函数 size_t strlen(const char *s); 函数说明 strlen()用来计 ...
- strncpy,strcpy
strncpy不会为des自动添加“\0” strcpy遇空结束,自动添加结束符 结论: 1.使用strcpy时一定不能用于无结束符的字符串,因为strcpy依赖\0判断源字符串的结束 2.使用str ...
- [skill] strncpy里边有两个坑
以前的笔记,今日翻出了复看了一下,转过来. ------------------------------------ 今天发现xxxdump中使用xxx_strncpy 替换 strncpy导致的bu ...
随机推荐
- web发展阶段简介
web1.0.web2.0和web3.0的区别前言: 其实并没有什么所谓的2.0.3.0,因为你没法准确的界定它是什么样的应用,也没法界定它是什么时候开始的,什么时候结束,它只是互联网本身发展的一种 ...
- Mybatis关联一对多映射不能查询出所有的数据的问题
在使用Mybatis进行一对多查询时,如果返回的是一个对象的话,可以发现将一对多的数据全都取出来了,但是这样的缺点是有很多值为null,我们更喜欢将返回值设为Map的形式,这样可以去除那些多余null ...
- 利用Python中的mock库对Python代码进行模拟测试
这篇文章主要介绍了利用Python中的mock库对Python代码进行模拟测试,mock库自从Python3.3依赖成为了Python的内置库,本文也等于介绍了该库的用法,需要的朋友可以参考下 ...
- HDU 1435 Stable Match 【稳定婚姻问题】
<题目链接> 题目大意:给你n个发射站和n个接受站的位置,并且给出他们的容量,现在需要你对这n对站台进行匹配,距离越近的站台越稳定,如果两个站台距离相等,容量越大的越稳定.问你稳定匹配是什 ...
- 002. Ansible部署及配置介绍
一 Ansible的安装部署 1.1 PIP方式 安装PIP 略,可参考<001.Pip简介及使用>. 提示:建议将PIP升级到最新:pip install --upgrade pip. ...
- Python常用模块--configparser
作用: 官方:实现基本配置语言的类,该语言提供类似于Microsoft Windows INI文件中的结构.您可以使用它来编写可由最终用户轻松定制的Python程序. 通俗的说:该模块用于系统配置文件 ...
- hexo博客pure主题解决不蒜子计数不显示的问题
最近在建个人博客网站,想统计自己的博客页面访问量,就用到了不蒜子页面访问统计.可是遇到了糟心事,居然不显示!!! 不蒜子官网示例:两行代码,搞定计数 <script async src=&quo ...
- 阿里 EasyExcel 使用及避坑
github地址:https://github.com/alibaba/easyexcel 原本在项目中使用EasyPoi读取excel,后来为了统一技术方案,改用阿里的EasyExcel.EasyE ...
- Java反射之基础概念
0.实例准备 package com.blueStarWei.invoke; public class Student { private String name; public Student() ...
- 深度学习目标检测:RCNN,Fast,Faster,YOLO,SSD比较
转载出处:http://blog.csdn.net/ikerpeng/article/details/54316814 知乎的图可以放大,更清晰,链接:https://www.zhihu.com/qu ...