shithub: jbig2

Download patch

ref: 50d99ce72e1722427415ac745445dc1ca7e67c83
parent: 576c7d4162890ecee3b7322fd5665a8ae3c2d6d7
author: Shailesh Mistry <shailesh.mistry@hotmail.co.uk>
date: Mon Jul 2 17:53:20 EDT 2012

Bug 693050 : Fix error handling in 0717 folder

--- a/jbig2_halftone.c
+++ b/jbig2_halftone.c
@@ -277,7 +277,7 @@
                               Jbig2Image *GSKIP, int GSTEMPLATE,
                               Jbig2ArithCx *GB_stats)
 {
-  uint8_t **GSVALS;
+  uint8_t **GSVALS = NULL;
   size_t consumed_bytes = 0;
   int i, j, code, stride;
   int x, y;
@@ -289,7 +289,7 @@
   /* allocate GSPLANES */
   GSPLANES = jbig2_new(ctx, Jbig2Image*, GSBPP);
   if (GSPLANES == NULL) {
-    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, 
+    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
                 "failed to allocate %d bytes for GSPLANES", GSBPP);
     return NULL;
   }
@@ -297,7 +297,7 @@
   for (i = 0; i < GSBPP; ++i) {
     GSPLANES[i] = jbig2_image_new(ctx, GSW, GSH);
     if (GSPLANES[i] == NULL) {
-      jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
+      jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
                   "failed to allocate %dx%d image for GSPLANES", GSW, GSH);
       /* free already allocated */
       for (j = i-1; j >= 0; --j) {
@@ -328,7 +328,20 @@
                                      GSPLANES[GSBPP-1], &consumed_bytes);
   } else {
     ws = jbig2_word_stream_buf_new(ctx, data, size);
+    if (ws == NULL)
+    {
+      code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+        "failed to allocate ws in jbig2_decode_gray_scale_image");
+      goto cleanup;
+    }
+
     as = jbig2_arith_new(ctx, ws);
+    if (as == NULL)
+    {
+      code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+        "failed to allocate as in jbig2_decode_gray_scale_image");
+      goto cleanup;
+    }
 
     code = jbig2_decode_generic_region(ctx, segment, &rparams, as,
                                        GSPLANES[GSBPP-1], GB_stats);
@@ -335,8 +348,9 @@
 
   }
   if (code != 0) {
-    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
+    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
                 "error decoding GSPLANES for halftone image");
+    goto cleanup;
   }
 
   /* C.5 step 2. Set j = GSBPP-2 */ 
@@ -353,8 +367,9 @@
                                          GSPLANES[j], GB_stats);
     }
     if (code != 0) {
-      jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
+      jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
                   "error decoding GSPLANES for halftone image");
+      goto cleanup;
     }
 
     /* C.5 step 3. (b):
@@ -371,16 +386,22 @@
   /* allocate GSVALS */
   GSVALS = jbig2_new(ctx, uint8_t* , GSW);
   if (GSVALS == NULL) {
-    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
+    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
                 "failed to allocate GSVALS: %d bytes", GSW);
-    return NULL;
+    goto cleanup;
   }
   for (i=0; i<GSW; ++i) {
     GSVALS[i] = jbig2_new(ctx, uint8_t , GSH);
     if (GSVALS[i] == NULL) {
-      jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
+      jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
                   "failed to allocate GSVALS: %d bytes", GSH * GSW);
-      return NULL;
+      /* free already allocated */
+      for (j = i-1; j >= 0; --j) {
+        jbig2_free(ctx->allocator, GSVALS[j]);
+      }
+      jbig2_free(ctx->allocator, GSVALS);
+      GSVALS = NULL;
+      goto cleanup;
     }
   }
 
@@ -394,6 +415,7 @@
     }
   }
 
+cleanup:
   /* free memory */
   if (!GSMMR) {
     jbig2_free(ctx->allocator, as);
--- a/jbig2_mmr.c
+++ b/jbig2_mmr.c
@@ -1057,6 +1057,7 @@
 	byte *dst = image->data;
 	byte *ref = NULL;
 	int y;
+        int code = 0;
 	const uint32_t EOFB = 0x001001;
 
 	jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
@@ -1063,7 +1064,8 @@
 
 	for (y = 0; y < image->height; y++) {
 		memset(dst, 0, rowstride);
-		jbig2_decode_mmr_line(&mmr, ref, dst);
+		code = jbig2_decode_mmr_line(&mmr, ref, dst);
+               if (code < 0) return code;
 		ref = dst;
 		dst += rowstride;
 	}
@@ -1075,5 +1077,5 @@
 
 	*consumed_bytes += mmr.data_index + (mmr.bit_index >> 3) + 
                        (mmr.bit_index > 0 ? 1 : 0);
-	return 0;
+	return code;
 }