1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-09-08 14:17:06 +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

@@ -679,10 +679,13 @@ friend Standard_EXPORT Standard_IStream& operator >> (Standard_IStream& astream,
//! aString.Value(2) returns 'e'
Standard_EXPORT Standard_Character Value (const Standard_Integer where) const;
//! Hash function for AsciiString
//! (returns the same Integer value that the hash function for ExtendedString)
static Standard_Integer HashCode (const TCollection_AsciiString& astring, const Standard_Integer Upper);
//! Computes a hash code for the given ASCII string, in the range [1, theUpperBound].
//! Returns the same integer value as the hash function for TCollection_ExtendedString
//! @param theAsciiString the ASCII string 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 TCollection_AsciiString& theAsciiString, Standard_Integer theUpperBound);
//! Returns True when the two strings are the same.
//! (Just for HashCode for AsciiString)
static Standard_Boolean IsEqual (const TCollection_AsciiString& string1, const TCollection_AsciiString& string2);

View File

@@ -15,14 +15,15 @@
#include <Standard_OutOfRange.hxx>
#include <Standard_CString.hxx>
//definition global methods for using in NCollection
//------------------------------------------------------------------------
// HashCode
//------------------------------------------------------------------------
inline Standard_Integer HashCode(const TCollection_AsciiString& astring,
const Standard_Integer Upper)
// definition global methods for using in NCollection
//! Computes a hash code for the given ASCII string, in the range [1, theUpperBound]
//! @param theAsciiString the ASCII string 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]
inline Standard_Integer HashCode (const TCollection_AsciiString& theAsciiString, const Standard_Integer theUpperBound)
{
return TCollection_AsciiString::HashCode(astring,Upper);
return TCollection_AsciiString::HashCode (theAsciiString, theUpperBound);
}
@@ -78,13 +79,14 @@ inline TCollection_AsciiString TCollection_AsciiString::Cat(const Standard_Real
return TCollection_AsciiString( *this , TCollection_AsciiString(other) ) ;
}
//------------------------------------------------------------------------
// HashCode
//------------------------------------------------------------------------
inline Standard_Integer TCollection_AsciiString::HashCode(const TCollection_AsciiString& astring,
const Standard_Integer Upper)
//============================================================================
// function : HashCode
// purpose :
//============================================================================
inline Standard_Integer TCollection_AsciiString::HashCode (const TCollection_AsciiString& theAsciiString,
const Standard_Integer theUpperBound)
{
return ::HashCode(astring.ToCString(),Upper);
return ::HashCode (theAsciiString.ToCString(), theAsciiString.Length(), theUpperBound);
}

View File

@@ -335,13 +335,16 @@ friend Standard_EXPORT Standard_OStream& operator << (Standard_OStream& astream,
//! the bounds of this extended string.
Standard_EXPORT Standard_ExtCharacter Value (const Standard_Integer where) const;
//! Returns a hashed value for the extended string within the range 1..theUpper.
//! Returns a hashed value for the extended string within the range 1 .. theUpper.
//! Note: if string is ASCII, the computed value is the same as the value computed with the HashCode function on a
//! TCollection_AsciiString string composed with equivalent ASCII characters.
//! @param theExtendedString the extended string 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 TCollection_ExtendedString& theString,
const Standard_Integer theUpper)
const Standard_Integer theUpperBound)
{
return ::HashCode (theString.ToExtString(), theUpper);
return ::HashCode (theString.ToExtString(), theUpperBound);
}
//! Returns true if the characters in this extended
@@ -376,11 +379,14 @@ private:
};
//! Compute hash code for extended string
inline Standard_Integer HashCode (const TCollection_ExtendedString& theString,
const Standard_Integer theUpper)
//! Computes a hash code for the given extended string, in the range [1, theUpperBound]
//! @param theExtendedString the extended string 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]
inline Standard_Integer HashCode (const TCollection_ExtendedString& theExtendedString,
const Standard_Integer theUpperBound)
{
return TCollection_ExtendedString::HashCode (theString, theUpper);
return TCollection_ExtendedString::HashCode (theExtendedString, theUpperBound);
}
#endif // _TCollection_ExtendedString_HeaderFile