Make FPDF_RenderPage() return a bool

Bug: 42270418
Change-Id: I39034c7f6ea5fd061d1cd79b42d01f5511afa871
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/124230
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@google.com>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index 53e0d47..7b0caf7 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -570,17 +570,17 @@
 
 }  // namespace
 
-FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc,
-                                               FPDF_PAGE page,
-                                               int start_x,
-                                               int start_y,
-                                               int size_x,
-                                               int size_y,
-                                               int rotate,
-                                               int flags) {
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_RenderPage(HDC dc,
+                                                    FPDF_PAGE page,
+                                                    int start_x,
+                                                    int start_y,
+                                                    int size_x,
+                                                    int size_y,
+                                                    int rotate,
+                                                    int flags) {
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
-    return;
+    return false;
 
   auto owned_context = std::make_unique<CPDF_PageRenderContext>();
   CPDF_PageRenderContext* context = owned_context.get();
@@ -606,11 +606,13 @@
                                   size_y, rotate, flags,
                                   /*color_scheme=*/nullptr,
                                   /*need_to_restore=*/true, /*pause=*/nullptr);
-    return;
+    return true;
   }
 
   RetainPtr<CFX_DIBitmap> pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
-  CHECK(pBitmap->Create(size_x, size_y, FXDIB_Format::kBgra));
+  if (!pBitmap->Create(size_x, size_y, FXDIB_Format::kBgra)) {
+    return false;
+  }
   if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
     // Not needed by Skia. Call it for AGG to preserve pre-existing behavior.
     pBitmap->Clear(0x00ffffff);
@@ -646,7 +648,7 @@
     }
     if (!bitsStretched)
       win_dc.SetDIBits(std::move(pBitmap), 0, 0);
-    return;
+    return true;
   }
 
   // Finish rendering the page to bitmap and copy the correct segments
@@ -672,10 +674,10 @@
   context->m_pOptions->GetOptions().bBreakForMasks = true;
 
   CPDFSDK_RenderPageWithContext(context, pPage, start_x, start_y, size_x,
-                                size_y, rotate, flags, /*color_scheme=*/nullptr,
+                                size_y, rotate, flags,
+                                /*color_scheme=*/nullptr,
                                 /*need_to_restore=*/true,
                                 /*pause=*/nullptr);
-
   // Render masks
   for (size_t i = 0; i < mask_boxes.size(); i++) {
     // Render the bitmap for the mask and free the bitmap.
@@ -686,6 +688,8 @@
     // Render the next portion of page.
     context->m_pRenderer->Continue(nullptr);
   }
+
+  return true;
 }
 #endif  // BUILDFLAG(IS_WIN)
 
diff --git a/public/fpdfview.h b/public/fpdfview.h
index c7b644bc..e3cda00 100644
--- a/public/fpdfview.h
+++ b/public/fpdfview.h
@@ -850,8 +850,9 @@
 //          flags       -   0 for normal display, or combination of flags
 //                          defined above.
 // Return value:
-//          None.
-FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc,
+//          Returns true if the page is rendered successfully, false otherwise.
+
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_RenderPage(HDC dc,
                                                FPDF_PAGE page,
                                                int start_x,
                                                int start_y,
diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp
index dadce90..9e048a9 100644
--- a/testing/embedder_test.cpp
+++ b/testing/embedder_test.cpp
@@ -626,7 +626,7 @@
   // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
   Rectangle(dc, 0, 0, width + 1, height + 1);
 
-  FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
+  CHECK(FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags));
 
   HENHMETAFILE emf = CloseEnhMetaFile(dc);
   UINT size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
diff --git a/testing/helpers/write.cc b/testing/helpers/write.cc
index 951a170..85e1dbe 100644
--- a/testing/helpers/write.cc
+++ b/testing/helpers/write.cc
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include "core/fxcrt/check.h"
 #include "core/fxcrt/notreached.h"
 #include "public/cpp/fpdf_scopers.h"
 #include "public/fpdf_annot.h"
@@ -554,7 +555,8 @@
   // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
   Rectangle(dc, 0, 0, width + 1, height + 1);
 
-  FPDF_RenderPage(dc, page, 0, 0, width, height, 0, FPDF_ANNOT | FPDF_PRINTING);
+  CHECK(FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
+                        FPDF_ANNOT | FPDF_PRINTING));
 
   DeleteEnhMetaFile(CloseEnhMetaFile(dc));
 }
@@ -573,7 +575,8 @@
 
   int width = static_cast<int>(FPDF_GetPageWidthF(page));
   int height = static_cast<int>(FPDF_GetPageHeightF(page));
-  FPDF_RenderPage(dc, page, 0, 0, width, height, 0, FPDF_ANNOT | FPDF_PRINTING);
+  CHECK(FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
+                        FPDF_ANNOT | FPDF_PRINTING));
 
   HENHMETAFILE emf = CloseEnhMetaFile(dc);
   std::vector<const ENHMETARECORD*> items;
diff --git a/testing/pdfium_test.cc b/testing/pdfium_test.cc
index 85b4ca5..95cc95f 100644
--- a/testing/pdfium_test.cc
+++ b/testing/pdfium_test.cc
@@ -1189,9 +1189,13 @@
     CHECK_NE(old_obj, HGDI_ERROR);
 
     // Render into the in-memory DC.
-    FPDF_RenderPage(dc_.Get(), page(), /*start_x=*/0, /*start_y=*/0,
-                    /*size_x=*/width(), /*size_y=*/height(), /*rotate=*/0,
-                    /*flags=*/flags());
+    FPDF_BOOL render_result =
+        FPDF_RenderPage(dc_.Get(), page(), /*start_x=*/0, /*start_y=*/0,
+                        /*size_x=*/width(), /*size_y=*/height(), /*rotate=*/0,
+                        /*flags=*/flags());
+    if (!render_result) {
+      return false;
+    }
 
     bool result = !!GdiFlush();
     HGDIOBJ dib_obj = SelectObject(dc_.Get(), old_obj);