[iortcw] 486/497: All: Make jpeg loading errors non-fatal

Simon McVittie smcv at debian.org
Fri Sep 8 10:38:02 UTC 2017


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to annotated tag 1.42d
in repository iortcw.

commit fd56996481cc915a14f6bfb114b1cac96cc01d66
Author: MAN-AT-ARMS <M4N4T4RMS at gmail.com>
Date:   Sun Dec 20 16:02:52 2015 -0500

    All: Make jpeg loading errors non-fatal
---
 MP/code/rend2/tr_image_jpg.c    | 49 ++++++++++++++++++++++++++++++-----------
 MP/code/renderer/tr_image_jpg.c | 43 +++++++++++++++++++++++++++++-------
 SP/code/rend2/tr_image_jpg.c    | 49 ++++++++++++++++++++++++++++++-----------
 SP/code/renderer/tr_image_jpg.c | 43 +++++++++++++++++++++++++++++-------
 4 files changed, 142 insertions(+), 42 deletions(-)

diff --git a/MP/code/rend2/tr_image_jpg.c b/MP/code/rend2/tr_image_jpg.c
index abe5e65..0173d9e 100644
--- a/MP/code/rend2/tr_image_jpg.c
+++ b/MP/code/rend2/tr_image_jpg.c
@@ -20,11 +20,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
 
-#include "../qcommon/q_shared.h"
-#include "../qcommon/qfiles.h"
-#include "../qcommon/qcommon.h"
-#include "../renderer/tr_public.h"
-extern	refimport_t		ri;
+#include <setjmp.h>
+
+#include "tr_local.h"
 
 /*
  * Include file for users of JPEG library.
@@ -46,16 +44,27 @@ extern	refimport_t		ri;
 #  endif
 #endif
 
-static void __attribute__((__noreturn__)) R_JPGErrorExit(j_common_ptr cinfo)
+/* Catching errors, as done in libjpeg's example.c */
+typedef struct q_jpeg_error_mgr_s
+{
+  struct jpeg_error_mgr pub;  /* "public" fields */
+
+  jmp_buf setjmp_buffer;  /* for return to caller */
+} q_jpeg_error_mgr_t;
+
+static void R_JPGErrorExit(j_common_ptr cinfo)
 {
   char buffer[JMSG_LENGTH_MAX];
+
+  /* cinfo->err really points to a q_jpeg_error_mgr_s struct, so coerce pointer */
+  q_jpeg_error_mgr_t *jerr = (q_jpeg_error_mgr_t *)cinfo->err;
   
   (*cinfo->err->format_message) (cinfo, buffer);
-  
-  /* Let the memory manager delete any temp files before we die */
-  jpeg_destroy(cinfo);
-  
-  ri.Error(ERR_FATAL, "%s", buffer);
+
+  ri.Printf(PRINT_ALL, "R_LoadJPG() error: %s", buffer);
+
+  /* Return control to the setjmp point */
+  longjmp(jerr->setjmp_buffer, 1);
 }
 
 static void R_JPGOutputMessage(j_common_ptr cinfo)
@@ -87,7 +96,7 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
    * Note that this struct must live as long as the main JPEG parameter
    * struct, to avoid dangling-pointer problems.
    */
-  struct jpeg_error_mgr jerr;
+  q_jpeg_error_mgr_t jerr;
   /* More stuff */
   JSAMPARRAY buffer;		/* Output row buffer */
   unsigned int row_stride;	/* physical row width in output buffer */
@@ -119,10 +128,24 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
    * This routine fills in the contents of struct jerr, and returns jerr's
    * address which we place into the link field in cinfo.
    */
-  cinfo.err = jpeg_std_error(&jerr);
+  cinfo.err = jpeg_std_error(&jerr.pub);
   cinfo.err->error_exit = R_JPGErrorExit;
   cinfo.err->output_message = R_JPGOutputMessage;
 
+  /* Establish the setjmp return context for R_JPGErrorExit to use. */
+  if (setjmp(jerr.setjmp_buffer))
+  {
+    /* If we get here, the JPEG code has signaled an error.
+     * We need to clean up the JPEG object, close the input file, and return.
+     */
+    jpeg_destroy_decompress(&cinfo);
+    ri.FS_FreeFile(fbuffer.v);
+
+    /* Append the filename to the error for easier debugging */
+    ri.Printf(PRINT_ALL, ", file %s\n", filename);
+    return;
+  }
+
   /* Now we can initialize the JPEG decompression object. */
   jpeg_create_decompress(&cinfo);
 
diff --git a/MP/code/renderer/tr_image_jpg.c b/MP/code/renderer/tr_image_jpg.c
index b16b3df..0173d9e 100644
--- a/MP/code/renderer/tr_image_jpg.c
+++ b/MP/code/renderer/tr_image_jpg.c
@@ -20,6 +20,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
 
