1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-09 13:22:24 +03:00

0030550: Coding - Integer overflow in Standard_CString HashCodes

0030551: Foundation Classes - Integer overflow in NCollection_CellFilter HashCode

Signed integers are not used in hash code functions now to prevent undefined behavior on left shift operations with signed integers.
A possibility of negative values of hash codes is eliminated.
INT_MAX → IntegerLast() in hash code functions.
All found hash code functions behaves uniformly now: they return a value in the range [1, theUpperBound]. Relevant comments are added to such functions.
This commit is contained in:
tiv
2019-03-28 12:42:41 +03:00
committed by bugmaster
parent 833034f301
commit 2b2be3fb82
89 changed files with 878 additions and 580 deletions

View File

@@ -108,8 +108,14 @@ protected:
class Hasher
{
public:
static Standard_Integer HashCode(const Key theKey, const Standard_Integer theUpper)
{ return theKey.Value % theUpper + 1; }
//! Returns hash code for the given key, in the range [1, theUpperBound]
//! @param theKey the key which hash code is to be computed
//! @param theUpperBound the upper bound of the range a computing hash code must be within
//! @return a computed hash code, in the range [1, theUpperBound]
static Standard_Integer HashCode (const Key theKey, const Standard_Integer theUpperBound)
{
return ::HashCode (theKey.Value, theUpperBound);
}
static Standard_Boolean IsEqual(const Key theOne, const Key theTwo)
{ return theOne.Value == theTwo.Value; }

View File

@@ -317,26 +317,38 @@ protected:
return Standard_True;
}
//! Compute hash code
Standard_Integer HashCode (const Standard_Integer theUpper) const
//! Returns hash code for this cell, in the range [1, theUpperBound]
//! @param theUpperBound the upper bound of the range a computing hash code must be within
//! @return a computed hash code, in the range [1, theUpperBound]
Standard_Integer HashCode (const Standard_Integer theUpperBound) const
{
// number of bits per each dimension in the hash code
Standard_Integer aDim = Standard_Integer(index.Size());
const Standard_Size aShiftBits = (BITS(long)-1) / aDim;
long aCode=0;
for (int i=0; i < aDim; i++)
aCode = ( aCode << aShiftBits ) ^ index[i];
return (unsigned)aCode % theUpper;
const std::size_t aDim = index.Size();
const std::size_t aShiftBits = (BITS (long) - 1) / aDim;
unsigned int aCode = 0;
for (std::size_t i = 0; i < aDim; ++i)
{
aCode = (aCode << aShiftBits) ^ index[i];
}
return ::HashCode(aCode, theUpperBound);
}
public:
NCollection_LocalArray<long, 10> index;
ListNode *Objects;
};
//! Returns hash code for the given cell, in the range [1, theUpperBound]
//! @param theCell the cell object which hash code is to be computed
//! @param theUpperBound the upper bound of the range a computing hash code must be within
//! @return a computed hash code, in the range [1, theUpperBound]
friend Standard_Integer HashCode (const Cell& theCell, const Standard_Integer theUpperBound)
{
return theCell.HashCode (theUpperBound);
}
// definition of global functions is needed for map
friend Standard_Integer HashCode (const Cell &aCell, const Standard_Integer theUpper)
{ return aCell.HashCode(theUpper); }
friend Standard_Boolean IsEqual (const Cell &aCell1, const Cell &aCell2)
{ return aCell1.IsEqual(aCell2); }

View File

@@ -23,11 +23,15 @@
//purpose : Function is required to call the global function HashCode.
//=======================================================================
template <class TheKeyType>
inline Standard_Integer HashCode_Proxy (const TheKeyType& theKey,
const Standard_Integer Upper)
//! Returns hash code for the given key, in the range [1, theUpperBound]
//! @tparam TheKeyType the type of the given key
//! @param theKey the key which hash code is to be computed
//! @param theUpperBound the upper bound of the range a computing hash code must be within
//! @return a computed hash code, in the range [1, theUpperBound]
template <class TheKeyType>
inline Standard_Integer HashCode_Proxy (const TheKeyType& theKey, const Standard_Integer theUpperBound)
{
return HashCode (theKey, Upper);
return HashCode (theKey, theUpperBound);
}
//=======================================================================
@@ -65,11 +69,15 @@ inline Standard_Boolean IsEqual_Proxy (const TheKeyType& theKey1,
*/
template <class TheKeyType> class NCollection_DefaultHasher {
public:
//
static Standard_Integer HashCode(const TheKeyType& theKey,
const Standard_Integer Upper) {
return HashCode_Proxy(theKey, Upper);
//! Returns hash code for the given key, in the range [1, theUpperBound]
//! @param theKey the key which hash code is to be computed
//! @param theUpperBound the upper bound of the range a computing hash code must be within
//! @return a computed hash code, in the range [1, theUpperBound]
static Standard_Integer HashCode (const TheKeyType& theKey, const Standard_Integer theUpperBound)
{
return HashCode_Proxy (theKey, theUpperBound);
}
//
static Standard_Boolean IsEqual(const TheKeyType& theKey1,
const TheKeyType& theKey2) {