shithub: jbig2

Download patch

ref: f1d00697525dd2d7a5f63f96e01ad0d99e673b13
parent: 63e0436a711c59f7fae6cfd721b90428ae19a7b3
author: Ralph Giles <giles@ghostscript.com>
date: Mon Mar 30 14:35:21 EDT 2009

Restore the proper size of the huffman symbol length histogram.

The previous size as 256 KB, but LENCOUNT's highest index is PREFLEN
(or LENMAX - 1, but LENMAX is the largest value of PREFLEN) and that
comes from a byte member of a Jbig2HuffmanEntry. Therefore 256 members
are all that is necessary, or 1 KB on most machines.

This was changed in commit a0531af8 (jbig2dec svn c430) apparently in
the mistaken belief that it needed to scale with the size of the table
itself. In that revision the previous stack allocation of 256 elements
was replaced with the 1k, which later became 64k, generally overflowing
the stack. While 256 elements is a reasonable stack allocation, it's
safer to continue using the heap.

--- a/jbig2_huffman.c
+++ b/jbig2_huffman.c
@@ -287,6 +287,7 @@
 {
   int *LENCOUNT;
   int LENMAX = -1;
+  const int lencountsize = 256 * sizeof(*LENCOUNT);
   const Jbig2HuffmanLine *lines = params->lines;
   int n_lines = params->n_lines;
   int i, j;
@@ -299,14 +300,13 @@
   int CURCODE;
   int CURTEMP;
 
-  LENCOUNT = jbig2_alloc(ctx->allocator,
-    sizeof(*LENCOUNT)*(1 << LOG_TABLE_SIZE_MAX));
-  if (LENCOUNT== NULL) {
+  LENCOUNT = jbig2_alloc(ctx->allocator, lencountsize);
+  if (LENCOUNT == NULL) {
     jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
       "couldn't allocate storage for huffman histogram");
     return NULL;
   }
-  memset(LENCOUNT, 0, sizeof(*LENCOUNT)*(1 << LOG_TABLE_SIZE_MAX));
+  memset(LENCOUNT, 0, lencountsize);
 
   /* B.3, 1. */
   for (i = 0; i < params->n_lines; i++)
@@ -321,6 +321,9 @@
 			LENMAX = PREFLEN;
 		}
       LENCOUNT[PREFLEN]++;
+
+	jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1,
+	  "PREFLEN %d", PREFLEN);
 
       lts = PREFLEN + lines[i].RANGELEN;
       if (lts > LOG_TABLE_SIZE_MAX)