Bug 487018 Switch to nsCString and fix other allocator mismatches r=timeless sr=dveditz
authorNeil Rashbrook <neil@parkwaycc.co.uk>
Tue, 07 Apr 2009 10:24:58 +0100
changeset 27026 1fd2edfa2d49902f8a123a635ad695b95512a0d8
parent 27025 39115c97b792fe341b5ef2758c57878dbfa4075c
child 27027 19672932377eae08877ec239a366baa3d57e3561
push idunknown
push userunknown
push dateunknown
reviewerstimeless, dveditz
bugs487018
milestone1.9.2a1pre
Bug 487018 Switch to nsCString and fix other allocator mismatches r=timeless sr=dveditz
modules/libjar/nsJAR.cpp
modules/libjar/nsJAR.h
modules/libjar/nsWildCard.cpp
modules/libjar/nsZipArchive.cpp
--- a/modules/libjar/nsJAR.cpp
+++ b/modules/libjar/nsJAR.cpp
@@ -79,39 +79,34 @@ public:
   // True if the second step of verification (VerifyEntry) 
   // has taken place:
   PRBool              entryVerified;
   
   // Not signed, valid, or failure code
   PRInt16             status;
   
   // Internal storage of digests
-  char*               calculatedSectionDigest;
-  char*               storedEntryDigest;
+  nsCString           calculatedSectionDigest;
+  nsCString           storedEntryDigest;
 
   nsJARManifestItem();
   virtual ~nsJARManifestItem();
 };
 
 //-------------------------------------------------
 // nsJARManifestItem constructors and destructor
 //-------------------------------------------------
 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);
 }
 
 //----------------------------------------------
 // nsJAR constructor/destructor
 //----------------------------------------------
 static PRBool
 DeleteManifestEntry(nsHashKey* aKey, void* aData, void* closure)
 {
@@ -705,17 +700,17 @@ nsJAR::ParseOneFile(const char* filebuf,
         }
 
         if (curItemMF->mType == JAR_INVALID)
           delete curItemMF;
         else //-- calculate section digest
         {
           PRUint32 sectionLength = curPos - sectionStart;
           CalculateDigest(sectionStart, sectionLength,
-                          &(curItemMF->calculatedSectionDigest));
+                          curItemMF->calculatedSectionDigest);
           //-- Save item in the hashtable
           nsCStringKey itemKey(curItemName);
           mManifestData.Put(&itemKey, (void*)curItemMF);
         }
         if (nextLineStart == nsnull) // end-of-file
           break;
 
         sectionStart = nextLineStart;
@@ -737,20 +732,20 @@ nsJAR::ParseOneFile(const char* filebuf,
                          "SECURITY ERROR: nsJARManifestItem not correctly initialized");
             curItemSF->status = mGlobalStatus;
             if (curItemSF->status == JAR_VALID_MANIFEST)
             { // Compare digests
               if (storedSectionDigest.IsEmpty())
                 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)
         } // if(foundName)
 
         if(nextLineStart == nsnull) // end-of-file
           break;
       } // aFileType == JAR_SF
@@ -776,77 +771,66 @@ nsJAR::ParseOneFile(const char* filebuf,
     //-- Break down the line
     nsCAutoString lineName;
     curLine.Left(lineName, colonPos);
     nsCAutoString lineData;
     curLine.Mid(lineData, colonPos+2, linelen - (colonPos+2));
 
     //-- 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;
       continue;
     }
 
     // (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;
       continue;
     }
 
   } // for (;;)
   return NS_OK;
 } //ParseOneFile()
 
 nsresult
 nsJAR::VerifyEntry(nsJARManifestItem* aManItem, const char* aEntryData,
                    PRUint32 aLen)
 {
   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;
   return NS_OK;
 }
 
 void nsJAR::ReportError(const char* aFilename, PRInt16 errorCode)
 {
@@ -893,38 +877,30 @@ void nsJAR::ReportError(const char* aFil
   if (!messageCstr) return;
   fprintf(stderr, "%s\n", messageCstr);
   nsMemory::Free(messageCstr);
 #endif
 }
 
 
 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;
 
   rv = hasher->Init(nsICryptoHash::SHA1);
   if (NS_FAILED(rv)) return rv;
 
   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)
   
 //----------------------------------------------
 // nsJAREnumerator::HasMore
 //----------------------------------------------
 NS_IMETHODIMP
--- a/modules/libjar/nsJAR.h
+++ b/modules/libjar/nsJAR.h
@@ -159,17 +159,17 @@ class nsJAR : public nsIZipReader, publi
     nsresult LoadEntry(const char* aFilename, char** aBuf, 
                        PRUint32* aBufLen = nsnull);
     PRInt32  ReadLine(const char** src); 
     nsresult ParseOneFile(const char* filebuf, PRInt16 aFileType);
     nsresult VerifyEntry(nsJARManifestItem* aEntry, const char* aEntryData, 
                          PRUint32 aLen);
 
     nsresult CalculateDigest(const char* aInBuf, PRUint32 aInBufLen,
-                             char** digest);
+                             nsCString& digest);
 
     //-- Debugging
     void DumpMetadata(const char* aMessage);
 };
 
 /**
  * nsJARItem
  *
--- a/modules/libjar/nsWildCard.cpp
+++ b/modules/libjar/nsWildCard.cpp
@@ -285,16 +285,16 @@ NS_WildCardMatch(char *str, char *xp, PR
         if((expr[x] == '~') && (expr[x-1] != '\\')) {
             expr[x] = '\0';
             if(_shexp_match(str,&expr[++x], case_insensitive) == MATCH)
                 goto punt;
             break;
         }
     }
     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;
 }
--- a/modules/libjar/nsZipArchive.cpp
+++ b/modules/libjar/nsZipArchive.cpp
@@ -740,17 +740,17 @@ nsZipArchive::FindInit(const char * aPat
 
     pattern = PL_strdup(aPattern);
     if (!pattern)
       return ZIP_ERR_MEMORY;
   }
 
   *aFind = new nsZipFind(this, pattern, regExp);
   if (!*aFind) {
-    PR_FREEIF(pattern);
+    PL_strfree(pattern);
     return ZIP_ERR_MEMORY;
   }
 
   return ZIP_OK;
 }
 
 
 
@@ -1343,17 +1343,17 @@ nsZipFind::nsZipFind(nsZipArchive* aZip,
   mSlot(0),
   mRegExp(aRegExp)
 {
   MOZ_COUNT_CTOR(nsZipFind);
 }
 
 nsZipFind::~nsZipFind()
 {
-  PR_FREEIF(mPattern);
+  PL_strfree(mPattern);
 
   MOZ_COUNT_DTOR(nsZipFind);
 }
 
 //------------------------------------------
 // helper functions
 //------------------------------------------