diff --git a/content/base/src/nsNodeIterator.cpp b/content/base/src/nsNodeIterator.cpp index 5ed96bdda1a..bcfb60f7c00 100644 --- a/content/base/src/nsNodeIterator.cpp +++ b/content/base/src/nsNodeIterator.cpp @@ -94,9 +94,14 @@ PRBool nsNodeIterator::NodePointer::MoveToPrevious(nsINode *aRoot) return PR_TRUE; } -void nsNodeIterator::NodePointer::AdjustAfterInsertion(nsINode *aContainer, PRInt32 aIndexInContainer) +void nsNodeIterator::NodePointer::AdjustAfterInsertion(nsINode *aRoot, + nsINode *aContainer, + PRInt32 aIndexInContainer) { - if (!mNode) + // If mNode is null or the root there is nothing to do. This also prevents + // valgrind from complaining about consuming uninitialized memory for + // mNodeParent and mIndexInParent + if (!mNode || mNode == aRoot) return; // check if earlier sibling was added @@ -104,21 +109,19 @@ void nsNodeIterator::NodePointer::AdjustAfterInsertion(nsINode *aContainer, PRIn mIndexInParent++; } -void nsNodeIterator::NodePointer::AdjustAfterRemoval(nsINode* aRoot, +void nsNodeIterator::NodePointer::AdjustAfterRemoval(nsINode *aRoot, nsINode *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer) { - if (!mNode) + // If mNode is null or the root there is nothing to do. This also prevents + // valgrind from complaining about consuming uninitialized memory for + // mNodeParent and mIndexInParent + if (!mNode || mNode == aRoot) return; // Check if earlier sibling was removed. - // The mNode != aRoot check isn't strictly needed because if mNode is the - // root, we'll never use mNodeParent or mIndexInParent. However without the - // check valgrind will (rightly) complain about reading uninitialized - // memory. - if (mNode != aRoot && - aContainer == mNodeParent && aIndexInContainer < mIndexInParent) { + if (aContainer == mNodeParent && aIndexInContainer < mIndexInParent) { --mIndexInParent; return; } @@ -368,15 +371,15 @@ NS_IMETHODIMP nsNodeIterator::GetPointerBeforeReferenceNode(PRBool *aBeforeNode) * nsIMutationObserver interface */ -void nsNodeIterator::ContentInserted(nsIDocument* aDocument, - nsIContent* aContainer, - nsIContent* aChild, +void nsNodeIterator::ContentInserted(nsIDocument *aDocument, + nsIContent *aContainer, + nsIContent *aChild, PRInt32 aIndexInContainer) { nsINode *container = NODE_FROM(aContainer, aDocument); - mPointer.AdjustAfterInsertion(container, aIndexInContainer); - mWorkingPointer.AdjustAfterInsertion(container, aIndexInContainer); + mPointer.AdjustAfterInsertion(mRoot, container, aIndexInContainer); + mWorkingPointer.AdjustAfterInsertion(mRoot, container, aIndexInContainer); } diff --git a/content/base/src/nsNodeIterator.h b/content/base/src/nsNodeIterator.h index a5661048c3e..14b654af21b 100644 --- a/content/base/src/nsNodeIterator.h +++ b/content/base/src/nsNodeIterator.h @@ -83,9 +83,9 @@ private: PRBool MoveForward(nsINode *aRoot, nsINode *aParent, PRInt32 aChildNum); void MoveBackward(nsINode *aParent, PRInt32 aChildNum); - void AdjustAfterInsertion(nsINode *aContainer, PRInt32 aIndexInContainer); + void AdjustAfterInsertion(nsINode *aRoot, nsINode *aContainer, PRInt32 aIndexInContainer); void AdjustAfterRemoval(nsINode *aRoot, nsINode *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer); - + void Clear() { mNode = nsnull; } nsINode *mNode;