+#include <setjmp.h>
+
 #include "tr_local.h"
 
 /*
@@ -42,16 +44,27 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #  endif
 #endif
 
-static void __attribute__((__noreturn__)) R_JPGErrorExit(j_common_ptr cinfo)
+/* Catching errors, as done in libjpeg's example.c */
+typedef struct q_jpeg_error_mgr_s
+{
+  struct jpeg_error_mgr pub;  /* "public" fields */
+
+  jmp_buf setjmp_buffer;  /* for return to caller */
+} q_jpeg_error_mgr_t;
+
+static void R_JPGErrorExit(j_common_ptr cinfo)
 {
   char buffer[JMSG_LENGTH_MAX];
+
+  /* cinfo->err really points to a q_jpeg_error_mgr_s struct, so coerce pointer */
+  q_jpeg_error_mgr_t *jerr = (q_jpeg_error_mgr_t *)cinfo->err;
   
   (*cinfo->err->format_message) (cinfo, buffer);
-  
-  /* Let the memory manager delete any temp files before we die */
-  jpeg_destroy(cinfo);
-  
-  ri.Error(ERR_FATAL, "%s", buffer);
+
+  ri.Printf(PRINT_ALL, "R_LoadJPG() error: %s", buffer);
+
+  /* Return control to the setjmp point */
+  longjmp(jerr->setjmp_buffer, 1);
 }
 
 static void R_JPGOutputMessage(j_common_ptr cinfo)
@@ -83,7 +96,7 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
    * Note that this struct must live as long as the main JPEG parameter
    * struct, to avoid dangling-pointer problems.
    */
-  struct jpeg_error_mgr jerr;
+  q_jpeg_error_mgr_t jerr;
   /* More stuff */
   JSAMPARRAY buffer;		/* Output row buffer */
   unsigned int row_stride;	/* physical row width in output buffer */
@@ -115,10 +128,24 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
    * This routine fills in the contents of struct jerr, and returns jerr's
    * address which we place into the link field in cinfo.
    */
-  cinfo.err = jpeg_std_error(&jerr);
+  cinfo.err = jpeg_std_error(&jerr.pub);
   cinfo.err->error_exit = R_JPGErrorExit;
   cinfo.err->output_message = R_JPGOutputMessage;
 
+  /* Establish the setjmp return context for R_JPGErrorExit to use. */
+  if (setjmp(jerr.setjmp_buffer))
+  {
+    /* If we get here, the JPEG code has signaled an error.
+     * We need to clean up the JPEG object, close the input file, and return.
+     */
+    jpeg_destroy_decompress(&cinfo);
+    ri.FS_FreeFile(fbuffer.v);
+
+    /* Append the filename to the error for easier debugging */
+    ri.Printf(PRINT_ALL, ", file %s\n", filename);
+    return;
+  }
+
   /* Now we can initialize the JPEG decompression object. */
   jpeg_create_decompress(&cinfo);
 
diff --git a/SP/code/rend2/tr_image_jpg.c b/SP/code/rend2/tr_image_jpg.c
index abe5e65..0173d9e 100644
--- a/SP/code/rend2/tr_image_jpg.c
+++ b/SP/code/rend2/tr_image_jpg.c
@@ -20,11 +20,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
 
-#include "../qcommon/q_shared.h"
-#include "../qcommon/qfiles.h"
-#include "../qcommon/qcommon.h"
-#include "../renderer/tr_public.h"
-extern	refimport_t		ri;
+#include <setjmp.h>
+
+#include "tr_local.h"
 
 /*
  * Include file for users of JPEG library.
@@ -46,16 +44,27 @@ extern	refimport_t		ri;
 #  endif
 #endif
 
-static void __attribute__((__noreturn__)) R_JPGErrorExit(j_common_ptr cinfo)
+/* Catching errors, as done in libjpeg's example.c */
+typedef struct q_jpeg_error_mgr_s
+{
+  struct jpeg_error_mgr pub;  /* "public" fields */
+
+  jmp_buf setjmp_buffer;  /* for return to caller */
+} q_jpeg_error_mgr_t;
+
+static void R_JPGErrorExit(j_common_ptr cinfo)
 {
   char buffer[JMSG_LENGTH_MAX];
+
+  /* cinfo->err really points to a q_jpeg_error_mgr_s struct, so coerce pointer */
+  q_jpeg_error_mgr_t *jerr = (q_jpeg_error_mgr_t *)cinfo->err;
   
   (*cinfo->err->format_message) (cinfo, buffer);
-  
-  /* Let the memory manager delete any temp files before we die */
-  jpeg_destroy(cinfo);
-  
-  ri.Error(ERR_FATAL, "%s", buffer);
+
+  ri.Printf(PRINT_ALL, "R_LoadJPG() error: %s", buffer);
+
+  /* Return control to the setjmp point */
+  longjmp(jerr->setjmp_buffer, 1);
 }
 
 static void R_JPGOutputMessage(j_common_ptr cinfo)
