Hi,

I just ran into the weird libjpeg default behavior of calling "exit()" when
encountering an error - clearly not the cleanest shutdown when used from
within the podofo library :)

It seems you already handle this in PdfImage.cpp by providing a custom
error-handling routing (raising an exception instead of calling exit() ),
but the filter code in PdfFiltersPrivate.cpp also makes use of libjpeg for
decoding/decompression, but it does _not_ use this custom error-handling
routine.

I'm a bit surprised that you don't use the same behavior there as well - if
there is a particular reason, could you please let me know why? Otherwise,
please see the attached patch that moves the custom error-handling logic
from PdfImage.cpp to the base-src (into PdfFiltersPrivate.cpp), and
registers the handler also during the decoding/decompression in the filter.

Unfortunately I cannot provide an example file triggering an error, since
I'm still hunting this down - wanted to hear some early feedback (but the
provided patch is tested and working, just not actually calling the
error-handling routine in my test inputs).

Thanks!
-Clemens

-- 
Clemens Kolbitsch
Security Researcher
kolbit...@lastline.com
805-456-7075

Lastline, Inc.
6950 Hollister Avenue, Suite 101
Goleta, CA 93117

www.lastline.com
From db964b5fedede24265541b933ce6f05605ade528 Mon Sep 17 00:00:00 2001
From: Clemens Kolbitsch <kolbit...@lastline.com>
Date: Wed, 11 Nov 2015 20:43:13 -0800
Subject: [PATCH] libjpeg error handling: raise exception instead of calling exit()

---
 podofo-0.9.3/src/base/PdfFiltersPrivate.cpp | 25 +++++++++++++++++++++++++
 podofo-0.9.3/src/base/PdfFiltersPrivate.h   |  6 ++++++
 podofo-0.9.3/src/doc/PdfImage.cpp           | 19 -------------------
 3 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/podofo-0.9.3/src/base/PdfFiltersPrivate.cpp b/podofo-0.9.3/src/base/PdfFiltersPrivate.cpp
index 8cbddd8..5b1a2fa 100644
--- a/podofo-0.9.3/src/base/PdfFiltersPrivate.cpp
+++ b/podofo-0.9.3/src/base/PdfFiltersPrivate.cpp
@@ -882,6 +882,28 @@ void PdfLZWFilter::InitTable()
 #ifdef PODOFO_HAVE_JPEG_LIB
 
 /*
+ * Handlers for errors inside the JPeg library
+ */
+extern "C" {
+void JPegErrorExit(j_common_ptr cinfo)
+{
+#if 1
+    char buffer[JMSG_LENGTH_MAX];
+
+    /* Create the message */
+    (*cinfo->err->format_message) (cinfo, buffer);
+#endif
+    jpeg_destroy(cinfo);
+    PODOFO_RAISE_ERROR_INFO( ePdfError_UnsupportedImageFormat, buffer);
+}
+
+void JPegErrorOutput(j_common_ptr, int)
+{
+}
+
+};
+
+/*
  * The actual filter implementation
  */
 PdfDCTFilter::PdfDCTFilter()
@@ -912,6 +934,9 @@ void PdfDCTFilter::BeginDecodeImpl( const PdfDictionary* )
 { 
     // Setup variables for JPEGLib
     m_cinfo.err = jpeg_std_error( &m_jerr );
+    m_jerr.error_exit = &JPegErrorExit;
+    m_jerr.emit_message = &JPegErrorOutput;
+
 
     jpeg_create_decompress( &m_cinfo );
 
diff --git a/podofo-0.9.3/src/base/PdfFiltersPrivate.h b/podofo-0.9.3/src/base/PdfFiltersPrivate.h
index 7ba01dc..5d025ce 100644
--- a/podofo-0.9.3/src/base/PdfFiltersPrivate.h
+++ b/podofo-0.9.3/src/base/PdfFiltersPrivate.h
@@ -705,6 +705,12 @@ EPdfFilter PdfLZWFilter::GetType() const
 
 void PODOFO_API jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize);
 
+extern "C" {
+void JPegErrorExit(j_common_ptr cinfo);
+
+void JPegErrorOutput(j_common_ptr, int);
+};
+
 /** The DCT filter can decoded JPEG compressed data.
  *  
  *  This filter requires JPEG lib to be available
diff --git a/podofo-0.9.3/src/doc/PdfImage.cpp b/podofo-0.9.3/src/doc/PdfImage.cpp
index 1ca251f..f293870 100644
--- a/podofo-0.9.3/src/doc/PdfImage.cpp
+++ b/podofo-0.9.3/src/doc/PdfImage.cpp
@@ -254,25 +254,6 @@ void PdfImage::LoadFromFile( const char* pszFilename )
 }
 
 #ifdef PODOFO_HAVE_JPEG_LIB
-extern "C" {
-static void JPegErrorExit(j_common_ptr cinfo)
-{
-#if 1	
-    char buffer[JMSG_LENGTH_MAX];
-
-    /* Create the message */
-    (*cinfo->err->format_message) (cinfo, buffer);
-#endif
-    jpeg_destroy(cinfo);
-    PODOFO_RAISE_ERROR_INFO( ePdfError_UnsupportedImageFormat, buffer);
-}
-
-static void JPegErrorOutput(j_common_ptr, int)
-{
-}
-
-};
-
 
 void PdfImage::LoadFromJpeg( const char* pszFilename )
     {
-- 
2.5.0

------------------------------------------------------------------------------
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to