1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-04 13:13:25 +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

@@ -32,11 +32,12 @@ public:
DEFINE_STANDARD_ALLOC
//! Returns a HasCode value for the Key <K> in the
//! range 0..Upper.
static Standard_Integer HashCode (const IntTools_CurveRangeSample& K, const Standard_Integer Upper);
//! Computes a 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 IntTools_CurveRangeSample& theKey, const Standard_Integer theUpperBound);
//! Returns True when the two keys are the same. Two
//! same keys must have the same hashcode, the
//! contrary is not necessary.

View File

@@ -13,11 +13,16 @@
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
inline Standard_Integer IntTools_CurveRangeSampleMapHasher::HashCode(const IntTools_CurveRangeSample& K,
const Standard_Integer Upper) {
return (K.GetDepth() % Upper);
//=======================================================================
// function : HashCode
// purpose :
//=======================================================================
inline Standard_Integer IntTools_CurveRangeSampleMapHasher::HashCode (const IntTools_CurveRangeSample& theKey,
const Standard_Integer theUpperBound)
{
return ::HashCode(theKey.GetDepth(), theUpperBound);
}
inline Standard_Boolean IntTools_CurveRangeSampleMapHasher::IsEqual(const IntTools_CurveRangeSample& S1,
const IntTools_CurveRangeSample& S2) {
return S1.IsEqual(S2);

View File

@@ -32,11 +32,12 @@ public:
DEFINE_STANDARD_ALLOC
//! Returns a HasCode value for the Key <K> in the
//! range 0..Upper.
static Standard_Integer HashCode (const IntTools_SurfaceRangeSample& K, const Standard_Integer Upper);
//! Computes a 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 IntTools_SurfaceRangeSample& theKey, Standard_Integer theUpperBound);
//! Returns True when the two keys are the same. Two
//! same keys must have the same hashcode, the
//! contrary is not necessary.

View File

@@ -13,12 +13,17 @@
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
inline Standard_Integer IntTools_SurfaceRangeSampleMapHasher::HashCode(const IntTools_SurfaceRangeSample& K,
const Standard_Integer Upper) {
// return (((K.GetDepthU() % Upper) ^ (K.GetDepthV() % Upper)) % Upper);
return ((K.GetIndexU() * K.GetIndexV()) % Upper);
//=======================================================================
// function : HashCode
// purpose :
//=======================================================================
inline Standard_Integer IntTools_SurfaceRangeSampleMapHasher::HashCode (const IntTools_SurfaceRangeSample& theKey,
const Standard_Integer theUpperBound)
{
// return (((K.GetDepthU() % Upper) ^ (K.GetDepthV() % Upper)) % Upper);
return ::HashCode (theKey.GetIndexU() * theKey.GetIndexV(), theUpperBound);
}
inline Standard_Boolean IntTools_SurfaceRangeSampleMapHasher::IsEqual(const IntTools_SurfaceRangeSample& S1,
const IntTools_SurfaceRangeSample& S2) {
return S1.IsEqual(S2);