未验证 提交 27195f67 编写于 作者: H Huo Yaoyuan 提交者: GitHub

Use ClrSafeInt everywhere and cleanup other same math declarations (#70197)

* Replace safe math in util.hpp with ClrSafeInt

* Remove unused definitions in safeint.h

* Replace BSTR usage with ClrSafeInt

* Delete intsafe.h
上级 949b7230
此差异已折叠。
......@@ -56,7 +56,7 @@ namespace
pfwalk->m_sequence = (ULONG)-1;
// Treat base class as an initial member.
if (!SafeAddUINT32(&(pfwalk->m_placement.m_offset), cbAdjustedParentLayoutNativeSize))
if (!ClrSafeInt<UINT32>::addition(pfwalk->m_placement.m_offset, cbAdjustedParentLayoutNativeSize, pfwalk->m_placement.m_offset))
COMPlusThrowOM();
}
}
......@@ -172,7 +172,7 @@ namespace
// Insert enough padding to align the current data member.
while (cbCurOffset % alignmentRequirement)
{
if (!SafeAddUINT32(&cbCurOffset, 1))
if (!ClrSafeInt<UINT32>::addition(cbCurOffset, 1, cbCurOffset))
COMPlusThrowOM();
}
......@@ -192,8 +192,8 @@ namespace
if (classSizeInMetadata != 0)
{
ULONG classSize = classSizeInMetadata;
if (!SafeAddULONG(&classSize, (ULONG)parentSize))
ULONG classSize;
if (!ClrSafeInt<ULONG>::addition(classSizeInMetadata, (ULONG)parentSize, classSize))
COMPlusThrowOM();
// size must be large enough to accomodate layout. If not, we use the layout size instead.
......@@ -207,7 +207,7 @@ namespace
if (calcTotalSize % LargestAlignmentRequirement != 0)
{
if (!SafeAddUINT32(&calcTotalSize, LargestAlignmentRequirement - (calcTotalSize % LargestAlignmentRequirement)))
if (!ClrSafeInt<uint32_t>::addition(calcTotalSize, LargestAlignmentRequirement - (calcTotalSize % LargestAlignmentRequirement), calcTotalSize))
COMPlusThrowOM();
}
}
......
......@@ -4344,8 +4344,8 @@ FCIMPL3(void, MngdNativeArrayMarshaler::ConvertSpaceToNative, MngdNativeArrayMar
if (cbElement == 0)
COMPlusThrow(kArgumentException, IDS_EE_COM_UNSUPPORTED_SIG);
SIZE_T cbArray = cElements;
if ( (!SafeMulSIZE_T(&cbArray, cbElement)) || cbArray > MAX_SIZE_FOR_INTEROP)
SIZE_T cbArray;
if ( (!ClrSafeInt<SIZE_T>::multiply(cElements, cbElement, cbArray)) || cbArray > MAX_SIZE_FOR_INTEROP)
COMPlusThrow(kArgumentException, IDS_EE_STRUCTARRAYTOOLARGE);
*pNativeHome = CoTaskMemAlloc(cbArray);
......@@ -4374,7 +4374,7 @@ FCIMPL3(void, MngdNativeArrayMarshaler::ConvertContentsToNative, MngdNativeArray
SIZE_T cElements = (*pArrayRef)->GetNumComponents();
if (pMarshaler == NULL || pMarshaler->ComToOleArray == NULL)
{
if ( (!SafeMulSIZE_T(&cElements, OleVariant::GetElementSizeForVarType(pThis->m_vt, pThis->m_pElementMT))) || cElements > MAX_SIZE_FOR_INTEROP)
if ( (!ClrSafeInt<SIZE_T>::multiply(cElements, OleVariant::GetElementSizeForVarType(pThis->m_vt, pThis->m_pElementMT), cElements)) || cElements > MAX_SIZE_FOR_INTEROP)
COMPlusThrow(kArgumentException, IDS_EE_STRUCTARRAYTOOLARGE);
_ASSERTE(!GetTypeHandleForCVType(OleVariant::GetCVTypeForVarType(pThis->m_vt)).GetMethodTable()->ContainsPointers());
......@@ -4434,8 +4434,8 @@ FCIMPL3(void, MngdNativeArrayMarshaler::ConvertContentsToManaged, MngdNativeArra
if (pMarshaler == NULL || pMarshaler->OleToComArray == NULL)
{
SIZE_T cElements = (*pArrayRef)->GetNumComponents();
if ( (!SafeMulSIZE_T(&cElements, OleVariant::GetElementSizeForVarType(pThis->m_vt, pThis->m_pElementMT))) || cElements > MAX_SIZE_FOR_INTEROP)
SIZE_T cElements;
if ( (!ClrSafeInt<SIZE_T>::multiply((*pArrayRef)->GetNumComponents(), OleVariant::GetElementSizeForVarType(pThis->m_vt, pThis->m_pElementMT), cElements)) || cElements > MAX_SIZE_FOR_INTEROP)
COMPlusThrow(kArgumentException, IDS_EE_STRUCTARRAYTOOLARGE);
// If we are copying variants, strings, etc, we need to use write barrier
......
......@@ -122,98 +122,6 @@ BOOL inline FitsInU4(unsigned __int64 val)
return val == (unsigned __int64)(unsigned __int32)val;
}
// returns FALSE if overflows 15 bits: otherwise, (*pa) is incremented by b
BOOL inline SafeAddUINT15(UINT16 *pa, ULONG b)
{
LIMITED_METHOD_CONTRACT;
UINT16 a = *pa;
// first check if overflows 16 bits
if ( ((UINT16)b) != b )
{
return FALSE;
}
// now make sure that doesn't overflow 15 bits
if (((ULONG)a + b) > 0x00007FFF)
{
return FALSE;
}
(*pa) += (UINT16)b;
return TRUE;
}
// returns FALSE if overflows 16 bits: otherwise, (*pa) is incremented by b
BOOL inline SafeAddUINT16(UINT16 *pa, ULONG b)
{
UINT16 a = *pa;
if ( ((UINT16)b) != b )
{
return FALSE;
}
// now make sure that doesn't overflow 16 bits
if (((ULONG)a + b) > 0x0000FFFF)
{
return FALSE;
}
(*pa) += (UINT16)b;
return TRUE;
}
// returns FALSE if overflow: otherwise, (*pa) is incremented by b
BOOL inline SafeAddUINT32(UINT32 *pa, UINT32 b)
{
LIMITED_METHOD_CONTRACT;
UINT32 a = *pa;
if ( ((UINT32)(a + b)) < a)
{
return FALSE;
}
(*pa) += b;
return TRUE;
}
// returns FALSE if overflow: otherwise, (*pa) is incremented by b
BOOL inline SafeAddULONG(ULONG *pa, ULONG b)
{
LIMITED_METHOD_CONTRACT;
ULONG a = *pa;
if ( ((ULONG)(a + b)) < a)
{
return FALSE;
}
(*pa) += b;
return TRUE;
}
// returns FALSE if overflow: otherwise, (*pa) is multiplied by b
BOOL inline SafeMulSIZE_T(SIZE_T *pa, SIZE_T b)
{
LIMITED_METHOD_CONTRACT;
#ifdef _DEBUG_IMPL
{
//Make sure SIZE_T is unsigned
SIZE_T m = ((SIZE_T)(-1));
SIZE_T z = 0;
_ASSERTE(m > z);
}
#endif
SIZE_T a = *pa;
const SIZE_T m = ((SIZE_T)(-1));
if ( (m / b) < a )
{
return FALSE;
}
(*pa) *= b;
return TRUE;
}
//************************************************************************
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册