Bug 225578 crash [@ nsAbView::GetSelectedAddresses(nsIArray**)] does not check for NULL pointer. r/sr=bienvenu,a=test fire drill COMM192_20100119_RELBRANCH
authorMark Banner <bugzilla@standard8.plus.com>
Wed, 24 Feb 2010 16:57:27 +0000
branchCOMM192_20100119_RELBRANCH
changeset 5014 c1bb767189c16c28d3839b6cb983e629fb7e34d3
parent 4812 5e3c86b43e80fc91227069220b08a7bd607795ff
child 5016 18d21af1c15cbf30be5105f6695aa74ec978131b
push idunknown
push userunknown
push dateunknown
reviewerstest
bugs225578
Bug 225578 crash [@ nsAbView::GetSelectedAddresses(nsIArray**)] does not check for NULL pointer. r/sr=bienvenu,a=test fire drill
mailnews/addrbook/src/nsAbView.cpp
mailnews/addrbook/src/nsAbView.h
--- a/mailnews/addrbook/src/nsAbView.cpp
+++ b/mailnews/addrbook/src/nsAbView.cpp
@@ -694,18 +694,20 @@ NS_IMETHODIMP nsAbView::SortBy(const PRU
     if (!sortDir)
       sortDirection = NS_LITERAL_STRING("ascending").get();  // default direction
     else
       sortDirection = sortDir;
 
     SortClosure closure;
     SetSortClosure(sortColumn.get(), sortDirection.get(), this, &closure);
     
-    nsCOMPtr<nsIArray> selectedCards;
-    rv = GetSelectedCards(getter_AddRefs(selectedCards));
+    nsCOMPtr<nsIMutableArray> selectedCards = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = GetSelectedCards(selectedCards);
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIAbCard> indexCard;
 
     if (mTreeSelection) {
       PRInt32 currentIndex = -1;
 
       rv = mTreeSelection->GetCurrentIndex(&currentIndex);
@@ -1073,16 +1075,20 @@ nsresult nsAbView::ReselectCards(nsIArra
     return NS_OK;
 
   nsresult rv = mTreeSelection->ClearSelection();
   NS_ENSURE_SUCCESS(rv,rv);
 
   rv = aCards->GetLength(&count);
   NS_ENSURE_SUCCESS(rv, rv);
 
+  // If we don't have any cards selected, nothing else to do.
+  if (!count)
+    return NS_OK;
+
   for (i = 0; i < count; i++) {
     nsCOMPtr<nsIAbCard> card = do_QueryElementAt(aCards, i);
     if (card) {
       PRInt32 index = FindIndexForCard(card);
       if (index != CARD_NOT_FOUND) {
         mTreeSelection->RangedSelect(index, index, PR_TRUE /* augment */);
       }
     }
@@ -1100,68 +1106,64 @@ nsresult nsAbView::ReselectCards(nsIArra
     }
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP nsAbView::DeleteSelectedCards()
 {
-  nsCOMPtr<nsIArray> cardsToDelete;
+  nsresult rv;
+  nsCOMPtr<nsIMutableArray> cardsToDelete = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
   
-  nsresult rv = GetSelectedCards(getter_AddRefs(cardsToDelete));
+  rv = GetSelectedCards(cardsToDelete);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // mDirectory should not be null
   // Bullet proof (and assert) to help figure out bug #127748
   NS_ENSURE_TRUE(mDirectory, NS_ERROR_UNEXPECTED);
 
   rv = mDirectory->DeleteCards(cardsToDelete);
   NS_ENSURE_SUCCESS(rv, rv);
   return rv;
 }
 
-nsresult nsAbView::GetSelectedCards(nsIArray **aSelectedCards)
+nsresult nsAbView::GetSelectedCards(nsCOMPtr<nsIMutableArray> &aSelectedCards)
 {
-  *aSelectedCards = nsnull;
   if (!mTreeSelection)
     return NS_OK;
 
   PRInt32 selectionCount; 
   nsresult rv = mTreeSelection->GetRangeCount(&selectionCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (!selectionCount)
     return NS_OK;
 
-  nsCOMPtr<nsIMutableArray> result(do_CreateInstance(NS_ARRAY_CONTRACTID, &rv));
-  NS_ENSURE_SUCCESS(rv,rv);
-  
   for (PRInt32 i = 0; i < selectionCount; i++)
   {
     PRInt32 startRange;
     PRInt32 endRange;
     rv = mTreeSelection->GetRangeAt(i, &startRange, &endRange);
     NS_ENSURE_SUCCESS(rv, NS_OK); 
     PRInt32 totalCards = mCards.Count();
     if (startRange >= 0 && startRange < totalCards)
     {
       for (PRInt32 rangeIndex = startRange; rangeIndex <= endRange && rangeIndex < totalCards; rangeIndex++) {
         nsCOMPtr<nsIAbCard> abCard;
         rv = GetCardFromRow(rangeIndex, getter_AddRefs(abCard));
         NS_ENSURE_SUCCESS(rv,rv);
         
-        rv = result->AppendElement(abCard, PR_FALSE);
+        rv = aSelectedCards->AppendElement(abCard, PR_FALSE);
         NS_ENSURE_SUCCESS(rv, rv);
       }
     }
   }
 
-  NS_ADDREF(*aSelectedCards = result);
-
   return NS_OK;
 }
 
 NS_IMETHODIMP nsAbView::SwapFirstNameLastName()
 {
   if (!mTreeSelection)
     return NS_OK;
   
@@ -1291,22 +1293,26 @@ NS_IMETHODIMP nsAbView::SwapFirstNameLas
   rv = RefreshTree();
 
   return rv;
 }
 
 NS_IMETHODIMP nsAbView::GetSelectedAddresses(nsIArray **_retval)
 {
   NS_ENSURE_ARG_POINTER(_retval);
- 
-  nsCOMPtr<nsIArray> selectedCards;
-  nsresult rv = GetSelectedCards(getter_AddRefs(selectedCards));
+
+  nsresult rv;
+  nsCOMPtr<nsIMutableArray> selectedCards = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIMutableArray> addresses = do_CreateInstance(NS_ARRAY_CONTRACTID);
+  rv = GetSelectedCards(selectedCards);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCOMPtr<nsIMutableArray> addresses = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
   PRUint32 count;
   selectedCards->GetLength(&count);
 
   for (PRUint32 i = 0; i < count; i++) {
     nsCOMPtr<nsIAbCard> card(do_QueryElementAt(selectedCards, i, &rv));
     NS_ENSURE_SUCCESS(rv, rv);
 
     PRBool isMailList;
--- a/mailnews/addrbook/src/nsAbView.h
+++ b/mailnews/addrbook/src/nsAbView.h
@@ -86,17 +86,17 @@ private:
   PRInt32 FindIndexForCard(nsIAbCard *card);
   nsresult GenerateCollationKeysForCard(const PRUnichar *colID, AbCard *abcard);
   nsresult InvalidateTree(PRInt32 row);
   nsresult RemoveCardAt(PRInt32 row);
   nsresult AddCard(AbCard *abcard, PRBool selectCardAfterAdding, PRInt32 *index);
   nsresult RemoveCardAndSelectNextCard(nsISupports *item);
   nsresult EnumerateCards();
   nsresult SetGeneratedNameFormatFromPrefs();
-  nsresult GetSelectedCards(nsIArray **aSelectedCards);
+  nsresult GetSelectedCards(nsCOMPtr<nsIMutableArray> &aSelectedCards);
   nsresult ReselectCards(nsIArray *aCards, nsIAbCard *aIndexCard);
   nsresult GetCardValue(nsIAbCard *card, const PRUnichar *colID, nsAString &_retval);
   nsresult RefreshTree();
 
   nsCOMPtr<nsITreeBoxObject> mTree;
   nsCOMPtr<nsITreeSelection> mTreeSelection;
   nsCOMPtr <nsIAbDirectory> mDirectory;
   nsVoidArray mCards;