diff --git a/content/html/content/src/nsHTMLSelectElement.cpp b/content/html/content/src/nsHTMLSelectElement.cpp
index 0664f323e97..67ec5c072d3 100644
--- a/content/html/content/src/nsHTMLSelectElement.cpp
+++ b/content/html/content/src/nsHTMLSelectElement.cpp
@@ -1024,13 +1024,15 @@ nsHTMLSelectElement::Add(nsIDOMHTMLElement* aElement,
}
nsCOMPtr ancestor(parent);
+ nsCOMPtr temp;
while (ancestor != NS_STATIC_CAST(nsIDOMNode*, this)) {
- ancestor->GetParentNode(getter_AddRefs(ancestor));
- if (!ancestor) {
+ ancestor->GetParentNode(getter_AddRefs(temp));
+ if (!temp) {
// NOT_FOUND_ERR: Raised if before is not a descendant of the SELECT
// element.
return NS_ERROR_DOM_NOT_FOUND_ERR;
}
+ temp.swap(ancestor);
}
// If the before parameter is not null, we are equivalent to the