Simplify loop in core/fpdfapi/page/cpdf_page.cpp

Avoid inserting into a set where possible at the cost of a
lookup in an empty set (which should be very cheap).

Change-Id: I9c384b488ebf3bd85592c6398f8198a4991669ab
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/94570
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp
index 4f11c91..5aad0f1 100644
--- a/core/fpdfapi/page/cpdf_page.cpp
+++ b/core/fpdfapi/page/cpdf_page.cpp
@@ -75,16 +75,15 @@
 }
 
 CPDF_Object* CPDF_Page::GetPageAttr(const ByteString& name) const {
-  CPDF_Dictionary* pPageDict = GetDict();
   std::set<CPDF_Dictionary*> visited;
-  while (true) {
-    visited.insert(pPageDict);
-    if (CPDF_Object* pObj = pPageDict->GetDirectObjectFor(name))
+  CPDF_Dictionary* pPageDict = GetDict();
+  while (pPageDict && !pdfium::Contains(visited, pPageDict)) {
+    CPDF_Object* pObj = pPageDict->GetDirectObjectFor(name);
+    if (pObj)
       return pObj;
 
+    visited.insert(pPageDict);
     pPageDict = pPageDict->GetDictFor(pdfium::page_object::kParent);
-    if (!pPageDict || pdfium::Contains(visited, pPageDict))
-      break;
   }
   return nullptr;
 }