shithub: jbig2

Download patch

ref: 861e081b86b9e4d6c8bf4ff6231025c7a950f6bf
parent: 3b08883c63526aebe86715ff0e7551eb0dbe4468
author: Sebastian Rasmussen <sebras@gmail.com>
date: Tue Apr 24 16:05:39 EDT 2018

jbig2dec: free/release functions must handle NULL argument.

--- a/jbig2.c
+++ b/jbig2.c
@@ -338,9 +338,13 @@
 void
 jbig2_ctx_free(Jbig2Ctx *ctx)
 {
-    Jbig2Allocator *ca = ctx->allocator;
+    Jbig2Allocator *ca;
     int i;
 
+    if (ctx == NULL)
+        return;
+
+    ca = ctx->allocator;
     jbig2_free(ca, ctx->buf);
     if (ctx->segments != NULL) {
         for (i = 0; i < ctx->n_segments; i++)
--- a/jbig2_halftone.c
+++ b/jbig2_halftone.c
@@ -102,8 +102,9 @@
 
     if (dict == NULL)
         return;
-    for (i = 0; i < dict->n_patterns; i++)
-        jbig2_image_release(ctx, dict->patterns[i]);
+    if (dict->patterns != NULL)
+        for (i = 0; i < dict->n_patterns; i++)
+            jbig2_image_release(ctx, dict->patterns[i]);
     jbig2_free(ctx->allocator, dict->patterns);
     jbig2_free(ctx->allocator, dict);
 }
--- a/jbig2_huffman.c
+++ b/jbig2_huffman.c
@@ -474,7 +474,6 @@
         jbig2_free(ctx->allocator, table->entries);
         jbig2_free(ctx->allocator, table);
     }
-    return;
 }
 
 /* Routines to handle "code table segment (53)" */
--- a/jbig2_image.c
+++ b/jbig2_image.c
@@ -91,7 +91,7 @@
     if (image == NULL)
         return;
     image->refcount--;
-    if (!image->refcount)
+    if (image->refcount == 0)
         jbig2_image_free(ctx, image);
 }
 
@@ -99,9 +99,10 @@
 void
 jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image)
 {
-    if (image)
+    if (image != NULL) {
         jbig2_free(ctx->allocator, image->data);
-    jbig2_free(ctx->allocator, image);
+        jbig2_free(ctx->allocator, image);
+    }
 }
 
 /* resize a Jbig2Image */
--- a/jbig2_metadata.c
+++ b/jbig2_metadata.c
@@ -60,6 +60,9 @@
 {
     int i;
 
+    if (md == NULL)
+        return;
+
     if (md->keys) {
         /* assume we own the pointers */
         for (i = 0; i < md->entries; i++)
--- a/jbig2_page.c
+++ b/jbig2_page.c
@@ -331,6 +331,9 @@
 {
     int index;
 
+    if (image == NULL)
+        return;
+
     /* find the matching page struct and mark it released */
     for (index = 0; index < ctx->max_page_index; index++) {
         if (ctx->pages[index].image == image) {
--- a/jbig2_segment.c
+++ b/jbig2_segment.c
@@ -134,8 +134,10 @@
 void
 jbig2_free_segment(Jbig2Ctx *ctx, Jbig2Segment *segment)
 {
-    jbig2_free(ctx->allocator, segment->referred_to_segments);
+    if (segment == NULL)
+        return;
 
+    jbig2_free(ctx->allocator, segment->referred_to_segments);
     /* todo: we need either some separate fields or
        a more complex result object rather than this
        brittle special casing */
--- a/jbig2_symbol_dict.c
+++ b/jbig2_symbol_dict.c
@@ -125,8 +125,9 @@
 
     if (dict == NULL)
         return;
-    for (i = 0; i < dict->n_symbols; i++)
-        jbig2_image_release(ctx, dict->glyphs[i]);
+    if (dict->glyphs != NULL)
+        for (i = 0; i < dict->n_symbols; i++)
+            jbig2_image_release(ctx, dict->glyphs[i]);
     jbig2_free(ctx->allocator, dict->glyphs);
     jbig2_free(ctx->allocator, dict);
 }