博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
strncpy, strncpy_s
阅读量:6417 次
发布时间:2019-06-23

本文共 5496 字,大约阅读时间需要 18 分钟。

Defined in header 
<string.h>
   
  (1)  
char *strncpychar *dest, const char *src,  count );
(until C99)
char *strncpychar *restrict dest, const char *restrict src,  count );
(since C99)
errno_t strncpy_s(char *restrict dest, rsize_t destsz,
                  const char *restrict src, rsize_t count);
(2) (since C11)
     
1) Copies at most 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.
 If count is reached before the entire array src was copied, the resulting character array is not null-terminated.
 If, after copying the terminating null character from srccount is not reached, additional null characters are written to dest until the total of count characters have been written.
 The behavior is undefined if the character arrays overlap, if either 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.
2) Same as (1), except that the function does not continue writing zeroes into the destination array to pad up to 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  function:
  • src or dest is a null pointer
  • destsz or count is zero or greater than RSIZE_MAX
  • count is greater or equal destsz, but destsz is less or equal strnlen_s(src, count), in other words, truncation would occur
  • overlap would occur between the source and the destination strings
 The behavior is undefined if the size of the character array pointed to by 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 including string.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

1) returns a copy of dest
2) returns zero on success, returns non-zero on error. Also, on error, writes zero to dest[0] (unless 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 , is only allowed to clobber the remainder of the destination array if an error occurs.

Unlike strncpystrncpy_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 
#include
#include
 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 ("strncpy(dest, src, 5) to a 6-byte dest gives : "); for( n = 0; n < sizeof dest; ++n) { char c = dest[n]; c ? ("'%c' ", c) : ("'\\0' "); } ("\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( n = 0; n < sizeof dest2; ++n) { char c = dest2[n]; c ? ("'%c' ", c) : ("'\\0' "); } ("\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 ("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 ("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 ("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 = 0dst2 = "", r2 = 22dst3 = "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

转载地址:http://svpra.baihongyu.com/

你可能感兴趣的文章
产品设计体会(3013)项目的“敏捷沟通”实践
查看>>
RHEL6.3基本网络配置(1)ifconfig命令
查看>>
网络诊断工具之—路由追踪tracert命令
查看>>
Java模拟HTTP的Get和Post请求(增强)
查看>>
php 环境搭建(windows php+apache)
查看>>
让虚拟机的软盘盘符不显示(适用于所有windows系统包括Windows Server)
查看>>
Cygwin不好用
查看>>
jQuery插件之验证控件jquery.validate.js
查看>>
[经验]无线鼠标和无线键盘真的不能用了?——雷柏的重生之路~
查看>>
【转】plist涉及到沙盒的一个问题
查看>>
GNU make manual 翻译( 一百四十五)
查看>>
重构之美-走在Web标准化设计的路上[复杂表单]3 9 Update
查看>>
linux中的优先搜索树的实现--prio_tree【转】
查看>>
重构之美-跨越Web标准,触碰语义网[开门见山:Microformat]
查看>>
git入门与实践【转】
查看>>
WPF 虚拟键盘
查看>>
储存卡无法打开专家教您怎么数据恢复
查看>>
彼得原理
查看>>
如何利用【百度地图API】,制作房产酒店地图?(下)——结合自己的数据库...
查看>>
[20171113]修改表结构删除列相关问题3.txt
查看>>