简体   繁体   English

错误C4996:'std :: _ Copy_impl':带有可能不安全的参数的函数调用

[英]error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe

I know this question was asked numerous times in SO, but this is a variation from the rest. 我知道这个问题在SO中被多次询问过,但这与其他问题有所不同。

Compiler Error: Function call with parameters that may be unsafe 编译器错误:带有可能不安全的参数的函数调用

Visual Studio Warning C4996 Visual Studio警告C4996

xutility(2227): warning C4996: 'std::_Copy_impl' xutility(2227):警告C4996:'std :: _ Copy_impl'

Failing Code Snippet 失败的代码片段

DWORD dwNumberOfNames = pExportDirectory->NumberOfNames;
LPDWORD dwNames = (LPDWORD)((LPBYTE)hDLL +  pExportDirectory->AddressOfNames);
std::vector< std::string > exports;
std::copy(
    dwNames, 
    dwNames + dwNumberOfNames, 
    [&exports, &hDLL](DWORD  dwFuncOffset)
{
    std::string fname = std::string((PCHAR)((PBYTE)hDLL + dwFuncOffset));
    exports.push_back(fname);
}
);

Compiler Error 编译错误

Error 1 error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. 错误1错误C4996:'std :: _ Copy_impl':带有可能不安全的参数的函数调用 - 此调用依赖于调用者来检查传递的值是否正确。 To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. 要禁用此警告,请使用-D_SCL_SECURE_NO_WARNINGS。 See documentation on how to use Visual C++ 'Checked Iterators' C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\include\\xutility 2176 请参阅有关如何使用Visual C ++'Checked Iterators'的文档:C:\\ Program Files(x86)\\ Microsoft Visual Studio 11.0 \\ VC \\ include \\ xutility 2176

Question

Considering, C4996 , means the function was marked to be deprecated Where is the problem? 考虑到, C4996 ,意味着该功能已标记为已弃用问题在哪里?

  1. Is it the use of std::copy, that MS thinks is unsafe and would be deprecated? 它是否使用std :: copy,MS认为它是不安全的并且会被弃用?
  2. Is it because I have used std::copy with a C Array ? 是因为我使用了带有C Array std::copy吗?
  3. Is it because of the way I am using Lambda expression? 是因为我使用Lambda表达式的方式吗?
  4. If std::copy is deprecated, what is the alternative, if I need to be portable. 如果不推荐使用std :: copy,那么如果我需要可移植的话,还有什么选择。

Note 注意

I know, how to suppress the warning, but I am curious to know, the root cause of the problem? 我知道,如何压制警告,但我很想知道,问题的根本原因是什么?

Also, equally important for me to know, the portable way to handle this issue without compromising code quality. 此外,同样重要的是,我知道,在不影响代码质量的情况下处理此问题的便携方式。

You aren't calling std::copy according to msdn: http://msdn.microsoft.com/en-us/library/x9f6s1wf.aspx 你不是根据msdn调用std::copyhttp//msdn.microsoft.com/en-us/library/x9f6s1wf.aspx

That function signature is this: 那个函数签名是这样的:

template<class InputIterator, class OutputIterator> 
   OutputIterator copy( 
      InputIterator _First,  
      InputIterator _Last,  
      OutputIterator _DestBeg 
   );

There's no place for a functor/lambda there. 那里没有functor / lambda的地方。

You're not calling std::copy_if either: http://msdn.microsoft.com/en-us/library/ee384415.aspx 你也不是在调用std::copy_ifhttp//msdn.microsoft.com/en-us/library/ee384415.aspx

template<class InputIterator, class OutputIterator, class BinaryPredicate>
   OutputIterator copy_if(
      InputIterator _First, 
      InputIterator _Last,
      OutputIterator _Dest,
      Predicate _Pred
    );

As you have no output iterator, nor does your predicate return a bool. 因为你没有输出迭代器,你的谓词也没有返回bool。

It looks like you want std::transform : http://msdn.microsoft.com/en-us/library/391xya49.aspx 看起来你想要std::transformhttp//msdn.microsoft.com/en-us/library/391xya49.aspx

template<class InputIterator, class OutputIterator, class UnaryFunction> 
   OutputIterator transform( 
      InputIterator _First1,  
      InputIterator _Last1,  
      OutputIterator _Result, 
      UnaryFunction _Func 
   ); 

And you should be returning the value you want in the output iterator. 你应该在输出迭代器中返回你想要的值。 So it'll be something like this: 所以它会是这样的:

std::transform(
    dwNames, 
    dwNames + dwNumberOfNames,
    std::back_inserter(exports),
    [&hDLL](DWORD  dwFuncOffset) // This lambda is WRONG
{
    // THIS LINE IS WRONG 
    std::string fname = std::string((PCHAR)((PBYTE)hDLL + dwFuncOffset));
    return fname;
}
);

My lambda is wrong. 我的lambda错了。 You need the input of the ELEMENTS of your array (I'm not sure of the type) as the argument to the lambda, and return what you want inserted into the exports vector. 您需要输入数组的ELEMENTS(我不确定类型)作为lambda的参数,并返回要插入到exports向量中的内容。

You probably also didn't know about back_inserter . 你可能也不知道back_inserter It's in the <iterator> header. 它位于<iterator>标头中。 See here: http://msdn.microsoft.com/en-us/library/12awccbs.aspx and here: http://www.cplusplus.com/reference/iterator/back_inserter/ 请参阅此处: http//msdn.microsoft.com/en-us/library/12awccbs.aspx ,此处: http//www.cplusplus.com/reference/iterator/back_inserter/

That may not be 100% of the answer, but with that, I think you can get to where you want to go. 这可能不是100%的答案,但有了这个,我想你可以到达你想去的地方。

It is not std::copy itself, I think your problem is to use LPDWORD to do the copy which makes Visuall C++ thinks you are doing C string copy because LPDWORD is not a checked iterator. 它不是std::copy本身,我认为你的问题是使用LPDWORD进行复制,这使得Visuall C ++认为你正在进行C字符串复制,因为LPDWORD不是一个经过检查的迭代器。

http://msdn.microsoft.com/en-us/library/ttcz0bys(v=vs.120).aspx http://msdn.microsoft.com/en-us/library/ttcz0bys(v=vs.120).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2015错误C4996&#39;std :: _ Copy_impl&#39;:使用参数可能不安全的函数调用 - Visual Studio 2015 error C4996 'std::_Copy_impl': Function call with parameters that may be unsafe xutility(2227):警告C4996:&#39;std :: _ Copy_impl&#39; - xutility(2227): warning C4996: 'std::_Copy_impl' 错误C4996:&#39;ctime&#39;:此函数或变量可能不安全 - error C4996: 'ctime': This function or variable may be unsafe 警告C4996:与POSIX上的GCC相比,此功能或变量可能不安全 - Warning C4996: This function or variable may be unsafe — compared to GCC on POSIX sprintf错误“变量可能不安全”(C4996)…替代品? - c++ - sprintf error “variable may be unsafe” (C4996)… alternatives? C++17 什么新:错误 C4996:'getenv':此 function 或变量可能不安全。 考虑改用 _dupenv_s - C++17 what new with : error C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead 带有_wfopen函数的C ++ C4996错误 - C++ C4996 error with _wfopen function std :: copy参数可能不安全 - std::copy with parameters that may be unsafe 使用升压示例时出现错误C4996 - Error C4996 on using boost example OpenCL编译器错误C4996 - OpenCL Compiler Error C4996
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM