Bug 211276 nsStaticCaseInsensitiveNameTable::Init doesn't check the return value of PL_DHashTableInit Last

r=alecf sr=dbaron


git-svn-id: svn://10.0.0.236/trunk@144846 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
timeless%mozdev.org
2003-07-14 21:21:54 +00:00
parent 6d7e99d5a4
commit 77aea2f7b3

View File

@@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
@@ -18,7 +18,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
@@ -45,21 +45,20 @@
struct nameTableEntry : public PLDHashEntryHdr
{
// no ownership here!
const char *mKey;
PRInt32 mIndex;
// no ownership here!
const char *mKey;
PRInt32 mIndex;
};
PR_STATIC_CALLBACK(PRBool)
matchNameKeysCaseInsensitive(PLDHashTable*, const PLDHashEntryHdr* aHdr,
const void* key)
{
const nameTableEntry* entry =
NS_STATIC_CAST(const nameTableEntry *, aHdr);
const char *keyValue = NS_STATIC_CAST(const char*, key);
return (nsCRT::strcasecmp(entry->mKey, keyValue)==0);
const nameTableEntry* entry =
NS_STATIC_CAST(const nameTableEntry *, aHdr);
const char *keyValue = NS_STATIC_CAST(const char*, key);
return (nsCRT::strcasecmp(entry->mKey, keyValue)==0);
}
/*
@@ -73,20 +72,21 @@ matchNameKeysCaseInsensitive(PLDHashTable*, const PLDHashEntryHdr* aHdr,
PR_STATIC_CALLBACK(PLDHashNumber)
caseInsensitiveStringHashKey(PLDHashTable *table, const void *key)
{
PLDHashNumber h = 0;
for (const unsigned char* s =
NS_STATIC_CAST(const unsigned char*, key); *s != '\0'; s++)
h = (h >> (PL_DHASH_BITS - 4)) ^ (h << 4) ^ (*s & ~0x20);
return h;
PLDHashNumber h = 0;
for (const unsigned char* s =
NS_STATIC_CAST(const unsigned char*, key);
*s != '\0';
s++)
h = (h >> (PL_DHASH_BITS - 4)) ^ (h << 4) ^ (*s & ~0x20);
return h;
}
PR_STATIC_CALLBACK(const void*)
getNameKey(PLDHashTable*, PLDHashEntryHdr* aHdr)
{
nameTableEntry* entry =
NS_STATIC_CAST(nameTableEntry*, aHdr);
nameTableEntry* entry = NS_STATIC_CAST(nameTableEntry*, aHdr);
return entry->mKey;
return entry->mKey;
}
static const struct PLDHashTableOps nametable_CaseInsensitiveHashTableOps = {
@@ -101,53 +101,60 @@ static const struct PLDHashTableOps nametable_CaseInsensitiveHashTableOps = {
nsnull,
};
nsStaticCaseInsensitiveNameTable::nsStaticCaseInsensitiveNameTable()
: mNameArray(nsnull), mNullStr("")
{
MOZ_COUNT_CTOR(nsStaticCaseInsensitiveNameTable);
mNameTable.ops = nsnull;
}
}
nsStaticCaseInsensitiveNameTable::~nsStaticCaseInsensitiveNameTable()
{
// manually call the destructor on placement-new'ed objects
for (PRUint32 index = 0; index < mNameTable.entryCount; index++) {
mNameArray[index].~nsDependentCString();
if (mNameArray) {
// manually call the destructor on placement-new'ed objects
for (PRUint32 index = 0; index < mNameTable.entryCount; index++) {
mNameArray[index].~nsDependentCString();
}
nsMemory::Free((void*)mNameArray);
}
nsMemory::Free((void*)mNameArray);
PL_DHashTableFinish(&mNameTable);
if (mNameTable.ops)
PL_DHashTableFinish(&mNameTable);
MOZ_COUNT_DTOR(nsStaticCaseInsensitiveNameTable);
}
}
PRBool
nsStaticCaseInsensitiveNameTable::Init(const char* const aNames[], PRInt32 Count)
{
NS_ASSERTION(!mNameArray, "double Init");
NS_ASSERTION(!mNameTable.ops, "double Init");
NS_ASSERTION(!mNameArray, "double Init");
NS_ASSERTION(!mNameTable.ops, "double Init");
NS_ASSERTION(aNames, "null name table");
NS_ASSERTION(Count, "0 count");
mNameArray = (nsDependentCString*)nsMemory::Alloc(Count * sizeof(nsDependentCString));
PL_DHashTableInit(&mNameTable, &nametable_CaseInsensitiveHashTableOps,
nsnull, sizeof(nameTableEntry), Count);
if (!mNameArray || !mNameTable.ops) {
mNameArray = (nsDependentCString*)
nsMemory::Alloc(Count * sizeof(nsDependentCString));
if (!mNameArray)
return PR_FALSE;
if (!PL_DHashTableInit(&mNameTable,
&nametable_CaseInsensitiveHashTableOps,
nsnull, sizeof(nameTableEntry), Count)) {
mNameTable.ops = nsnull;
return PR_FALSE;
}
for (PRInt32 index = 0; index < Count; ++index) {
const char* raw = aNames[index];
#ifdef DEBUG
{
// verify invariants of contents
nsCAutoString temp1(raw);
nsDependentCString temp2(raw);
ToLowerCase(temp1);
NS_ASSERTION(temp1.Equals(temp2), "upper case char in table");
NS_ASSERTION(nsCRT::IsAscii(raw),
"non-ascii string in table -- "
"case-insensitive matching won't work right");
}
{
// verify invariants of contents
nsCAutoString temp1(raw);
nsDependentCString temp2(raw);
ToLowerCase(temp1);
NS_ASSERTION(temp1.Equals(temp2), "upper case char in table");
NS_ASSERTION(nsCRT::IsAscii(raw),
"non-ascii string in table -- "
"case-insensitive matching won't work right");
}
#endif
// use placement-new to initialize the string object
new (&mNameArray[index]) nsDependentCString(raw);
@@ -155,61 +162,59 @@ nsStaticCaseInsensitiveNameTable::Init(const char* const aNames[], PRInt32 Count
nameTableEntry *entry =
NS_STATIC_CAST(nameTableEntry*,
PL_DHashTableOperate(&mNameTable, raw, PL_DHASH_ADD));
if (!entry) continue;
NS_ASSERTION(entry->mKey == 0, "Entry already exists!");
entry->mKey = raw; // not owned!
entry->mIndex = index;
}
return PR_TRUE;
}
}
inline PRInt32
LookupFlatKeyword(const nsAFlatCString& aKeyword,
LookupFlatKeyword(const nsAFlatCString& aKeyword,
PLDHashTable& aTable)
{
nameTableEntry *entry =
NS_STATIC_CAST(nameTableEntry*,
PL_DHashTableOperate(&aTable, aKeyword.get(), PL_DHASH_LOOKUP));
if (!entry || PL_DHASH_ENTRY_IS_FREE(entry))
return nsStaticCaseInsensitiveNameTable::NOT_FOUND;
return nsStaticCaseInsensitiveNameTable::NOT_FOUND;
return entry->mIndex;
}
}
PRInt32
nsStaticCaseInsensitiveNameTable::Lookup(const nsACString& aName)
{
NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited");
NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited");
return LookupFlatKeyword(PromiseFlatCString(aName), mNameTable);
}
}
PRInt32
nsStaticCaseInsensitiveNameTable::Lookup(const nsAString& aName)
{
NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited");
NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited");
nsCAutoString cstring;
cstring.AssignWithConversion(aName);
return LookupFlatKeyword(cstring, mNameTable);
}
}
const nsAFlatCString&
nsStaticCaseInsensitiveNameTable::GetStringValue(PRInt32 index)
{
NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited");
NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited");
if ((NOT_FOUND < index) && ((PRUint32)index < mNameTable.entryCount)) {
return mNameArray[index];
} else {
return mNullStr;
}
}
return mNullStr;
}