File xpdf-JBIG2Stream.patch of Package pdftohtml
--- xpdf/JBIG2Stream.cc
+++ xpdf/JBIG2Stream.cc
@@ -7,6 +7,7 @@
 //========================================================================
 
 #include <aconf.h>
+#include <limits.h>
 
 #ifdef USE_GCC_PRAGMAS
 #pragma implementation
@@ -1002,6 +1003,15 @@
   h = hA;
   line = (wA + 7) >> 3;
   data = (Guchar *)gmalloc(h * line);
+  if (h < 0 || line <= 0 || h >= (INT_MAX - 1) / line) {
+    error(-1, "invalid width/height");
+    data = NULL;
+    return;
+  }
+
+  // need to allocate one extra guard byte for use in combine()
+  data = (Guchar *)gmalloc(h * line + 1);
+  data[h * line] = 0;
 }
 
 JBIG2Bitmap::JBIG2Bitmap(Guint segNumA, JBIG2Bitmap *bitmap):
@@ -1011,7 +1021,17 @@
   h = bitmap->h;
   line = bitmap->line;
   data = (Guchar *)gmalloc(h * line);
+  if (h < 0 || line <= 0 || h >= (INT_MAX - 1) / line) {
+    error(-1, "invalid width/height");
+    data = NULL;
+    return;
+  }
+
+  // need to allocate one extra guard byte for use in combine()
+  data = (Guchar *)gmalloc(h * line + 1);
+
   memcpy(data, bitmap->data, h * line);
+  data[h * line] = 0;
 }
 
 JBIG2Bitmap::~JBIG2Bitmap() {
@@ -1036,10 +1056,13 @@
 }
 
 void JBIG2Bitmap::expand(int newH, Guint pixel) {
-  if (newH <= h) {
+  if (newH <= h || line <= 0 || newH >= (INT_MAX - 1)/ line) {
+    error(-1, "invalid width/height");
+    gfree(data);
+    data = NULL;
     return;
   }
-  data = (Guchar *)grealloc(data, newH * line);
+  data = (Guchar *)grealloc(data, newH * line + 1);
   if (pixel) {
     memset(data + h * line, 0xff, (newH - h) * line);
   } else {
@@ -2576,6 +2599,14 @@
     error(getPos(), "Bad symbol dictionary reference in JBIG2 halftone segment");
     return;
   }
+  if (gridH == 0 || gridW >= INT_MAX / gridH) {
+    error(getPos(), "Bad size in JBIG2 halftone segment");
+    return;
+  }
+  if (w == 0 || h >= INT_MAX / w) {
+     error(getPos(), "Bad size in JBIG2 bitmap segment");
+    return;
+  }
   patternDict = (JBIG2PatternDict *)seg;
   bpp = 0;
   i = 1;
@@ -3205,6 +3236,11 @@
   JBIG2BitmapPtr tpgrCXPtr0, tpgrCXPtr1, tpgrCXPtr2;
   int x, y, pix;
 
+  if (w < 0 || h <= 0 || w >= INT_MAX / h) {
+    error(-1, "invalid width/height");
+    return NULL;
+  }
+
   bitmap = new JBIG2Bitmap(0, w, h);
   bitmap->clearToZero();