Move setjmp calls out of progressive_decoder.cpp, part 3.

Convert JpegProgressiveDecoder APIs to no longer longjmp to callers.
Instead, catch these above JpegProgressiveDecoder and return false.

Last one in series.

-- Remove now-unused GetJumpMark() method.

Change-Id: I1837ba66f47f3a823a067e62c4746023c344b2bf
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/126530
Reviewed-by: Thomas Sepez <tsepez@google.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcodec/jpeg/jpeg_progressive_decoder.cpp b/core/fxcodec/jpeg/jpeg_progressive_decoder.cpp
index c42bd1a..7f2d9ca 100644
--- a/core/fxcodec/jpeg/jpeg_progressive_decoder.cpp
+++ b/core/fxcodec/jpeg/jpeg_progressive_decoder.cpp
@@ -24,8 +24,6 @@
   CJpegContext();
   ~CJpegContext() override;
 
-  jmp_buf& GetJumpMark() { return m_Common.jmpbuf; }
-
   JpegCommon m_Common = {};
 };
 
@@ -96,11 +94,6 @@
 }
 
 // static
-jmp_buf& JpegProgressiveDecoder::GetJumpMark(Context* pContext) {
-  return static_cast<CJpegContext*>(pContext)->GetJumpMark();
-}
-
-// static
 int JpegProgressiveDecoder::ReadHeader(Context* pContext,
                                        int* width,
                                        int* height,
@@ -134,11 +127,14 @@
 }
 
 // static
-bool JpegProgressiveDecoder::ReadScanline(Context* pContext,
-                                          unsigned char* dest_buf) {
+int JpegProgressiveDecoder::ReadScanline(Context* pContext,
+                                         unsigned char* dest_buf) {
   auto* ctx = static_cast<CJpegContext*>(pContext);
-  unsigned int nlines = jpeg_read_scanlines(&ctx->m_Common.cinfo, &dest_buf, 1);
-  return nlines == 1;
+  int nlines = jpeg_common_read_scanlines(&ctx->m_Common, &dest_buf, 1);
+  if (nlines == -1) {
+    return kFatal;
+  }
+  return nlines == 1 ? kOk : kError;
 }
 
 FX_FILESIZE JpegProgressiveDecoder::GetAvailInput(Context* pContext) const {
diff --git a/core/fxcodec/jpeg/jpeg_progressive_decoder.h b/core/fxcodec/jpeg/jpeg_progressive_decoder.h
index 2568e6b..f3e41aa 100644
--- a/core/fxcodec/jpeg/jpeg_progressive_decoder.h
+++ b/core/fxcodec/jpeg/jpeg_progressive_decoder.h
@@ -7,8 +7,6 @@
 #ifndef CORE_FXCODEC_JPEG_JPEG_PROGRESSIVE_DECODER_H_
 #define CORE_FXCODEC_JPEG_JPEG_PROGRESSIVE_DECODER_H_
 
-#include <setjmp.h>
-
 #include <memory>
 
 #include "core/fxcodec/progressive_decoder_iface.h"
@@ -23,9 +21,8 @@
   static void DestroyGlobals();
   static JpegProgressiveDecoder* GetInstance();
   static std::unique_ptr<Context> Start();
-  static jmp_buf& GetJumpMark(Context* pContext);
 
-  // Result codes for ReadHeader():
+  // Result codes for ReadHeader()/ReadScanline():
   static constexpr int kFatal = -1;
   static constexpr int kOk = 0;
   static constexpr int kError = 1;
@@ -38,7 +35,7 @@
                         CFX_DIBAttribute* pAttribute);
 
   static bool StartScanline(Context* pContext);
-  static bool ReadScanline(Context* pContext, uint8_t* dest_buf);
+  static int ReadScanline(Context* pContext, uint8_t* dest_buf);
 
   // ProgressiveDecoderIface:
   FX_FILESIZE GetAvailInput(Context* pContext) const override;
diff --git a/core/fxcodec/progressive_decoder.cpp b/core/fxcodec/progressive_decoder.cpp
index ec32e1c..f90017b 100644
--- a/core/fxcodec/progressive_decoder.cpp
+++ b/core/fxcodec/progressive_decoder.cpp
@@ -564,28 +564,24 @@
 }
 
 FXCODEC_STATUS ProgressiveDecoder::JpegContinueDecode() {
-  // JpegModule* pJpegModule = m_pCodecMgr->GetJpegModule();
-  // Setting jump marker before calling ReadScanLine, since a longjmp to
-  // the marker indicates a fatal error.
-  if (setjmp(JpegProgressiveDecoder::GetJumpMark(m_pJpegContext.get())) == -1) {
-    m_pJpegContext.reset();
-    m_status = FXCODEC_STATUS::kError;
-    return FXCODEC_STATUS::kError;
-  }
-
   while (true) {
-    bool readRes = JpegProgressiveDecoder::ReadScanline(m_pJpegContext.get(),
+    int err_code = JpegProgressiveDecoder::ReadScanline(m_pJpegContext.get(),
                                                         m_DecodeBuf.data());
-    while (!readRes) {
+    if (err_code == JpegProgressiveDecoder::kFatal) {
+      m_pJpegContext.reset();
+      m_status = FXCODEC_STATUS::kError;
+      return FXCODEC_STATUS::kError;
+    }
+    if (err_code != JpegProgressiveDecoder::kOk) {
+      // Maybe it needs more data.
       FXCODEC_STATUS error_status = FXCODEC_STATUS::kDecodeFinished;
-      if (!JpegReadMoreData(&error_status)) {
-        m_pDeviceBitmap = nullptr;
-        m_pFile = nullptr;
-        m_status = error_status;
-        return m_status;
+      if (JpegReadMoreData(&error_status)) {
+        continue;
       }
-      readRes = JpegProgressiveDecoder::ReadScanline(m_pJpegContext.get(),
-                                                     m_DecodeBuf.data());
+      m_pDeviceBitmap = nullptr;
+      m_pFile = nullptr;
+      m_status = error_status;
+      return m_status;
     }
     if (m_SrcFormat == FXCodec_Rgb) {
       RGB2BGR(UNSAFE_TODO(m_DecodeBuf.data()), m_SrcWidth);