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
+10 -7
View File
@@ -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);
}