编译器错误:使用可能不安全的参数的函数调用 [英] Compiler Error: Function call with parameters that may be unsafe

查看:208
本文介绍了编译器错误:使用可能不安全的参数的函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些不是我的代码,它产生这个警告atm:

  iehtmlwin.cpp(264):warning C4996 :'std :: basic_string< _Elem,_Traits,_Ax> :: copy':带有可能不安全的参数的函数调用 - 此调用依赖调用者检查传递的值是否正确。要禁用此警告,请使用-D_SCL_SECURE_NO_WARNINGS。请参阅关于如何使用Visual C ++'Checked Iterators'
[
_Elem = char,
_Traits = std :: char_traits< char>,
_Ax = std :: allocator< char>
]
c:\program files(x86)\microsoft visual studio 8 \vc\include\xstring(1680):参见声明std :: basic_string< _Elem,_Traits,_Ax> ; :: copy'
with
[
_Elem = char,
_Traits = std :: char_traits< char>,
_Ax = std :: allocator< char>
]

这是有问题的代码:

  HRESULT STDMETHODCALLTYPE读取(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行。我试过谷歌的警告,但不能解决它是什么。有人可以帮我解决这个问题。



Visual Studio 2005 SP1
Windows 7 RC1





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

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


解决方案

警告告诉你,如果 n 太大 - 你知道不能发生,因为你刚刚用 min ,但可怜的commpiler不。我建议你采取编译器自己的建议和使用-D_SCL_SECURE_NO_WARNINGS 这一个源文件...


Got some code that is not mine and its producing this warning 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>
        ]

this is the code in question:

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;
    };

and the warning refers to the prepend.copy line. I have tried googling the warning but cant work out what it is on about. Can some one help me solve this please.

Visual Studio 2005 SP1 Windows 7 RC1

.

Edit: prepend is a string which is typedefed

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

解决方案

The warning is telling you that you risk a buffer overflow if n is too large -- which you know can't happen because of the way you just computed with a min, but the poor commpiler doesn't. I suggest you take the compiler's own advice and use -D_SCL_SECURE_NO_WARNINGS for this one source file...

这篇关于编译器错误:使用可能不安全的参数的函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