CHECK() that content layout item lists do not get tangled.

Diagnostic for something hit in another (broken) CL, which has
uncovered a place where lists get tangled.  Fix that, too.

-- Add CHECK_NE() to logging.h while we're at it.

Change-Id: Id71029f78d43ef2cd08a83ab07bc1c5c2aa93c4a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/64532
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/third_party/base/logging.h b/third_party/base/logging.h
index 0081a44..9c0039d 100644
--- a/third_party/base/logging.h
+++ b/third_party/base/logging.h
@@ -117,7 +117,9 @@
 #endif
 
 #define CHECK_EQ(x, y) CHECK((x) == (y))
+#define CHECK_NE(x, y) CHECK((x) != (y))
 #define DCHECK_EQ(x, y) DCHECK((x) == (y))
+#define DCHECK_NE(x, y) DCHECK((x) != (y))
 #define NOTREACHED() DCHECK(false)
 
 #endif  // THIRD_PARTY_BASE_LOGGING_H_
diff --git a/xfa/fxfa/layout/cxfa_contentlayoutitem.cpp b/xfa/fxfa/layout/cxfa_contentlayoutitem.cpp
index 2e9bd23..b6e4545 100644
--- a/xfa/fxfa/layout/cxfa_contentlayoutitem.cpp
+++ b/xfa/fxfa/layout/cxfa_contentlayoutitem.cpp
@@ -42,6 +42,7 @@
 }
 
 void CXFA_ContentLayoutItem::InsertAfter(CXFA_ContentLayoutItem* pItem) {
+  CHECK_NE(this, pItem);
   pItem->RemoveSelf();
   pItem->m_pNext = m_pNext;
   pItem->m_pPrev = this;
diff --git a/xfa/fxfa/layout/cxfa_contentlayoutprocessor.cpp b/xfa/fxfa/layout/cxfa_contentlayoutprocessor.cpp
index 7b62264..833ab99 100644
--- a/xfa/fxfa/layout/cxfa_contentlayoutprocessor.cpp
+++ b/xfa/fxfa/layout/cxfa_contentlayoutprocessor.cpp
@@ -1571,9 +1571,11 @@
         if (!pLayoutNext->GetNextSibling() && m_pCurChildPreprocessor &&
             m_pCurChildPreprocessor->GetFormNode() ==
                 pLayoutNext->GetFormNode()) {
-          if (m_pCurChildPreprocessor->m_pLayoutItem)
+          if (m_pCurChildPreprocessor->m_pLayoutItem &&
+              m_pCurChildPreprocessor->m_pLayoutItem != pLayoutNext) {
             pLayoutNext->InsertAfter(
                 m_pCurChildPreprocessor->m_pLayoutItem.Get());
+          }
           m_pCurChildPreprocessor->m_pLayoutItem.Reset(pLayoutNext);
           break;
         }