memcpy() 函数即将禁用

  • 2009-05-24
  • 本文字数:919 字

    阅读完需:约 3 分钟

微软已经建议禁用 memcpy() 函数,该函数很可能会在今年底出现在微软的 SDL 禁用列表中。memcpy() 加入到 strcpy, strncpy, strcat, strncat 等常用函数的队伍中,都是由于可通过缓存溢出进行安全攻击而被禁用。

一些微软的安全更新由于 memcpy() 的原因而发布:MS03-030 (DirectX)、MS03-043 (Messenger Service)、MS03-044 (Help and Support)、MS05-039 (PnP)、MS04-011 (PCT)、MS05-030 (Outlook Express)、CVE-2007-3999 (MIT Kerberos v5)、CVE-2007-4000 (MIT Kerberos v5) 及其他。

微软禁用的函数有 memcpy()、CopyMemory() 和 RtlCopyMemory()。要禁用这些函数,编程人员可添加以下#pragma 代码行到头文件,编译器每一次遇到该行代码的时候都会发出警告。

errno_t __cdecl
memcpy_s(
_Out_opt_bytecap_post_bytecount_(_DstSize, _MaxCount)
void * _Dst,
_In_ rsize_t _DstSize,
_In_opt_bytecount_(_MaxCount) const void * _Src,
_In_ rsize_t _MaxCount
);

Description
描述
Banned function
禁用函数
Recommended function
推荐函数
String copy
字符串复制函数 strcpy, wcscpy, _tcscpy, _mbscpy, StrCpy, StrCpyA, StrCpyW, lstrcpy, lstrcpyA, lstrcpyW, strcpyA, strcpyW, _tccpy, _mbccpy strcpy_s String concatenation
字符串连接函数 strcat, wcscat, _tcscat, _mbscat, StrCat, StrCatA, StrCatW, lstrcat, lstrcatA, lstrcatW, StrCatBuffW, StrCatBuff, StrCatBuffA, StrCatChainW, strcatA, strcatW, _tccat, _mbccat strcat_s Sprintf
Sprintf 函数 wnsprintf, wnsprintfA, wnsprintfW, sprintfW, sprintfA, wsprintf, wsprintfW, wsprintfA, sprintf, swprintf, _stprintf sprintf_s Tokenizing 函数 strtok, _tcstok, wcstok, _mbstok strtok_s Scanf scanf, wscanf, _tscanf, sscanf, swscanf, _stscanf sscanf_s Numeric conversions

数值转换函数

_itoa, _itow, _i64toa, _i64tow, _ui64toa, _ui64tot, _ui64tow, _ultoa, _ultot, _ultow _itoa_s, _itow_s Gets gets, _getts, _gettws gets_s

SDL 提供了头文件 ( banned.h ),编程人员一旦使用所有被禁用函数就会得到相关的警告。另一种可选的方式是编程人员可以在 VS2005 或后续版本中使用 /W4-C4996 编译选项。