关于strncat的不安全。该如何解决
关于strncat的不安全。
如题,有如下语句,如何改写成strncat_s(,,,)
strncat(datadecrypted,plainBlock,8);
------解决方案--------------------
strncat_s(datadecrypted, _countof(datadecrypted), plainBlock, 8);
------解决方案--------------------
一般 的,xxx()和xxx_s()都会非常类似,_s只是为了安全增加了一些(有些还是一样的)需要的参数。
strncat,为了目的地址操作不越界,需要知道目的地址的最大长度。两个函数的原型 :
strncat(char*dst, char*src, tsize count);
strncat_s(char*dst, tsize dstcount, char* src, tsize maxcount);
其中,dstcount是为了复制过程中不会复制到超出dst上限而产生内存溢出(内存溢出很讨厌吧)
------解决方案--------------------
不一样,多了一个dst的长度(第二个参数)。用来把复制控制在允许范围内,不至于内存溢出。
------解决方案--------------------
strncat, strncpy 系列的函数其实让人觉得尴尬,限制长度,却又不保证'\0'结束!
而且strncat的参数定义和很鸡肋,
char *strncat(char * restrict s, const char * restrict append, size_t count);
count是增加的长度,使用非常不方便!
建议使用OpenBSD的
strlcpy和strlcat
------解决方案--------------------
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strncat.c
如题,有如下语句,如何改写成strncat_s(,,,)
strncat(datadecrypted,plainBlock,8);
------解决方案--------------------
strncat_s(datadecrypted, _countof(datadecrypted), plainBlock, 8);
------解决方案--------------------
一般 的,xxx()和xxx_s()都会非常类似,_s只是为了安全增加了一些(有些还是一样的)需要的参数。
strncat,为了目的地址操作不越界,需要知道目的地址的最大长度。两个函数的原型 :
strncat(char*dst, char*src, tsize count);
strncat_s(char*dst, tsize dstcount, char* src, tsize maxcount);
其中,dstcount是为了复制过程中不会复制到超出dst上限而产生内存溢出(内存溢出很讨厌吧)
------解决方案--------------------
不一样,多了一个dst的长度(第二个参数)。用来把复制控制在允许范围内,不至于内存溢出。
------解决方案--------------------
strncat, strncpy 系列的函数其实让人觉得尴尬,限制长度,却又不保证'\0'结束!
而且strncat的参数定义和很鸡肋,
char *strncat(char * restrict s, const char * restrict append, size_t count);
count是增加的长度,使用非常不方便!
建议使用OpenBSD的
strlcpy和strlcat
------解决方案--------------------
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strncat.c
/***
*strncat.c - append n chars of string to new string
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strncat() - appends n characters of string onto
* end of other string
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *strncat(front, back, count) - append count chars of back onto front
*
*Purpose:
* Appends at most count characters of the string back onto the
* end of front, and ALWAYS terminates with a null character.
* If count is greater than the length of back, the length of back
* is used instead. (Unlike strncpy, this routine does not pad out
* to count characters).
*
*Entry:
* char *front - string to append onto
* char *back - string to append
* unsigned count - count of max characters to append
*
*Exit:
* returns a pointer to string appended onto (front).
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strncat (
char * front,
const char * back,
size_t count