15

得到了一些不是我的代码并且它产生了这个警告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>
        ]

这是有问题的代码:

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 行。我试过用谷歌搜索警告,但无法弄清楚它是关于什么的。有人可以帮我解决这个问题吗?

视觉工作室 2005 SP1 Windows 7 RC1

.

编辑: prepend 是一个类型定义的字符串

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

2 回答 2

13

警告告诉您,如果n太大,您将面临缓冲区溢出的风险——您知道这不会发生,因为您刚刚使用 a 计算的方式min,但糟糕的编译器不会。我建议你接受编译器自己的建议,并use -D_SCL_SECURE_NO_WARNINGS为这个源文件...

于 2009-05-24T04:31:59.843 回答
8

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

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

于 2009-05-24T04:30:32.620 回答