编译器错误:带参数的函数调用可能不安全

问题描述:

得到了一些代码,不是我和它产生此警告大气压:编译器错误:带参数的函数调用可能不安全

iehtmlwin.cpp(264) : warning C4996: 'std::basic_string<_Elem,_Traits,_Ax>::copy': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' 
     with 
     [ 
      _Elem=char, 
      _Traits=std::char_traits<char>, 
      _Ax=std::allocator<char> 
     ] 
     c:\program files (x86)\microsoft visual studio 8\vc\include\xstring(1680) : see declaration of 'std::basic_string<_Elem,_Traits,_Ax>::copy' 
     with 
     [ 
      _Elem=char, 
      _Traits=std::char_traits<char>, 
      _Ax=std::allocator<char> 
     ] 

这是有问题的代码:

HRESULT STDMETHODCALLTYPE Read(void __RPC_FAR *pv, ULONG cb, ULONG __RPC_FAR *pcbRead) 
    { 
     if (prepend.size() > 0) 
     { 
      int n = min(prepend.size(), cb); 
      prepend.copy((char *) pv, n); 
      prepend = prepend.substr(n); 
      if (pcbRead) 
       *pcbRead = n; 

      return S_OK; 
     }; 

     int rc = Read((char *) pv, cb); 
     if (pcbRead) 
      *pcbRead = rc; 

     return S_OK; 
    }; 

和该警告指的是prepend.copy行。我试着用google搜索这个警告,但是不知道它是关于什么的。请有人帮我解决这个问题。

的Visual Studio 2005 SP1 的Windows 7 RC1

编辑:前置是其通过typedef

typedef basic_string<char, char_traits<char>, allocator<char> > string; 

警告告诉你,如果n太大,你就会冒一个缓冲区溢出的风险 - 你知道因为你用min计算的方式不会发生,但糟糕的commpiler没有。我建议你采取编译器自己的建议和use -D_SCL_SECURE_NO_WARNINGS这一个源文件...

+4

我结束了使用#pragma警告(禁用:4996)作为预处理器定义没有工作 – Lodle 2009-05-24 04:49:02

看看这个MSDN页文档上的警告

的MS C++编译器决定弃用字符串方法std :: string :: copy,因为它可能不安全,可能会导致缓冲区溢出。这种弃用是微软特有的,你可能不会在其他编译器平台上看到它。

+1

因此它是安全的禁用它?我使用odeint boost来求解微分方程,并且弹出这个错误。我使用的是visual studio 2013.我使用`#pragma警告(禁用:4996)`和代码正在工作,但不确定这是否安全。谢谢 – CroCo 2015-05-27 00:12:20