Error C4996: 'std :: _ Copy_impl': function call with parameters that may be unsafe

I know this question has been asked many times in SO, but this is an option from the rest.

Compiler error: calling a function with parameters that may be unsafe

Visual Studio C4996 Warning

xutility (2227): warning C4996: 'std :: _ Copy_impl'

Code Snippet Failure

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': calling a function with parameters that may be unsafe - this call depends on the caller to verify that the passed values ​​are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See Visual C ++ Tested Iterator Documentation C: \ Program Files (x86) \ Microsoft Visual Studio 11.0 \ VC \ include \ xutility 2176

Question

Given C4996 , it means the function has been marked as deprecated. Where is the problem?

  • Is std :: copy used, which MS considers unsafe and outdated?
  • Is it because I used std::copy with C Array ?
  • Is it because of how I use the Lambda expression?
  • If std :: copy is out of date, what is the alternative if I need to be portable.

Note

I know how to suppress a warning, but am curious to know what is the root cause of the problem?

In addition, it is equally important for me to know a portable way to deal with this problem without sacrificing code quality.

+6
source share
2 answers

You do not call std::copy according to msdn: http://msdn.microsoft.com/en-us/library/x9f6s1wf.aspx

This function signature is as follows:

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

There is no room for a functor / lambda.

You do not call std::copy_if : http://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 ); 

Since you do not have an output iterator, and your predicate does not return bool.

It looks like you want std::transform : http://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 return the required value in the output iterator. So, it will 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. You need to enter the ELEMENTS of your array (I'm not sure of the type) as the lambda argument and return what you want to insert into the exports vector.

You probably also did not know about back_inserter . It is located in the <iterator> header. See here: http://msdn.microsoft.com/en-us/library/12awccbs.aspx and here: http://www.cplusplus.com/reference/iterator/back_inserter/

This may not be a 100% answer, but with this, I think you can get where you want to go.

+6
source

This is not std::copy , I think your problem is to use LPDWORD for copying, which makes Visuall C ++ think you are doing C string copy because LPDWORD not a validated iterator.

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

+1
source

Source: https://habr.com/ru/post/954890/


All Articles