Bug 534136 Part 6: Properly handle invalid UTF8 string being passed to NS_NewAtom/do_GetAtom. r=jst

This commit is contained in:
Jonas Sicking 2010-03-08 07:45:00 -08:00
parent 3cfc98309e
commit 0baab851d8
4 changed files with 26 additions and 9 deletions

View File

@ -107,7 +107,15 @@ AtomTableGetHash(PLDHashTable *table, const void *key)
const AtomTableKey *k = static_cast<const AtomTableKey*>(key);
if (k->mUTF8String) {
return nsCRT::HashCodeAsUTF16(k->mUTF8String, k->mLength);;
PRBool err;
PRUint32 hash = nsCRT::HashCodeAsUTF16(k->mUTF8String, k->mLength, &err);
if (err) {
AtomTableKey* mutableKey = const_cast<AtomTableKey*>(k);
mutableKey->mUTF8String = nsnull;
mutableKey->mLength = 0;
hash = 0;
}
return hash;
}
return nsCRT::HashCode(k->mUTF16String, k->mLength);

View File

@ -253,17 +253,19 @@ PRUint32 nsCRT::HashCode(const PRUnichar* start, PRUint32 length)
return h;
}
PRUint32 nsCRT::HashCodeAsUTF16(const char* start, PRUint32 length)
PRUint32 nsCRT::HashCodeAsUTF16(const char* start, PRUint32 length,
PRBool* err)
{
PRUint32 h = 0;
const char* s = start;
const char* end = start + length;
*err = PR_FALSE;
while ( s < end )
{
PRBool err;
PRUint32 ucs4 = UTF8CharEnumerator::NextChar(&s, end, &err);
if (err) {
PRUint32 ucs4 = UTF8CharEnumerator::NextChar(&s, end, err);
if (*err) {
return 0;
}

View File

@ -242,7 +242,8 @@ public:
// Computes a hashcode for a length number of UTF8
// characters. Returns the same hash code as the HashCode method
// taking a |PRUnichar*| would if the string were converted to UTF16.
static PRUint32 HashCodeAsUTF16(const char* start, PRUint32 length);
static PRUint32 HashCodeAsUTF16(const char* start, PRUint32 length,
PRBool* err);
// String to longlong
static PRInt64 atoll(const char *str);

View File

@ -145,21 +145,27 @@ test_hashas16()
{
for (unsigned int i = 0; i < NS_ARRAY_LENGTH(ValidStrings); ++i) {
nsDependentCString str8(ValidStrings[i].m8);
PRBool err;
if (nsCRT::HashCode(ValidStrings[i].m16) !=
nsCRT::HashCodeAsUTF16(str8.get(), str8.Length()))
nsCRT::HashCodeAsUTF16(str8.get(), str8.Length(), &err) ||
err)
return PR_FALSE;
}
for (unsigned int i = 0; i < NS_ARRAY_LENGTH(Invalid8Strings); ++i) {
nsDependentCString str8(Invalid8Strings[i].m8);
PRBool err;
if (nsCRT::HashCode(Invalid8Strings[i].m16) !=
nsCRT::HashCodeAsUTF16(str8.get(), str8.Length()))
nsCRT::HashCodeAsUTF16(str8.get(), str8.Length(), &err) ||
err)
return PR_FALSE;
}
for (unsigned int i = 0; i < NS_ARRAY_LENGTH(Malformed8Strings); ++i) {
nsDependentCString str8(Malformed8Strings[i]);
if (nsCRT::HashCodeAsUTF16(str8.get(), str8.Length()) != 0)
PRBool err;
if (nsCRT::HashCodeAsUTF16(str8.get(), str8.Length(), &err) != 0 ||
!err)
return PR_FALSE;
}