@@ -87,7 +96,7 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
    * Note that this struct must live as long as the main JPEG parameter
    * struct, to avoid dangling-pointer problems.
    */
-  struct jpeg_error_mgr jerr;
+  q_jpeg_error_mgr_t jerr;
   /* More stuff */
   JSAMPARRAY buffer;		/* Output row buffer */
   unsigned int row_stride;	/* physical row width in output buffer */
@@ -119,10 +128,24 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
    * This routine fills in the contents of struct jerr, and returns jerr's
    * address which we place into the link field in cinfo.
    */
-  cinfo.err = jpeg_std_error(&jerr);
+  cinfo.err = jpeg_std_error(&jerr.pub);
   cinfo.err->error_exit = R_JPGErrorExit;
   cinfo.err->output_message = R_JPGOutputMessage;
 
+  /* Establish the setjmp return context for R_JPGErrorExit to use. */
+  if (setjmp(jerr.setjmp_buffer))
+  {
+    /* If we get here, the JPEG code has signaled an error.
+     * We need to clean up the JPEG object, close the input file, and return.
+     */
+    jpeg_destroy_decompress(&cinfo);
+    ri.FS_FreeFile(fbuffer.v);
+
+    /* Append the filename to the error for easier debugging */
+    ri.Printf(PRINT_ALL, ", file %s\n", filename);
+    return;
+  }
+
   /* Now we can initialize the JPEG decompression object. */
   jpeg_create_decompress(&cinfo);
 
diff --git a/SP/code/renderer/tr_image_jpg.c b/SP/code/renderer/tr_image_jpg.c
index b16b3df..0173d9e 100644
--- a/SP/code/renderer/tr_image_jpg.c
+++ b/SP/code/renderer/tr_image_jpg.c
@@ -20,6 +20,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
 
+#include <setjmp.h>
+
 #include "tr_local.h"
 
 /*
@@ -42,16 +44,27 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #  endif
 #endif
 
-static void __attribute__((__noreturn__)) R_JPGErrorExit(j_common_ptr cinfo)
+/* Catching errors, as done in libjpeg's example.c */
+typedef struct q_jpeg_error_mgr_s
+{
+  struct jpeg_error_mgr pub;  /* "public" fields */
+
+  jmp_buf setjmp_buffer;  /* for return to caller */
+} q_jpeg_error_mgr_t;
+
+static void R_JPGErrorExit(j_common_ptr cinfo)
 {
   char buffer[JMSG_LENGTH_MAX];
+
+  /* cinfo->err really points to a q_jpeg_error_mgr_s struct, so coerce pointer */
+  q_jpeg_error_mgr_t *jerr = (q_jpeg_error_mgr_t *)cinfo->err;
   
   (*cinfo->err->format_message) (cinfo, buffer);
-  
-  /* Let the memory manager delete any temp files before we die */
-  jpeg_destroy(cinfo);
-  
-  ri.Error(ERR_FATAL, "%s", buffer);
+
+  ri.Printf(PRINT_ALL, "R_LoadJPG() error: %s", buffer);
+
+  /* Return control to the setjmp point */
+  longjmp(jerr->setjmp_buffer, 1);
 }
 
 static void R_JPGOutputMessage(j_common_ptr cinfo)
@@ -83,7 +96,7 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
    * Note that this struct must live as long as the main JPEG parameter
    * struct, to avoid dangling-pointer problems.
    */
-  struct jpeg_error_mgr jerr;
+  q_jpeg_error_mgr_t jerr;
   /* More stuff */
   JSAMPARRAY buffer;		/* Output row buffer */
   unsigned int row_stride;	/* physical row width in output buffer */
@@ -115,10 +128,24 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
    * This routine fills in the contents of struct jerr, and returns jerr's
    * address which we place into the link field in cinfo.
    */
-  cinfo.err = jpeg_std_error(&jerr);
+  cinfo.err = jpeg_std_error(&jerr.pub);
   cinfo.err->error_exit = R_JPGErrorExit;
   cinfo.err->output_message = R_JPGOutputMessage;
 
+  /* Establish the setjmp return context for R_JPGErrorExit to use. */
+  if (setjmp(jerr.setjmp_buffer))
+  {
+    /* If we get here, the JPEG code has signaled an error.
+     * We need to clean up the JPEG object, close the input file, and return.
+     */
+    jpeg_destroy_decompress(&cinfo);
+    ri.FS_FreeFile(fbuffer.v);
+
+    /* Append the filename to the error for easier debugging */
+    ri.Printf(PRINT_ALL, ", file %s\n", filename);
+    return;
+  }
+
   /* Now we can initialize the JPEG decompression object. */
   jpeg_create_decompress(&cinfo);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/iortcw.git



More information about the Pkg-games-commits mailing list