shithub: jbig2

Download patch

ref: 56d7948b4d9f3825a20f0cb6cc26a0a3b6ae34f1
parent: 19dabb532ae37749fdb6c06447e2dfcf0e216e9b
author: Ralph Giles <giles@ghostscript.com>
date: Wed Jul 15 21:56:34 EDT 2009

Check more return codes from jbig2_image_new().

There are still a number of unchecked instances in jbig2_symbol_dict.

--- a/jbig2_generic.c
+++ b/jbig2_generic.c
@@ -596,6 +596,9 @@
   memcpy (params.gbat, gbat, gbat_bytes);
 
   image = jbig2_image_new(ctx, rsi.width, rsi.height);
+  if (image == NULL)
+    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+             "unable to allocate generic image");
   jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
     "allocated %d x %d image buffer for region decode results",
         rsi.width, rsi.height);
--- a/jbig2_halftone.c
+++ b/jbig2_halftone.c
@@ -91,6 +91,15 @@
     /* 6.7.5(4) - copy out the individual pattern images */
     for (i = 0; i < N; i++) {
       new->patterns[i] = jbig2_image_new(ctx, HPW, HPH);
+      if (new->patterns[i] == NULL) {
+        int j;
+        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
+            "failed to allocate pattern element image");
+        for (j = 0; j < i; j++)
+          jbig2_free(ctx->allocator, new->patterns[j]);
+        jbig2_free(ctx->allocator, new);
+        return NULL;
+      }
       /* compose with the REPLACE operator; the source
          will be clipped to the destintion, selecting the
          proper sub image */
@@ -150,7 +159,7 @@
 	params->HDPW * (params->GRAYMAX + 1), params->HDPH);
   if (image == NULL) {
     jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
-	"failed to allocate collective bitmap for halftone dict!\n");
+	"failed to allocate collective bitmap for halftone dict!");
     return NULL;
   }
 
@@ -343,6 +352,9 @@
   }
 
   image = jbig2_image_new(ctx, region_info.width, region_info.height);
+  if (image == NULL)
+    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+             "unable to allocate halftone image");
 
   code = jbig2_decode_halftone_region(ctx, segment, &params,
 		segment_data + offset, segment->data_length - offset,
--- a/jbig2_page.c
+++ b/jbig2_page.c
@@ -146,7 +146,6 @@
         page->image = jbig2_image_new(ctx, page->width, page->height);
     }
     if (page->image == NULL) {
-        jbig2_free(ctx->allocator, page);
         return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
             "failed to allocate buffer for page image");
     } else {
--- a/jbig2_refinement.c
+++ b/jbig2_refinement.c
@@ -403,7 +403,7 @@
     image = jbig2_image_new(ctx, rsi.width, rsi.height);
     if (image == NULL)
       return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
-               "unable to allocate image storage");
+               "unable to allocate refinement image");
     jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
       "allocated %d x %d image buffer for region decode results",
           rsi.width, rsi.height);
--- a/jbig2_text.c
+++ b/jbig2_text.c
@@ -312,6 +312,15 @@
 		IBO = IB;
 		refimage = jbig2_image_new(ctx, IBO->width + RDW,
 						IBO->height + RDH);
+		if (refimage == NULL) {
+		  jbig2_image_release(ctx, IBO);
+		  if (params->SBHUFF) {
+		    jbig2_release_huffman_table(ctx, SBSYMCODES);
+		  }
+		  return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, 
+			segment->number,
+			"couldn't allocate reference image");
+	        }
 
 		/* Table 12 */
 		rparams.GRTEMPLATE = params->SBRTEMPLATE;
@@ -676,6 +685,22 @@
     }
 
     image = jbig2_image_new(ctx, region_info.width, region_info.height);
+    if (image == NULL) {
+      if (!params.SBHUFF && params.SBREFINE) {
+	jbig2_free(ctx->allocator, GR_stats);
+      } else if (params.SBHUFF) {
+	jbig2_release_huffman_table(ctx, params.SBHUFFFS);
+	jbig2_release_huffman_table(ctx, params.SBHUFFDS);
+	jbig2_release_huffman_table(ctx, params.SBHUFFDT);
+	jbig2_release_huffman_table(ctx, params.SBHUFFRDX);
+	jbig2_release_huffman_table(ctx, params.SBHUFFRDY);
+	jbig2_release_huffman_table(ctx, params.SBHUFFRDW);
+	jbig2_release_huffman_table(ctx, params.SBHUFFRDH);
+	jbig2_release_huffman_table(ctx, params.SBHUFFRSIZE);
+      }
+      return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+		"couldn't allocate text region image");
+    }
 
     ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
     if (!params.SBHUFF) {