diff --git a/parser/html/javasrc/TreeBuilder.java b/parser/html/javasrc/TreeBuilder.java
index f651306ea3e..57d58b8588f 100644
--- a/parser/html/javasrc/TreeBuilder.java
+++ b/parser/html/javasrc/TreeBuilder.java
@@ -336,6 +336,8 @@ public abstract class TreeBuilder implements TokenHandler,
private static final int NOT_FOUND_ON_STACK = Integer.MAX_VALUE;
+ private static final int AAA_MAX_ITERATIONS = 10;
+
// [NOCPP[
private static final @Local String HTML_LOCAL = "html";
@@ -4334,7 +4336,7 @@ public abstract class TreeBuilder implements TokenHandler,
private void adoptionAgencyEndTag(@Local String name) throws SAXException {
// If you crash around here, perhaps some stack node variable claimed to
// be a weak ref isn't.
- for (;;) {
+ for (int i = 0; i < AAA_MAX_ITERATIONS; ++i) {
int formattingEltListPos = listPtr;
while (formattingEltListPos > -1) {
StackNode listNode = listOfActiveFormattingElements[formattingEltListPos]; // weak
@@ -4410,7 +4412,7 @@ public abstract class TreeBuilder implements TokenHandler,
int bookmark = formattingEltListPos;
int nodePos = furthestBlockPos;
StackNode lastNode = furthestBlock; // weak ref
- for (;;) {
+ for (int j = 0; j < AAA_MAX_ITERATIONS; ++j) {
nodePos--;
StackNode node = stack[nodePos]; // weak ref
int nodeListPos = findInListOfActiveFormattingElements(node);
diff --git a/parser/html/nsHtml5TreeBuilder.cpp b/parser/html/nsHtml5TreeBuilder.cpp
index c7b861f270c..319a985975c 100644
--- a/parser/html/nsHtml5TreeBuilder.cpp
+++ b/parser/html/nsHtml5TreeBuilder.cpp
@@ -3180,7 +3180,7 @@ nsHtml5TreeBuilder::removeFromListOfActiveFormattingElements(PRInt32 pos)
void
nsHtml5TreeBuilder::adoptionAgencyEndTag(nsIAtom* name)
{
- for (; ; ) {
+ for (PRInt32 i = 0; i < NS_HTML5TREE_BUILDER_AAA_MAX_ITERATIONS; ++i) {
PRInt32 formattingEltListPos = listPtr;
while (formattingEltListPos > -1) {
nsHtml5StackNode* listNode = listOfActiveFormattingElements[formattingEltListPos];
@@ -3238,7 +3238,7 @@ nsHtml5TreeBuilder::adoptionAgencyEndTag(nsIAtom* name)
PRInt32 bookmark = formattingEltListPos;
PRInt32 nodePos = furthestBlockPos;
nsHtml5StackNode* lastNode = furthestBlock;
- for (; ; ) {
+ for (PRInt32 j = 0; j < NS_HTML5TREE_BUILDER_AAA_MAX_ITERATIONS; ++j) {
nodePos--;
nsHtml5StackNode* node = stack[nodePos];
PRInt32 nodeListPos = findInListOfActiveFormattingElements(node);
diff --git a/parser/html/nsHtml5TreeBuilder.h b/parser/html/nsHtml5TreeBuilder.h
index 6db06fc9932..54adac0abc9 100644
--- a/parser/html/nsHtml5TreeBuilder.h
+++ b/parser/html/nsHtml5TreeBuilder.h
@@ -350,6 +350,7 @@ jArray nsHtml5TreeBuilder::QUIRKY_PUBLIC_IDS = nsnull;
#define NS_HTML5TREE_BUILDER_CHARSET_DOUBLE_QUOTED 10
#define NS_HTML5TREE_BUILDER_CHARSET_UNQUOTED 11
#define NS_HTML5TREE_BUILDER_NOT_FOUND_ON_STACK PR_INT32_MAX
+#define NS_HTML5TREE_BUILDER_AAA_MAX_ITERATIONS 10
#endif