Bug 487018 Switch to nsCString and fix other allocator mismatches r=timeless sr=dveditz

This commit is contained in:
Neil Rashbrook 2009-04-07 10:24:58 +01:00
parent c0e5a3886c
commit 8f638a751b
4 changed files with 24 additions and 48 deletions

View File

@ -84,8 +84,8 @@ public:
PRInt16 status;
// Internal storage of digests
char* calculatedSectionDigest;
char* storedEntryDigest;
nsCString calculatedSectionDigest;
nsCString storedEntryDigest;
nsJARManifestItem();
virtual ~nsJARManifestItem();
@ -96,17 +96,12 @@ public:
//-------------------------------------------------
nsJARManifestItem::nsJARManifestItem(): mType(JAR_INTERNAL),
entryVerified(PR_FALSE),
status(JAR_NOT_SIGNED),
calculatedSectionDigest(nsnull),
storedEntryDigest(nsnull)
status(JAR_NOT_SIGNED)
{
}
nsJARManifestItem::~nsJARManifestItem()
{
// Delete digests if necessary
PR_FREEIF(calculatedSectionDigest);
PR_FREEIF(storedEntryDigest);
}
//----------------------------------------------
@ -710,7 +705,7 @@ nsJAR::ParseOneFile(const char* filebuf, PRInt16 aFileType)
{
PRUint32 sectionLength = curPos - sectionStart;
CalculateDigest(sectionStart, sectionLength,
&(curItemMF->calculatedSectionDigest));
curItemMF->calculatedSectionDigest);
//-- Save item in the hashtable
nsCStringKey itemKey(curItemName);
mManifestData.Put(&itemKey, (void*)curItemMF);
@ -742,10 +737,10 @@ nsJAR::ParseOneFile(const char* filebuf, PRInt16 aFileType)
curItemSF->status = JAR_NOT_SIGNED;
else
{
if (!storedSectionDigest.Equals((const char*)curItemSF->calculatedSectionDigest))
if (!storedSectionDigest.Equals(curItemSF->calculatedSectionDigest))
curItemSF->status = JAR_INVALID_MANIFEST;
PR_FREEIF(curItemSF->calculatedSectionDigest)
storedSectionDigest = "";
curItemSF->calculatedSectionDigest.Truncate();
storedSectionDigest.Truncate();
}
} // (aPrincipal != nsnull)
} // if(curItemSF)
@ -781,25 +776,18 @@ nsJAR::ParseOneFile(const char* filebuf, PRInt16 aFileType)
//-- Lines to look for:
// (1) Digest:
if (lineName.Equals(NS_LITERAL_CSTRING("SHA1-Digest"),
nsCaseInsensitiveCStringComparator()))
if (lineName.LowerCaseEqualsLiteral("sha1-digest"))
//-- This is a digest line, save the data in the appropriate place
{
if(aFileType == JAR_MF)
{
curItemMF->storedEntryDigest = (char*)PR_MALLOC(lineData.Length()+1);
if (!(curItemMF->storedEntryDigest))
return NS_ERROR_OUT_OF_MEMORY;
PL_strcpy(curItemMF->storedEntryDigest, lineData.get());
}
curItemMF->storedEntryDigest = lineData;
else
storedSectionDigest = lineData;
continue;
}
// (2) Name: associates this manifest section with a file in the jar.
if (!foundName && lineName.Equals(NS_LITERAL_CSTRING("Name"),
nsCaseInsensitiveCStringComparator()))
if (!foundName && lineName.LowerCaseEqualsLiteral("name"))
{
curItemName = lineData;
foundName = PR_TRUE;
@ -808,12 +796,9 @@ nsJAR::ParseOneFile(const char* filebuf, PRInt16 aFileType)
// (3) Magic: this may be an inline Javascript.
// We can't do any other kind of magic.
if ( aFileType == JAR_MF &&
lineName.Equals(NS_LITERAL_CSTRING("Magic"),
nsCaseInsensitiveCStringComparator()))
if (aFileType == JAR_MF && lineName.LowerCaseEqualsLiteral("magic"))
{
if(lineData.Equals(NS_LITERAL_CSTRING("javascript"),
nsCaseInsensitiveCStringComparator()))
if (lineData.LowerCaseEqualsLiteral("javascript"))
curItemMF->mType = JAR_EXTERNAL;
else
curItemMF->mType = JAR_INVALID;
@ -830,18 +815,17 @@ nsJAR::VerifyEntry(nsJARManifestItem* aManItem, const char* aEntryData,
{
if (aManItem->status == JAR_VALID_MANIFEST)
{
if(!aManItem->storedEntryDigest)
if (aManItem->storedEntryDigest.IsEmpty())
// No entry digests in manifest file. Entry is unsigned.
aManItem->status = JAR_NOT_SIGNED;
else
{ //-- Calculate and compare digests
char* calculatedEntryDigest;
nsresult rv = CalculateDigest(aEntryData, aLen, &calculatedEntryDigest);
nsCString calculatedEntryDigest;
nsresult rv = CalculateDigest(aEntryData, aLen, calculatedEntryDigest);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
if (PL_strcmp(aManItem->storedEntryDigest, calculatedEntryDigest) != 0)
if (!aManItem->storedEntryDigest.Equals(calculatedEntryDigest))
aManItem->status = JAR_INVALID_ENTRY;
PR_FREEIF(calculatedEntryDigest)
PR_FREEIF(aManItem->storedEntryDigest)
aManItem->storedEntryDigest.Truncate();
}
}
aManItem->entryVerified = PR_TRUE;
@ -898,11 +882,9 @@ void nsJAR::ReportError(const char* aFilename, PRInt16 errorCode)
nsresult nsJAR::CalculateDigest(const char* aInBuf, PRUint32 aLen,
char** digest)
nsCString& digest)
{
*digest = nsnull;
nsresult rv;
nsCOMPtr<nsICryptoHash> hasher = do_CreateInstance("@mozilla.org/security/hash;1", &rv);
if (NS_FAILED(rv)) return rv;
@ -913,13 +895,7 @@ nsresult nsJAR::CalculateDigest(const char* aInBuf, PRUint32 aLen,
rv = hasher->Update((const PRUint8*) aInBuf, aLen);
if (NS_FAILED(rv)) return rv;
nsCAutoString hashString;
rv = hasher->Finish(PR_TRUE, hashString);
if (NS_FAILED(rv)) return rv;
*digest = ToNewCString(hashString);
return *digest ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
return hasher->Finish(PR_TRUE, digest);
}
NS_IMPL_THREADSAFE_ISUPPORTS1(nsJAREnumerator, nsIUTF8StringEnumerator)

View File

@ -164,7 +164,7 @@ class nsJAR : public nsIZipReader, public nsIJAR
PRUint32 aLen);
nsresult CalculateDigest(const char* aInBuf, PRUint32 aInBufLen,
char** digest);
nsCString& digest);
//-- Debugging
void DumpMetadata(const char* aMessage);

View File

@ -290,11 +290,11 @@ NS_WildCardMatch(char *str, char *xp, PRBool case_insensitive) {
}
}
if(_shexp_match(str,expr, case_insensitive) == MATCH) {
PR_Free(expr);
PL_strfree(expr);
return 0;
}
punt:
PR_Free(expr);
PL_strfree(expr);
return 1;
}

View File

@ -745,7 +745,7 @@ nsZipArchive::FindInit(const char * aPattern, nsZipFind **aFind)
*aFind = new nsZipFind(this, pattern, regExp);
if (!*aFind) {
PR_FREEIF(pattern);
PL_strfree(pattern);
return ZIP_ERR_MEMORY;
}
@ -1348,7 +1348,7 @@ nsZipFind::nsZipFind(nsZipArchive* aZip, char* aPattern, PRBool aRegExp) :
nsZipFind::~nsZipFind()
{
PR_FREEIF(mPattern);
PL_strfree(mPattern);
MOZ_COUNT_DTOR(nsZipFind);
}