Saner allocation of JBig2_DocumentContext

Avoid passing owning uniuqe_ptr as an out parameter.

Change-Id: Iedaccd286734d2b6135df5b17439b3bcf2b4eadb
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/77050
Reviewed-by: Hui Yingst <nigi@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp
index 6678267..2430183 100644
--- a/core/fpdfapi/page/cpdf_dib.cpp
+++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -348,8 +348,8 @@
         nGlobalObjNum = m_pGlobalAcc->GetStream()->GetObjNum();
     }
     iDecodeStatus = Jbig2Decoder::StartDecode(
-        m_pJbig2Context.get(), m_pDocument->CodecContext(), m_Width, m_Height,
-        pSrcSpan, nSrcObjNum, pGlobalSpan, nGlobalObjNum,
+        m_pJbig2Context.get(), m_pDocument->GetOrCreateCodecContext(), m_Width,
+        m_Height, pSrcSpan, nSrcObjNum, pGlobalSpan, nGlobalObjNum,
         m_pCachedBitmap->GetBuffer(), m_pCachedBitmap->GetPitch(), pPause);
   } else {
     iDecodeStatus = Jbig2Decoder::ContinueDecode(m_pJbig2Context.get(), pPause);
diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp
index 62106a7..7ab4f87 100644
--- a/core/fpdfapi/parser/cpdf_document.cpp
+++ b/core/fpdfapi/parser/cpdf_document.cpp
@@ -303,6 +303,12 @@
   m_PageList[iPage] = objNum;
 }
 
+JBig2_DocumentContext* CPDF_Document::GetOrCreateCodecContext() {
+  if (!m_pCodecContext)
+    m_pCodecContext = std::make_unique<JBig2_DocumentContext>();
+  return m_pCodecContext.get();
+}
+
 int CPDF_Document::GetPageIndex(uint32_t objnum) {
   uint32_t skip_count = 0;
   bool bSkipped = false;
diff --git a/core/fpdfapi/parser/cpdf_document.h b/core/fpdfapi/parser/cpdf_document.h
index 56168e9..6b1335e 100644
--- a/core/fpdfapi/parser/cpdf_document.h
+++ b/core/fpdfapi/parser/cpdf_document.h
@@ -105,9 +105,7 @@
 
   void SetPageObjNum(int iPage, uint32_t objNum);
 
-  std::unique_ptr<JBig2_DocumentContext>* CodecContext() {
-    return &m_pCodecContext;
-  }
+  JBig2_DocumentContext* GetOrCreateCodecContext();
   LinkListIface* GetLinksContext() const { return m_pLinksContext.get(); }
   void SetLinksContext(std::unique_ptr<LinkListIface> pContext) {
     m_pLinksContext = std::move(pContext);
diff --git a/core/fxcodec/jbig2/jbig2_decoder.cpp b/core/fxcodec/jbig2/jbig2_decoder.cpp
index f757195..5896fa8 100644
--- a/core/fxcodec/jbig2/jbig2_decoder.cpp
+++ b/core/fxcodec/jbig2/jbig2_decoder.cpp
@@ -31,13 +31,6 @@
 
 }  // namespace
 
-JBig2_DocumentContext* GetJBig2DocumentContext(
-    std::unique_ptr<JBig2_DocumentContext>* pContextHolder) {
-  if (!*pContextHolder)
-    *pContextHolder = std::make_unique<JBig2_DocumentContext>();
-  return pContextHolder->get();
-}
-
 Jbig2Context::Jbig2Context() = default;
 
 Jbig2Context::~Jbig2Context() = default;
@@ -45,7 +38,7 @@
 // static
 FXCODEC_STATUS Jbig2Decoder::StartDecode(
     Jbig2Context* pJbig2Context,
-    std::unique_ptr<JBig2_DocumentContext>* pContextHolder,
+    JBig2_DocumentContext* pJBig2DocumentContext,
     uint32_t width,
     uint32_t height,
     pdfium::span<const uint8_t> src_span,
@@ -55,10 +48,6 @@
     uint8_t* dest_buf,
     uint32_t dest_pitch,
     PauseIndicatorIface* pPause) {
-  ASSERT(pJbig2Context);
-
-  JBig2_DocumentContext* pJBig2DocumentContext =
-      GetJBig2DocumentContext(pContextHolder);
   pJbig2Context->m_width = width;
   pJbig2Context->m_height = height;
   pJbig2Context->m_pSrcSpan = src_span;
diff --git a/core/fxcodec/jbig2/jbig2_decoder.h b/core/fxcodec/jbig2/jbig2_decoder.h
index 97867ca..c5a7ff7 100644
--- a/core/fxcodec/jbig2/jbig2_decoder.h
+++ b/core/fxcodec/jbig2/jbig2_decoder.h
@@ -37,18 +37,17 @@
 
 class Jbig2Decoder {
  public:
-  static FXCODEC_STATUS StartDecode(
-      Jbig2Context* pJbig2Context,
-      std::unique_ptr<JBig2_DocumentContext>* pContextHolder,
-      uint32_t width,
-      uint32_t height,
-      pdfium::span<const uint8_t> src_span,
-      uint32_t src_objnum,
-      pdfium::span<const uint8_t> global_span,
-      uint32_t global_objnum,
-      uint8_t* dest_buf,
-      uint32_t dest_pitch,
-      PauseIndicatorIface* pPause);
+  static FXCODEC_STATUS StartDecode(Jbig2Context* pJbig2Context,
+                                    JBig2_DocumentContext* pDocumentContext,
+                                    uint32_t width,
+                                    uint32_t height,
+                                    pdfium::span<const uint8_t> src_span,
+                                    uint32_t src_objnum,
+                                    pdfium::span<const uint8_t> global_span,
+                                    uint32_t global_objnum,
+                                    uint8_t* dest_buf,
+                                    uint32_t dest_pitch,
+                                    PauseIndicatorIface* pPause);
 
   static FXCODEC_STATUS ContinueDecode(Jbig2Context* pJbig2Context,
                                        PauseIndicatorIface* pPause);
diff --git a/testing/fuzzers/pdf_codec_jbig2_fuzzer.cc b/testing/fuzzers/pdf_codec_jbig2_fuzzer.cc
index 88e288b..000a7b7 100644
--- a/testing/fuzzers/pdf_codec_jbig2_fuzzer.cc
+++ b/testing/fuzzers/pdf_codec_jbig2_fuzzer.cc
@@ -35,7 +35,7 @@
     return 0;
 
   Jbig2Context jbig2_context;
-  std::unique_ptr<JBig2_DocumentContext> document_context;
+  JBig2_DocumentContext document_context;
   FXCODEC_STATUS status = Jbig2Decoder::StartDecode(
       &jbig2_context, &document_context, width, height, {data, size}, 1, {}, 0,
       bitmap->GetBuffer(), bitmap->GetPitch(), nullptr);