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

Lod*_*dle 15 c++ windows warnings visual-studio-2005

得到了一些不是我的代码并且它产生了这个警告atm:

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>
        ]
Run Code Online (Sandbox Code Playgroud)

这是有问题的代码:

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;
    };
Run Code Online (Sandbox Code Playgroud)

并且警告指的是prepend.copy行.我试过谷歌搜索警告,但无法弄清楚它是什么.有人可以帮我解决这个问题.

Visual Studio 2005 SP1 Windows 7 RC1

.

编辑:prepend是一个typedefed的字符串

typedef basic_string<char, char_traits<char>, allocator<char> > string;
Run Code Online (Sandbox Code Playgroud)

Ale*_*lli 12

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

  • 我最终使用#pragma warning(disable:4996),因为预处理器定义没有用 (4认同)
  • @Lodle - 奇怪的是,`pragma warning(disable: 4996)` 在 VS2010 中对我不起作用;将 `_SCL_SECURE_NO_WARNINGS` 添加到每个单独文件(和每个单独构建)的定义中似乎可以解决问题。 (2认同)

Jar*_*Par 8

查看此MSDN页面以获取有关警告的文档

MS C++编译器决定弃用std :: string :: copy方法,因为它可能不安全使用并可能导致缓冲区溢出.此弃用是Microsoft特定的,您可能不会在其他编译器平台上看到它.