2009-05-24 47 views
14

得到了一些代碼,不是我和它產生此警告大氣壓:編譯器錯誤:帶參數的函數調用可能不安全

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; 

回答

10

警告告訴你,如果n太大,你就會冒一個緩衝區溢出的風險 - 你知道因爲你用min計算的方式不會發生,但糟糕的commpiler沒有。我建議你採取編譯器自己的建議和use -D_SCL_SECURE_NO_WARNINGS這一個源文件...

+4

我結束了使用#pragma警告(禁用:4996)作爲預處理器定義沒有工作 – Lodle 2009-05-24 04:49:02

6

看看這個MSDN頁文檔上的警告

的MS C++編譯器決定棄用字符串方法std :: string :: copy,因爲它可能不安全,可能會導致緩衝區溢出。這種棄用是微軟特有的,你可能不會在其他編譯器平臺上看到它。

+1

因此它是安全的禁用它?我使用odeint boost來求解微分方程,並且彈出這個錯誤。我使用的是visual studio 2013.我使用`#pragma警告(禁用:4996)`和代碼正在工作,但不確定這是否安全。謝謝 – CroCo 2015-05-27 00:12:20