0
我有以下代碼:微軟的安全替換爲std :: reverse_copy?
Integer::Integer(const byte *encodedInteger, size_t byteCount, Signedness s, ByteOrder o)
{
if(o == LITTLE_ENDIAN_ORDER)
{
SecByteBlock block(byteCount);
std::reverse_copy(encodedInteger, encodedInteger+byteCount, block.begin());
Decode(block.begin(), block.size(), s);
return;
}
...
}
我趕上一個C4996 warning上std::reverse_copy
:
1>c:\Program Files\...\VC\include\algorithm(2184): warning C4996: 'std::_Reverse_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'
1> c:\...\VC\include\algorithm(2168) : see declaration of 'std::_Reverse_copy'
1> integer.cpp(2898) : see reference to function template instantiation '_OutIt std::reverse_copy<const byte*,unsigned char*>(_BidIt,_BidIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=unsigned char *,
1> _BidIt=const byte *
1> ]
我很樂意爲您提供目的地緩衝區大小或最後一個目標元素,符合Microsoft平臺上的最佳實踐。
byteCount
是一個參數(和不編譯時間常數),所以我不能呼叫與目的地緩衝區大小的過載(count
模板以下參數):
std::reverse_copy<byte*, byte*, count>(...);
我也天真地嘗試添加目標緩衝區大小和最後一個元素,但他們導致編譯錯誤「預計3個參數 - 4提供」:
std::reverse_copy(encodedInteger, encodedInteger+byteCount, block.begin(), block.begin()+block.size());
什麼是微軟的標準:: reverse_copy安全的替代品,可以讓我指定目標緩衝區的大小?
以下是Microsoft提供的重載從<algorithm>
:
template<class _BidIt,
class _OutIt> inline
_SCL_INSECURE_DEPRECATE
_OutIt _Reverse_copy(_BidIt _First, _BidIt _Last,
_OutIt _Dest,
_STD tr1::false_type)
{ // copy reversing elements in [_First, _Last), unchecked dest
return (_Reverse_copy(_First, _Last,
_Dest, _Iter_cat(_First), _Iter_cat(_Dest)));
}
template<class _BidIt,
class _OutIt> inline
_OutIt reverse_copy(_BidIt _First, _BidIt _Last,
_OutIt _Dest)
{ // copy reversing elements in [_First, _Last)
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Dest);
return (_Reverse_copy(_Unchecked(_First), _Unchecked(_Last),
_Dest, _Is_checked(_Dest)));
}
template<class _BidIt,
class _OutTy,
size_t _OutSize> inline
_OutTy *reverse_copy(_BidIt _First, _BidIt _Last,
_OutTy (&_Dest)[_OutSize])
{ // copy reversing elements in [_First, _Last), array dest
return (_Unchecked(
_STD reverse_copy(_First, _Last,
_Array_iterator<_OutTy, _OutSize>(_Dest))));
}
謝謝。那是哪個超載?它是否有'_Array_iterator <_OutTy,_OutSize>'?爲了完整性,我沒有聲稱目標緩衝區大小是一個問題。我很高興微軟採用更安全的編碼做法。 – jww