mirror of
https://github.com/Open-Cascade-SAS/OCCT.git
synced 2026-08-06 21:46:12 +08: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:
@@ -13,19 +13,22 @@
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#include <Standard_ExtString.hxx>
|
||||
#include <Standard_Type.hxx>
|
||||
#include <Standard_OStream.hxx>
|
||||
#include <Standard_Type.hxx>
|
||||
|
||||
Standard_Integer HashCode (const Standard_ExtString Value,
|
||||
const Standard_Integer Upper)
|
||||
//============================================================================
|
||||
// function : HashCode
|
||||
// purpose :
|
||||
//============================================================================
|
||||
Standard_Integer HashCode (const Standard_ExtString theExtString, const Standard_Integer theUpperBound)
|
||||
{
|
||||
// compute SDBM hash of an ext string
|
||||
Standard_Integer hash = 0;
|
||||
for (const Standard_ExtCharacter *c = Value; *c; c++)
|
||||
unsigned int hash = 0;
|
||||
|
||||
for (const Standard_ExtCharacter* c = theExtString; *c; ++c)
|
||||
{
|
||||
/* hash = hash * 33 ^ c */
|
||||
hash = (*c) + (hash << 6) + (hash << 16) - hash;
|
||||
}
|
||||
|
||||
return HashCode (hash, Upper);
|
||||
return HashCode (hash, theUpperBound);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user