memcpy_s 的安全提示

本文详细介绍了memcpy_s函数的使用方法及注意事项。此函数用于安全地从一个内存块复制数据到另一个内存块,并在可能发生错误的情况下进行错误处理。文章还解释了函数参数的意义以及返回值的含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

errno_t memcpy_s( void *restrict dest, rsize_t destsz,
                  const void *restrict src, rsize_t count );
(2)(since C11)
   
1) Copies  count characters from the object pointed to by  src to the object pointed to by  dest. Both objects are interpreted as arrays of  unsigned char.
 The behavior is undefined if access occurs beyond the end of the dest array. If the objects overlap  (which is a violation of the restrict contract) (since C99), the behavior is undefined. The behavior is undefined if either  destor  src is a null pointer.
2) Same as  (1), except that the following errors are detected at runtime and cause the entire destination range  [dest, dest+destsz) to be zeroed out (if both  dest and  destsz are valid), as well as call the currently installed  constraint handler function:
  • dest or src is a null pointer
  • destsz or count is greater than RSIZE_MAX
  • count is greater than destsz (buffer overflow would occur)
  • the source and the destination objects overlap
 The behavior is undefined if the size of the character array pointed to by  dest <  count <=  destsz; in other words, an erroneous value of  destsz does not expose the impending buffer overflow.
As all bounds-checked functions,  memcpy_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  1before including  string.h.

Parameters

dest-pointer to the object to copy to
destsz-max number of bytes to modify in the destination (typically the size of the destination object)
src-pointer to the object to copy from
count-number of bytes to copy

Return value

1) Returns a copy of  dest
2) Returns zero on success and non-zero value on error. Also on error, if  dest is not a null pointer and  destsz is valid, writes  destsz zero bytes in to the destination array.
// // Created by l30079074 on 25-8-11. // #include <securec.h> #include <stdlib.h> #include <stdio.h> #include <stdbool.h> #include “securec.h” static int GetUnselectedNum(const int *favorites,size_t favoritesSize, const int *presents,size_t presentsSize) { int isGetGift[favoritesSize]; memset_s(isGetGift, favoritesSize, 1, favoritesSize); int giftIndex = 0; int peopleIndex = 0; int countOneCycle = 0; int numGetGift = 0; while (peopleIndex < favoritesSize && giftIndex < presentsSize) { if (isGetGift[peopleIndex] == 1 && presents[giftIndex] == favorites[peopleIndex]) { isGetGift[peopleIndex] = -1; giftIndex++; countOneCycle++; numGetGift++; } if (peopleIndex == favoritesSize - 1) { if (countOneCycle == 0) { break; } else { peopleIndex = 0; countOneCycle = 0; } } else { peopleIndex++; } } return favoritesSize - numGetGift; } int main() { int favn = 5; int pren = 5; int fav[5] = {1, 2, 2, 1, 2}; int pre[5] = {2, 1, 1, 2, 1}; int res = GetUnselectedNum(fav, favn, pre, pren); printf(“%d”, res); return 0; } 我这个代码进行如下报错是为什么 ====================[ 构建 | hw663 | Debug ]====================================== "C:\Program Files\JetBrains\CLion 2024.3.2\bin\cmake\win\x64\bin\cmake.exe" --build D:\untitled\cmake-build-debug --target hw663 -j 26 [1/2] Building C object CMakeFiles/hw663.dir/hw663.c.obj In file included from D:/untitled/hw663.c:4: D:/C_Libraries/secure-master/include/securec.h:87:13: warning: 'memmove_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 87 | errno_t memmove_s(void* dest, size_t destMax, const void* src, size_t count); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:91:13: warning: 'wcscpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 91 | errno_t wcscpy_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:93:13: warning: 'wcsncpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 93 | errno_t wcsncpy_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc, size_t count); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:95:13: warning: 'wcscat_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 95 | errno_t wcscat_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:97:13: warning: 'wcsncat_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 97 | errno_t wcsncat_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc, size_t count); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:100:11: warning: 'strtok_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 100 | char* strtok_s(char* strToken, const char* strDelimit, char** context); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:102:14: warning: 'wcstok_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 102 | wchar_t* wcstok_s(wchar_t* strToken, const wchar_t* strDelimit, wchar_t** context); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:105:9: warning: 'sprintf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 105 | int sprintf_s(char* strDest, size_t destMax, const char* format, ...) SECUREC_ATTRIBUTE(3,4); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:110:9: warning: 'vsprintf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 110 | int vsprintf_s(char* strDest, size_t destMax, const char* format, va_list argptr) SECUREC_ATTRIBUTE(3,0); | ^~~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:120:9: warning: 'scanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 120 | int scanf_s(const char* format, ...); | ^~~~~~~ D:/C_Libraries/secure-master/include/securec.h:122:9: warning: 'wscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 122 | int wscanf_s(const wchar_t* format, ...); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:130:9: warning: 'fscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 130 | int fscanf_s(FILE* stream, const char* format, ...); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:132:9: warning: 'fwscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 132 | int fwscanf_s(FILE* stream, const wchar_t* format, ...); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:140:9: warning: 'sscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 140 | int sscanf_s(const char* buffer, const char* format, ...); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:142:9: warning: 'swscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 142 | int swscanf_s(const wchar_t* buffer, const wchar_t* format, ...); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:150:11: warning: 'gets_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 150 | char* gets_s(char* buffer, size_t destMax); | ^~~~~~ D:/C_Libraries/secure-master/include/securec.h:155:13: warning: 'memcpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 155 | errno_t memcpy_s(void* dest, size_t destMax, const void* src, size_t count); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:158:13: warning: 'strcpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 158 | errno_t strcpy_s(char* strDest, size_t destMax, const char* strSrc); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:160:13: warning: 'strncpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 160 | errno_t strncpy_s(char* strDest, size_t destMax, const char* strSrc, size_t count); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:163:13: warning: 'strcat_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 163 | errno_t strcat_s(char* strDest, size_t destMax, const char* strSrc); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:165:13: warning: 'strncat_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 165 | errno_t strncat_s(char* strDest, size_t destMax, const char* strSrc, size_t count); | ^~~~~~~~~ [2/2] Linking C executable hw663.exe FAILED: hw663.exe C:\Windows\system32\cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\gcc.exe -g CMakeFiles/hw663.dir/hw663.c.obj -o hw663.exe -Wl,--out-implib,libhw663.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ." C:\Program Files\JetBrains\CLion 2024.3.2\bin\mingw\bin/ld.exe: CMakeFiles/hw663.dir/hw663.c.obj: in function `GetUnselectedNum': D:/untitled/hw663.c:13: undefined reference to `memset_s' collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed.
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值