d776db3
diff --git a/analyzer/pgmtexture.c b/analyzer/pgmtexture.c
9ec2837
index 84c6bf4..c9576f5 100644
d776db3
--- a/analyzer/pgmtexture.c
d776db3
+++ b/analyzer/pgmtexture.c
d776db3
@@ -98,6 +98,8 @@ vector(unsigned int const nl,
d60ce20
 
d60ce20
     assert(nh >= nl);
d776db3
 
93a9cf2
+    overflow_add(nh - nl, 1);
d776db3
+    
93a9cf2
     MALLOCARRAY(v, (unsigned) (nh - nl + 1));
d60ce20
 
93a9cf2
     if (v == NULL)
d776db3
@@ -129,6 +131,7 @@ matrix (unsigned int const nrl,
d60ce20
     assert(nrh >= nrl);
cvsdist 499931c
 
93a9cf2
     /* allocate pointers to rows */
93a9cf2
+    overflow_add(nrh - nrl, 1);
93a9cf2
     MALLOCARRAY(m, (unsigned) (nrh - nrl + 1));
93a9cf2
     if (m == NULL)
93a9cf2
         pm_error("Unable to allocate memory for a matrix.");
d776db3
@@ -137,6 +140,7 @@ matrix (unsigned int const nrl,
cvsdist 499931c
 
d60ce20
     assert (nch >= ncl);
d776db3
 
93a9cf2
+    overflow_add(nch - ncl, 1);
d60ce20
     /* allocate rows and set pointers to them */
d60ce20
     for (i = nrl; i <= nrh; ++i) {
93a9cf2
         MALLOCARRAY(m[i], (unsigned) (nch - ncl + 1));
d776db3
diff --git a/converter/other/gemtopnm.c b/converter/other/gemtopnm.c
d776db3
index aac7479..5f1a51a 100644
d776db3
--- a/converter/other/gemtopnm.c
d776db3
+++ b/converter/other/gemtopnm.c
93a9cf2
@@ -106,6 +106,7 @@ main(argc, argv)
cvsdist 499931c
 
93a9cf2
 	pnm_writepnminit( stdout, cols, rows, MAXVAL, type, 0 );
cvsdist 499931c
 
93a9cf2
+    overflow_add(cols, padright);
93a9cf2
     { 
93a9cf2
         /* allocate input row data structure */
93a9cf2
         int plane;
d776db3
diff --git a/converter/other/jpegtopnm.c b/converter/other/jpegtopnm.c
d776db3
index ab3b18e..c324b86 100644
d776db3
--- a/converter/other/jpegtopnm.c
d776db3
+++ b/converter/other/jpegtopnm.c
d776db3
@@ -861,6 +861,8 @@ convertImage(FILE *                          const ofP,
93a9cf2
     /* Calculate output image dimensions so we can allocate space */
93a9cf2
     jpeg_calc_output_dimensions(cinfoP);
cvsdist 499931c
 
93a9cf2
+    overflow2(cinfoP->output_width, cinfoP->output_components);
b3d2df7
+
b3d2df7
     /* Start decompressor */
b3d2df7
     jpeg_start_decompress(cinfoP);
b3d2df7
 
d776db3
diff --git a/converter/other/pbmtopgm.c b/converter/other/pbmtopgm.c
d776db3
index 69b20fb..382a487 100644
d776db3
--- a/converter/other/pbmtopgm.c
d776db3
+++ b/converter/other/pbmtopgm.c
93a9cf2
@@ -47,6 +47,7 @@ main(int argc, char *argv[]) {
93a9cf2
                  "than the image height (%u rows)", height, rows);
a3c9c4b
 
93a9cf2
     outrow = pgm_allocrow(cols) ;
93a9cf2
+    overflow2(width, height);
93a9cf2
     maxval = MIN(PGM_OVERALLMAXVAL, width*height);
93a9cf2
     pgm_writepgminit(stdout, cols, rows, maxval, 0) ;
a3c9c4b
 
d776db3
diff --git a/converter/other/pnmtoddif.c b/converter/other/pnmtoddif.c
9ec2837
index ac02e42..a2f045b 100644
d776db3
--- a/converter/other/pnmtoddif.c
d776db3
+++ b/converter/other/pnmtoddif.c
9ec2837
@@ -629,6 +629,7 @@ main(int argc, char *argv[]) {
93a9cf2
     switch (PNM_FORMAT_TYPE(format)) {
93a9cf2
     case PBM_TYPE:
93a9cf2
         ip.bits_per_pixel = 1;
93a9cf2
+        overflow_add(cols, 7);
93a9cf2
         ip.bytes_per_line = (cols + 7) / 8;
93a9cf2
         ip.spectral = 2;
93a9cf2
         ip.components = 1;
9ec2837
@@ -644,6 +645,7 @@ main(int argc, char *argv[]) {
93a9cf2
         ip.polarity = 2;
93a9cf2
         break;
93a9cf2
     case PPM_TYPE:
93a9cf2
+        overflow2(cols, 3);
93a9cf2
         ip.bytes_per_line = 3 * cols;
93a9cf2
         ip.bits_per_pixel = 24;
93a9cf2
         ip.spectral = 5;
d776db3
diff --git a/converter/other/pnmtojpeg.c b/converter/other/pnmtojpeg.c
d776db3
index ce231c9..1279040 100644
d776db3
--- a/converter/other/pnmtojpeg.c
d776db3
+++ b/converter/other/pnmtojpeg.c
d776db3
@@ -605,7 +605,11 @@ read_scan_script(j_compress_ptr const cinfo,
b3d2df7
            want JPOOL_PERMANENT.  
b3d2df7
         */
b3d2df7
         const unsigned int scan_info_size = nscans * sizeof(jpeg_scan_info);
b3d2df7
-        jpeg_scan_info * const scan_info = 
b3d2df7
+        const jpeg_scan_info * scan_info;
b3d2df7
+      
b3d2df7
+        overflow2(nscans, sizeof(jpeg_scan_info));
b3d2df7
+      
b3d2df7
+        scan_info =
b3d2df7
             (jpeg_scan_info *)
b3d2df7
             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
b3d2df7
                                         scan_info_size);
d776db3
@@ -937,6 +941,8 @@ compute_rescaling_array(JSAMPLE ** const rescale_p, const pixval maxval,
93a9cf2
   const long half_maxval = maxval / 2;
93a9cf2
   long val;
cvsdist 499931c
 
93a9cf2
+  overflow_add(maxval, 1);
93a9cf2
+  overflow2(maxval+1, sizeof(JSAMPLE));
93a9cf2
   *rescale_p = (JSAMPLE *)
93a9cf2
     (cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_IMAGE,
93a9cf2
                               (size_t) (((long) maxval + 1L) * 
d776db3
@@ -1015,6 +1021,7 @@ convert_scanlines(struct jpeg_compress_struct * const cinfo_p,
93a9cf2
     */
cvsdist 499931c
 
93a9cf2
   /* Allocate the libpnm output and compressor input buffers */
93a9cf2
+  overflow2(cinfo_p->image_width, cinfo_p->input_components);
93a9cf2
   buffer = (*cinfo_p->mem->alloc_sarray)
93a9cf2
     ((j_common_ptr) cinfo_p, JPOOL_IMAGE,
93a9cf2
      (unsigned int) cinfo_p->image_width * cinfo_p->input_components, 
d776db3
diff --git a/converter/other/pnmtops.c b/converter/other/pnmtops.c
9ec2837
index c1dadc3..8f9f608 100644
d776db3
--- a/converter/other/pnmtops.c
d776db3
+++ b/converter/other/pnmtops.c
9ec2837
@@ -293,17 +293,21 @@ parseCommandLine(int argc, const char ** argv,
d60ce20
     validateCompDimension(width, 72, "-width value");
d60ce20
     validateCompDimension(height, 72, "-height value");
93a9cf2
     
93a9cf2
+    overflow2(width, 72);
93a9cf2
     cmdlineP->width  = width * 72;
93a9cf2
+    overflow2(height, 72);
93a9cf2
     cmdlineP->height = height * 72;
cvsdist 499931c
 
d60ce20
     if (imagewidthSpec) {
d60ce20
         validateCompDimension(imagewidth, 72, "-imagewidth value");
93a9cf2
+        overflow2(imagewidth, 72);
93a9cf2
         cmdlineP->imagewidth = imagewidth * 72;
d60ce20
     }
d60ce20
     else
93a9cf2
         cmdlineP->imagewidth = 0;
d60ce20
     if (imageheightSpec) {
d60ce20
-        validateCompDimension(imagewidth, 72, "-imageheight value");
d60ce20
+        validateCompDimension(imageheight, 72, "-imageheight value");
93a9cf2
+        overflow2(imageheight, 72);
93a9cf2
         cmdlineP->imageheight = imageheight * 72;
d60ce20
     }
d60ce20
     else
d776db3
diff --git a/converter/other/rletopnm.c b/converter/other/rletopnm.c
9ec2837
index ff37cfe..a1dd7b9 100644
d776db3
--- a/converter/other/rletopnm.c
d776db3
+++ b/converter/other/rletopnm.c
93a9cf2
@@ -19,6 +19,8 @@
93a9cf2
  * If you modify this software, you should include a notice giving the
93a9cf2
  * name of the person performing the modification, the date of modification,
93a9cf2
  * and the reason for such modification.
93a9cf2
+ *
93a9cf2
+ *  2002-12-19: Fix maths wrapping bugs. Alan Cox <alan@redhat.com>
93a9cf2
  */
93a9cf2
 /*
93a9cf2
  * rletopnm - A conversion program to convert from Utah's "rle" image format
d776db3
diff --git a/converter/other/sirtopnm.c b/converter/other/sirtopnm.c
d776db3
index fafcc91..9fe49d0 100644
d776db3
--- a/converter/other/sirtopnm.c
d776db3
+++ b/converter/other/sirtopnm.c
93a9cf2
@@ -69,6 +69,7 @@ char* argv[];
93a9cf2
 	    }
93a9cf2
 	    break;
93a9cf2
 	case PPM_TYPE:
93a9cf2
+	    overflow3(cols, rows, 3);
93a9cf2
 	    picsize = cols * rows * 3;
93a9cf2
 	    planesize = cols * rows;
93a9cf2
             if ( !( sirarray = (unsigned char*) malloc( picsize ) ) ) 
d776db3
diff --git a/converter/other/tifftopnm.c b/converter/other/tifftopnm.c
9ec2837
index f9e602f..4637afa 100644
d776db3
--- a/converter/other/tifftopnm.c
d776db3
+++ b/converter/other/tifftopnm.c
9ec2837
@@ -1317,7 +1317,9 @@ convertRasterByRows(pnmOut *       const pnmOutP,
93a9cf2
     if (scanbuf == NULL)
93a9cf2
         pm_error("can't allocate memory for scanline buffer");
cvsdist 499931c
 
93a9cf2
-    MALLOCARRAY(samplebuf, cols * spp);
93a9cf2
+    /* samplebuf is unsigned int * !!! */
93a9cf2
+    samplebuf = (unsigned int *) malloc3(cols , sizeof(unsigned int) , spp);
b3d2df7
+
93a9cf2
     if (samplebuf == NULL)
b3d2df7
         pm_error("can't allocate memory for row buffer");
b3d2df7
 
d776db3
diff --git a/converter/other/xwdtopnm.c b/converter/other/xwdtopnm.c
9ec2837
index d49a2b0..3a6b335 100644
d776db3
--- a/converter/other/xwdtopnm.c
d776db3
+++ b/converter/other/xwdtopnm.c
d776db3
@@ -209,6 +209,10 @@ processX10Header(X10WDFileHeader *  const h10P,
b3d2df7
         *colorsP = pnm_allocrow(2);
b3d2df7
         PNM_ASSIGN1((*colorsP)[0], 0);
b3d2df7
         PNM_ASSIGN1((*colorsP)[1], *maxvalP);
b3d2df7
+        overflow_add(h10P->pixmap_width, 15);
b3d2df7
+        if(h10P->pixmap_width < 0)
b3d2df7
+            pm_error("assert: negative width");
b3d2df7
+        overflow2((((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width), 8);
b3d2df7
         *padrightP =
b3d2df7
             (((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width) * 8;
b3d2df7
         *bits_per_itemP = 16;
d776db3
@@ -634,6 +638,7 @@ processX11Header(X11WDFileHeader *  const h11P,
cvsdist 499931c
 
b3d2df7
     *colsP = h11FixedP->pixmap_width;
b3d2df7
     *rowsP = h11FixedP->pixmap_height;
b3d2df7
+    overflow2(h11FixedP->bytes_per_line, 8);
b3d2df7
     *padrightP =
b3d2df7
         h11FixedP->bytes_per_line * 8 -
b3d2df7
         h11FixedP->pixmap_width * h11FixedP->bits_per_pixel;
d776db3
diff --git a/converter/pbm/mdatopbm.c b/converter/pbm/mdatopbm.c
d776db3
index d8e0657..12c7468 100644
d776db3
--- a/converter/pbm/mdatopbm.c
d776db3
+++ b/converter/pbm/mdatopbm.c
93a9cf2
@@ -245,10 +245,13 @@ main(int argc, char **argv) {
93a9cf2
         pm_readlittleshort(infile, &yy;; nInCols = yy;
1f08c10
     }
93a9cf2
     
93a9cf2
+    overflow2(nOutCols, 8);
93a9cf2
     nOutCols = 8 * nInCols;
93a9cf2
     nOutRows = nInRows;
93a9cf2
-    if (bScale) 
93a9cf2
+    if (bScale) {
93a9cf2
+        overflow2(nOutRows, 2);
93a9cf2
         nOutRows *= 2;
93a9cf2
+    }
cvsdist 499931c
 
93a9cf2
     data = pbm_allocarray(nOutCols, nOutRows);
93a9cf2
     
d776db3
diff --git a/converter/pbm/mgrtopbm.c b/converter/pbm/mgrtopbm.c
d776db3
index 9f7004a..60e8477 100644
d776db3
--- a/converter/pbm/mgrtopbm.c
d776db3
+++ b/converter/pbm/mgrtopbm.c
b3d2df7
@@ -65,6 +65,8 @@ readMgrHeader(FILE *          const ifP,
1f08c10
     if (head.h_high < ' ' || head.l_high < ' ')
1f08c10
         pm_error("Invalid width field in MGR header");
cvsdist 499931c
     
1f08c10
+    overflow_add(*colsP, pad);
1f08c10
+
1f08c10
     *colsP = (((int)head.h_wide - ' ') << 6) + ((int)head.l_wide - ' ');
1f08c10
     *rowsP = (((int)head.h_high - ' ') << 6) + ((int) head.l_high - ' ');
1f08c10
     *padrightP = ( ( *colsP + pad - 1 ) / pad ) * pad - *colsP;
d776db3
diff --git a/converter/pbm/pbmto4425.c b/converter/pbm/pbmto4425.c
d776db3
index 1d97ac6..c4c8cbb 100644
d776db3
--- a/converter/pbm/pbmto4425.c
d776db3
+++ b/converter/pbm/pbmto4425.c
d776db3
@@ -2,6 +2,7 @@
d776db3
 
d776db3
 #include "nstring.h"
d776db3
 #include "pbm.h"
d776db3
+#include <string.h>
d776db3
 
d776db3
 static char bit_table[2][3] = {
d776db3
 {1, 4, 0x10},
d776db3
@@ -160,7 +161,7 @@ main(int argc, char * argv[]) {
d776db3
     xres = vmap_width * 2;
d776db3
     yres = vmap_height * 3;
d776db3
 
d776db3
-    vmap = malloc(vmap_width * vmap_height * sizeof(char));
d776db3
+    vmap = malloc3(vmap_width, vmap_height, sizeof(char));
d776db3
     if(vmap == NULL)
d776db3
 	{
d776db3
         pm_error( "Cannot allocate memory" );
d776db3
diff --git a/converter/pbm/pbmtogem.c b/converter/pbm/pbmtogem.c
d776db3
index 9eab041..13b0257 100644
d776db3
--- a/converter/pbm/pbmtogem.c
d776db3
+++ b/converter/pbm/pbmtogem.c
d60ce20
@@ -79,6 +79,7 @@ putinit (int const rows, int const cols)
93a9cf2
   bitsperitem = 0;
93a9cf2
   bitshift = 7;
93a9cf2
   outcol = 0;
93a9cf2
+  overflow_add(cols, 7);
93a9cf2
   outmax = (cols + 7) / 8;
93a9cf2
   outrow = (unsigned char *) pm_allocrow (outmax, sizeof (unsigned char));
93a9cf2
   lastrow = (unsigned char *) pm_allocrow (outmax, sizeof (unsigned char));
d776db3
diff --git a/converter/pbm/pbmtogo.c b/converter/pbm/pbmtogo.c
d776db3
index 23b2ee9..d2ee91f 100644
d776db3
--- a/converter/pbm/pbmtogo.c
d776db3
+++ b/converter/pbm/pbmtogo.c
b3d2df7
@@ -158,6 +158,7 @@ main(int           argc,
93a9cf2
     bitrow = pbm_allocrow(cols);
93a9cf2
 
93a9cf2
     /* Round cols up to the nearest multiple of 8. */
93a9cf2
+    overflow_add(cols, 7);
93a9cf2
     rucols = ( cols + 7 ) / 8;
93a9cf2
     bytesperrow = rucols;       /* GraphOn uses bytes */
93a9cf2
     rucols = rucols * 8;
d776db3
diff --git a/converter/pbm/pbmtolj.c b/converter/pbm/pbmtolj.c
d776db3
index 0cceb4f..fdab6df 100644
d776db3
--- a/converter/pbm/pbmtolj.c
d776db3
+++ b/converter/pbm/pbmtolj.c
b3d2df7
@@ -120,7 +120,11 @@ parseCommandLine(int argc, char ** argv,
93a9cf2
 static void
93a9cf2
 allocateBuffers(unsigned int const cols) {
a3c9c4b
 
93a9cf2
+    overflow_add(cols, 8);
93a9cf2
     rowBufferSize = (cols + 7) / 8;
93a9cf2
+    overflow_add(rowBufferSize, 128);
93a9cf2
+    overflow_add(rowBufferSize, rowBufferSize+128);
93a9cf2
+    overflow_add(rowBufferSize+10, rowBufferSize/8);
93a9cf2
     packBufferSize = rowBufferSize + (rowBufferSize + 127) / 128 + 1;
93a9cf2
     deltaBufferSize = rowBufferSize + rowBufferSize / 8 + 10;
a3c9c4b
 
d776db3
diff --git a/converter/pbm/pbmtomda.c b/converter/pbm/pbmtomda.c
d776db3
index 3ad5149..9efe5cf 100644
d776db3
--- a/converter/pbm/pbmtomda.c
d776db3
+++ b/converter/pbm/pbmtomda.c
93a9cf2
@@ -179,6 +179,7 @@ int main(int argc, char **argv)
93a9cf2
     
93a9cf2
     nOutRowsUnrounded = bScale ? nInRows/2 : nInRows;
93a9cf2
 
93a9cf2
+    overflow_add(nOutRowsUnrounded, 3);
93a9cf2
     nOutRows = ((nOutRowsUnrounded + 3) / 4) * 4;
93a9cf2
         /* MDA wants rows a multiple of 4 */   
93a9cf2
     nOutCols = nInCols / 8;
d776db3
diff --git a/converter/pbm/pbmtoppa/pbm.c b/converter/pbm/pbmtoppa/pbm.c
d776db3
index 2f8a42b..1c8d236 100644
d776db3
--- a/converter/pbm/pbmtoppa/pbm.c
d776db3
+++ b/converter/pbm/pbmtoppa/pbm.c
d776db3
@@ -106,6 +106,7 @@ int pbm_readline(pbm_stat* pbm,unsigned char* data)
cvsdist 499931c
     return 0;
cvsdist 499931c
 
cvsdist 499931c
   case P4:
cvsdist 499931c
+    overflow_add(pbm->width, 7);
cvsdist 499931c
     tmp=(pbm->width+7)/8;
cvsdist 499931c
     tmp2=fread(data,1,tmp,pbm->fptr);
cvsdist 499931c
     if(tmp2 == tmp)
d776db3
@@ -130,6 +131,7 @@ void pbm_unreadline (pbm_stat *pbm, void *data)
cvsdist 499931c
     return;
cvsdist 499931c
 
cvsdist 499931c
   pbm->unread = 1;
cvsdist 499931c
+  overflow_add(pbm->width, 7);
d776db3
   pbm->revdata = malloc ((pbm->width+7)/8);
cvsdist 499931c
   memcpy (pbm->revdata, data, (pbm->width+7)/8);
cvsdist 499931c
   pbm->current_line--;
d776db3
diff --git a/converter/pbm/pbmtoppa/pbmtoppa.c b/converter/pbm/pbmtoppa/pbmtoppa.c
d776db3
index f43c08a..98e0284 100644
d776db3
--- a/converter/pbm/pbmtoppa/pbmtoppa.c
d776db3
+++ b/converter/pbm/pbmtoppa/pbmtoppa.c
d776db3
@@ -452,6 +452,7 @@ main(int argc, char *argv[]) {
93a9cf2
             pm_error("main(): unrecognized parameter '%s'", argv[argn]);
1f08c10
     }
a3c9c4b
 
93a9cf2
+    overflow_add(Width, 7);
93a9cf2
     Pwidth=(Width+7)/8;
93a9cf2
     printer.fptr=out;
cvsdist 499931c
 
d776db3
diff --git a/converter/pbm/pbmtoxbm.c b/converter/pbm/pbmtoxbm.c
9ec2837
index 14c6b85..6323483 100644
d776db3
--- a/converter/pbm/pbmtoxbm.c
d776db3
+++ b/converter/pbm/pbmtoxbm.c
d776db3
@@ -351,6 +351,8 @@ convertRaster(FILE *          const ifP,
1f08c10
 
b3d2df7
     unsigned char * bitrow;
b3d2df7
     unsigned int row;
b3d2df7
+    
b3d2df7
+    overflow_add(cols, padright);
1f08c10
 
b3d2df7
     putinit(xbmVersion);
a3c9c4b
 
d776db3
diff --git a/converter/pbm/pktopbm.c b/converter/pbm/pktopbm.c
d776db3
index 712f339..b6fcb02 100644
d776db3
--- a/converter/pbm/pktopbm.c
d776db3
+++ b/converter/pbm/pktopbm.c
d776db3
@@ -280,6 +280,7 @@ main(int argc, char *argv[]) {
93a9cf2
         if (flagbyte == 7) {            /* long form preamble */
93a9cf2
             integer packetlength = get32() ;    /* character packet length */
93a9cf2
             car = get32() ;         /* character number */
93a9cf2
+            overflow_add(packetlength, pktopbm_pkloc);
93a9cf2
             endofpacket = packetlength + pktopbm_pkloc;
93a9cf2
                 /* calculate end of packet */
93a9cf2
             if ((car >= MAXPKCHAR) || !filename[car]) {
d776db3
diff --git a/converter/pbm/thinkjettopbm.l b/converter/pbm/thinkjettopbm.l
d776db3
index 5de4f2b..7f31de5 100644
d776db3
--- a/converter/pbm/thinkjettopbm.l
d776db3
+++ b/converter/pbm/thinkjettopbm.l
d60ce20
@@ -114,7 +114,9 @@ DIG             [0-9]
1f08c10
 <RASTERMODE>\033\*b{DIG}+W  {
1f08c10
                             int l;
1f08c10
                             if (rowCount >= rowCapacity) {
d776db3
-                                rowCapacity += 100;
1f08c10
+				overflow_add(rowCapacity, 100);
d776db3
+                                 rowCapacity += 100;
1f08c10
+				overflow2(rowCapacity, sizeof *rows);
1f08c10
                                 rows = realloc (rows, rowCapacity * sizeof *rows);
1f08c10
                                 if (rows == NULL)
1f08c10
                                     pm_error ("Out of memory.");
d60ce20
@@ -226,6 +228,8 @@ yywrap (void)
1f08c10
     /*
1f08c10
      * Quite simple since ThinkJet bit arrangement matches PBM
1f08c10
      */
1f08c10
+
1f08c10
+    overflow2(maxRowLength, 8);
1f08c10
     pbm_writepbminit(stdout, maxRowLength*8, rowCount, 0);
1f08c10
 
1f08c10
     packed_bitrow = malloc(maxRowLength);
d776db3
diff --git a/converter/pbm/ybmtopbm.c b/converter/pbm/ybmtopbm.c
d776db3
index 2a42908..cf1ff03 100644
d776db3
--- a/converter/pbm/ybmtopbm.c
d776db3
+++ b/converter/pbm/ybmtopbm.c
d776db3
@@ -43,6 +43,7 @@ getinit(FILE *  const ifP,
d60ce20
         pm_error("EOF / read error");
1f08c10
 
93a9cf2
     *depthP = 1;
93a9cf2
+    overflow_add(*colsP, 15);
d60ce20
 }
d60ce20
 
d776db3
 
d776db3
diff --git a/converter/pgm/lispmtopgm.c b/converter/pgm/lispmtopgm.c
d776db3
index 40dd3fb..b5469f7 100644
d776db3
--- a/converter/pgm/lispmtopgm.c
d776db3
+++ b/converter/pgm/lispmtopgm.c
93a9cf2
@@ -58,6 +58,7 @@ main( argc, argv )
93a9cf2
         pm_error( "depth (%d bits) is too large", depth);
1f08c10
 
93a9cf2
     pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 );
93a9cf2
+    overflow_add(cols, 7);
93a9cf2
     grayrow = pgm_allocrow( ( cols + 7 ) / 8 * 8 );
93a9cf2
 
93a9cf2
     for ( row = 0; row < rows; ++row )
d776db3
@@ -102,6 +103,8 @@ getinit( file, colsP, rowsP, depthP, padrightP )
1f08c10
     
93a9cf2
     if ( *depthP == 0 )
93a9cf2
 	*depthP = 1;	/* very old file */
93a9cf2
+
93a9cf2
+    overflow_add((int)colsP, 31);
d776db3
     
93a9cf2
     *padrightP = ( ( *colsP + 31 ) / 32 ) * 32 - *colsP;
1f08c10
     
d776db3
diff --git a/converter/pgm/psidtopgm.c b/converter/pgm/psidtopgm.c
d776db3
index 07417d1..25bb311 100644
d776db3
--- a/converter/pgm/psidtopgm.c
d776db3
+++ b/converter/pgm/psidtopgm.c
93a9cf2
@@ -78,6 +78,7 @@ main(int     argc,
93a9cf2
         pm_error("bits/sample (%d) is too large.", bitspersample);
93a9cf2
 
93a9cf2
     pgm_writepgminit(stdout, cols, rows, maxval, 0);
1f08c10
+    overflow_add(cols, 7);
93a9cf2
     grayrow = pgm_allocrow((cols + 7) / 8 * 8);
93a9cf2
     for (row = 0; row < rows; ++row) {
93a9cf2
         unsigned int col;
d776db3
diff --git a/converter/ppm/Makefile b/converter/ppm/Makefile
9ec2837
index 003ef8d..b97349d 100644
d776db3
--- a/converter/ppm/Makefile
d776db3
+++ b/converter/ppm/Makefile
9ec2837
@@ -11,7 +11,7 @@ SUBDIRS = hpcdtoppm ppmtompeg
d776db3
 
d776db3
 PORTBINARIES =	411toppm eyuvtoppm gouldtoppm ilbmtoppm imgtoppm \
d776db3
 		leaftoppm mtvtoppm neotoppm \
d776db3
-		pcxtoppm pc1toppm pi1toppm picttoppm pjtoppm \
d776db3
+		pcxtoppm pc1toppm pi1toppm pjtoppm \
d776db3
 		ppmtoacad ppmtoapplevol ppmtoarbtxt ppmtoascii \
d776db3
 		ppmtobmp ppmtoeyuv ppmtogif ppmtoicr ppmtoilbm \
d776db3
 		ppmtoleaf ppmtolj ppmtomitsu ppmtoneo \
d776db3
diff --git a/converter/ppm/ilbmtoppm.c b/converter/ppm/ilbmtoppm.c
d776db3
index 662be0b..2a86efc 100644
d776db3
--- a/converter/ppm/ilbmtoppm.c
d776db3
+++ b/converter/ppm/ilbmtoppm.c
d776db3
@@ -606,6 +606,7 @@ decode_row(FILE *          const ifP,
93a9cf2
     rawtype *chp;
1f08c10
 
93a9cf2
     cols = bmhdP->w;
93a9cf2
+    overflow_add(cols, 15);
93a9cf2
     bytes = RowBytes(cols);
93a9cf2
     for( plane = 0; plane < nPlanes; plane++ ) {
93a9cf2
         int mask;
d776db3
@@ -693,6 +694,23 @@ decode_mask(FILE *          const ifP,
93a9cf2
  Multipalette handling
93a9cf2
  ****************************************************************************/
93a9cf2
 
93a9cf2
+static void *
93a9cf2
+xmalloc2(x, y)
93a9cf2
+    int x;
93a9cf2
+    int y;
93a9cf2
+{
93a9cf2
+    void *mem;
93a9cf2
+
93a9cf2
+    overflow2(x,y);
93a9cf2
+    if( x * y == 0 )
93a9cf2
+        return NULL;
93a9cf2
+
93a9cf2
+    mem = malloc2(x,y);
93a9cf2
+    if( mem == NULL )
93a9cf2
+        pm_error("out of memory allocating %d bytes", x * y);
93a9cf2
+    return mem;
93a9cf2
+}
93a9cf2
+
93a9cf2
 
93a9cf2
 static void
93a9cf2
 multi_adjust(cmap, row, palchange)
d776db3
@@ -1355,6 +1373,9 @@ dcol_to_ppm(FILE *         const ifP,
93a9cf2
     if( redmaxval != maxval || greenmaxval != maxval || bluemaxval != maxval )
93a9cf2
         pm_message("scaling colors to %d bits", pm_maxvaltobits(maxval));
a3c9c4b
     
93a9cf2
+    overflow_add(redmaxval, 1);
93a9cf2
+    overflow_add(greenmaxval, 1);
93a9cf2
+    overflow_add(bluemaxval, 1);
93a9cf2
     MALLOCARRAY_NOFAIL(redtable,   redmaxval   +1);
93a9cf2
     MALLOCARRAY_NOFAIL(greentable, greenmaxval +1);
93a9cf2
     MALLOCARRAY_NOFAIL(bluetable,  bluemaxval  +1);
d776db3
@@ -1784,7 +1805,9 @@ PCHG_ConvertSmall(PCHG, cmap, mask, datasize)
93a9cf2
             ChangeCount32 = *data++;
93a9cf2
             datasize -= 2;
a3c9c4b
 
93a9cf2
+            overflow_add(ChangeCount16, ChangeCount32);
93a9cf2
             changes = ChangeCount16 + ChangeCount32;
93a9cf2
+            overflow_add(changes, 1);
93a9cf2
             for( i = 0; i < changes; i++ ) {
93a9cf2
                 if( totalchanges >= PCHG->TotalChanges ) goto fail;
93a9cf2
                 if( datasize < 2 ) goto fail;
d776db3
@@ -2049,6 +2072,9 @@ read_pchg(FILE *     const ifP,
93a9cf2
             cmap->mp_change[i] = NULL;
93a9cf2
         if( PCHG.StartLine < 0 ) {
93a9cf2
             int nch;
93a9cf2
+            if(PCHG.MaxReg < PCHG.MinReg)
93a9cf2
+                pm_error("assert: MinReg > MaxReg");
93a9cf2
+            overflow_add(PCHG.MaxReg-PCHG.MinReg, 2);
93a9cf2
             nch = PCHG.MaxReg - PCHG.MinReg +1;
93a9cf2
             MALLOCARRAY_NOFAIL(cmap->mp_init, nch + 1);
93a9cf2
             for( i = 0; i < nch; i++ )
d776db3
@@ -2125,6 +2151,7 @@ process_body( FILE *          const ifP,
d776db3
     if (typeid == ID_ILBM) {
93a9cf2
         int isdeep;
1f08c10
 
93a9cf2
+        overflow_add(bmhdP->w, 15);
93a9cf2
         MALLOCARRAY_NOFAIL(ilbmrow, RowBytes(bmhdP->w));
93a9cf2
         *viewportmodesP |= fakeviewport;      /* -isham/-isehb */
1f08c10
 
d776db3
diff --git a/converter/ppm/imgtoppm.c b/converter/ppm/imgtoppm.c
d776db3
index 7078b88..eb8509e 100644
d776db3
--- a/converter/ppm/imgtoppm.c
d776db3
+++ b/converter/ppm/imgtoppm.c
93a9cf2
@@ -84,6 +84,7 @@ main(int argc, char ** argv) {
93a9cf2
             len = atoi((char*) buf );
93a9cf2
             if ( fread( buf, len, 1, ifp ) != 1 )
93a9cf2
                 pm_error( "bad colormap buf" );
93a9cf2
+            overflow2(cmaplen, 3);
93a9cf2
             if ( cmaplen * 3 != len )
93a9cf2
             {
93a9cf2
                 pm_message(
93a9cf2
@@ -105,6 +106,7 @@ main(int argc, char ** argv) {
93a9cf2
                 pm_error( "bad pixel data header" );
93a9cf2
             buf[8] = '\0';
93a9cf2
             len = atoi((char*) buf );
93a9cf2
+            overflow2(cols, rows);
93a9cf2
             if ( len != cols * rows )
93a9cf2
                 pm_message(
93a9cf2
                     "pixel data length (%d) does not match image size (%d)",
d776db3
diff --git a/converter/ppm/pcxtoppm.c b/converter/ppm/pcxtoppm.c
d776db3
index e252ba2..270ae3b 100644
d776db3
--- a/converter/ppm/pcxtoppm.c
d776db3
+++ b/converter/ppm/pcxtoppm.c
d776db3
@@ -409,6 +409,7 @@ pcx_planes_to_pixels(pixels, bitplanes, bytesperline, planes, bitsperpixel)
93a9cf2
     /*
93a9cf2
      * clear the pixel buffer
93a9cf2
      */
93a9cf2
+    overflow2(bytesperline, 8);
93a9cf2
     npixels = (bytesperline * 8) / bitsperpixel;
93a9cf2
     p    = pixels;
93a9cf2
     while (--npixels >= 0)
b3d2df7
@@ -470,6 +471,7 @@ pcx_16col_to_ppm(FILE *       const ifP,
93a9cf2
     }
93a9cf2
 
93a9cf2
     /*  BytesPerLine should be >= BitsPerPixel * cols / 8  */
93a9cf2
+    overflow2(BytesPerLine, 8);
93a9cf2
     rawcols = BytesPerLine * 8 / BitsPerPixel;
93a9cf2
     if (headerCols > rawcols) {
93a9cf2
         pm_message("warning - BytesPerLine = %d, "
d776db3
diff --git a/converter/ppm/picttoppm.c b/converter/ppm/picttoppm.c
9ec2837
index d412038..a59bf77 100644
d776db3
--- a/converter/ppm/picttoppm.c
d776db3
+++ b/converter/ppm/picttoppm.c
d776db3
@@ -1,3 +1,4 @@
93a9cf2
+#error "Unfixable. Don't ship me"
93a9cf2
 /*
93a9cf2
  * picttoppm.c -- convert a MacIntosh PICT file to PPM format.
93a9cf2
  *
d776db3
diff --git a/converter/ppm/pjtoppm.c b/converter/ppm/pjtoppm.c
d776db3
index 7b694fb..62ce77e 100644
d776db3
--- a/converter/ppm/pjtoppm.c
d776db3
+++ b/converter/ppm/pjtoppm.c
d776db3
@@ -127,20 +127,22 @@ main(argc, argv)
93a9cf2
                 case 'V':   /* send plane */
93a9cf2
                 case 'W':   /* send last plane */
93a9cf2
                     if (rows == -1 || r >= rows || image == NULL) {
93a9cf2
-                        if (rows == -1 || r >= rows)
93a9cf2
+                        if (rows == -1 || r >= rows) {
93a9cf2
+                            overflow_add(rows, 100);
93a9cf2
                             rows += 100;
93a9cf2
+                        }
d776db3
+
93a9cf2
                         if (image == NULL) {
93a9cf2
-                            MALLOCARRAY(image, rows * planes);
93a9cf2
-                            MALLOCARRAY(imlen, rows * planes);
93a9cf2
+                            image = (unsigned char **)
93a9cf2
+                                malloc3(rows , planes , sizeof(unsigned char *));
93a9cf2
+                            imlen = (int *) malloc3(rows , planes,  sizeof(int));
93a9cf2
                         }
93a9cf2
                         else {
d776db3
-                            image = (unsigned char **) 
93a9cf2
-                                realloc(image, 
93a9cf2
-                                        rows * planes * 
d776db3
-                                        sizeof(unsigned char *));
93a9cf2
-                            imlen = (int *) 
93a9cf2
-                                realloc(imlen, rows * planes * sizeof(int));
d776db3
-                        }
d776db3
+                            overflow2(rows,planes);
d776db3
+                            image = (unsigned char **)
d776db3
+                                realloc2(image, rows * planes,
d776db3
+                                    sizeof(unsigned char *));
d776db3
+                            imlen = (int *) realloc2(imlen, rows * planes, sizeof(int));                        }
93a9cf2
                     }
93a9cf2
                     if (image == NULL || imlen == NULL)
d776db3
                         pm_error("out of memory");
93a9cf2
@@ -212,8 +214,10 @@ main(argc, argv)
93a9cf2
                 for (i = 0, c = 0; c < imlen[p + r * planes]; c += 2)
93a9cf2
                     for (cmd = image[p + r * planes][c],
93a9cf2
                              val = image[p + r * planes][c+1]; 
93a9cf2
-                         cmd >= 0 && i < newcols; cmd--, i++) 
93a9cf2
+                         cmd >= 0 && i < newcols; cmd--, i++) {
93a9cf2
                         buf[i] = val;
93a9cf2
+                        overflow_add(i, 1);
93a9cf2
+                    }
93a9cf2
                 cols = cols > i ? cols : i;
93a9cf2
                 free(image[p + r * planes]);
93a9cf2
                 /* 
93a9cf2
@@ -224,6 +228,7 @@ main(argc, argv)
93a9cf2
                 image[p + r * planes] = (unsigned char *) realloc(buf, i);
93a9cf2
             }
93a9cf2
         }
93a9cf2
+        overflow2(cols, 8);
93a9cf2
         cols *= 8;
93a9cf2
     }
93a9cf2
             
d776db3
diff --git a/converter/ppm/ppmtoeyuv.c b/converter/ppm/ppmtoeyuv.c
d776db3
index f5ce115..6f072be 100644
d776db3
--- a/converter/ppm/ppmtoeyuv.c
d776db3
+++ b/converter/ppm/ppmtoeyuv.c
d776db3
@@ -114,6 +114,7 @@ create_multiplication_tables(const pixval maxval) {
cvsdist 499931c
 
93a9cf2
     int index;
93a9cf2
 
93a9cf2
+    overflow_add(maxval, 1);
93a9cf2
     MALLOCARRAY_NOFAIL(mult299   , maxval+1);
93a9cf2
     MALLOCARRAY_NOFAIL(mult587   , maxval+1);
93a9cf2
     MALLOCARRAY_NOFAIL(mult114   , maxval+1);
d776db3
diff --git a/converter/ppm/ppmtolj.c b/converter/ppm/ppmtolj.c
d776db3
index 7ed814e..b4e7db1 100644
d776db3
--- a/converter/ppm/ppmtolj.c
d776db3
+++ b/converter/ppm/ppmtolj.c
d776db3
@@ -182,6 +182,7 @@ int main(int argc, char *argv[]) {
93a9cf2
     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
93a9cf2
     pixelrow = ppm_allocrow( cols );
d776db3
 
93a9cf2
+    overflow2(cols, 6);
93a9cf2
     obuf = (unsigned char *) pm_allocrow(cols * 3, sizeof(unsigned char));
93a9cf2
     cbuf = (unsigned char *) pm_allocrow(cols * 6, sizeof(unsigned char));
93a9cf2
     if (mode == C_TRANS_MODE_DELTA)
d776db3
diff --git a/converter/ppm/ppmtomitsu.c b/converter/ppm/ppmtomitsu.c
d776db3
index e59f09b..1d2be20 100644
d776db3
--- a/converter/ppm/ppmtomitsu.c
d776db3
+++ b/converter/ppm/ppmtomitsu.c
b3d2df7
@@ -685,6 +685,8 @@ main(int argc, char * argv[]) {
93a9cf2
         medias = MSize_User;
1f08c10
 
b3d2df7
     if (dpi300) {
b3d2df7
+        overflow2(medias.maxcols, 2);
b3d2df7
+        overflow2(medias.maxrows, 2);
b3d2df7
         medias.maxcols *= 2;
b3d2df7
         medias.maxrows *= 2;
b3d2df7
     }
d776db3
diff --git a/converter/ppm/ppmtopcx.c b/converter/ppm/ppmtopcx.c
d776db3
index fa68edc..97dfb2b 100644
d776db3
--- a/converter/ppm/ppmtopcx.c
d776db3
+++ b/converter/ppm/ppmtopcx.c
d776db3
@@ -425,6 +425,8 @@ ppmTo16ColorPcx(pixel **            const pixels,
93a9cf2
             else                   Planes = 1;
93a9cf2
         }
1f08c10
     }
93a9cf2
+    overflow2(BitsPerPixel, cols);
93a9cf2
+    overflow_add(BitsPerPixel * cols, 7);
93a9cf2
     BytesPerLine = ((cols * BitsPerPixel) + 7) / 8;
93a9cf2
     MALLOCARRAY_NOFAIL(indexRow, cols);
93a9cf2
     MALLOCARRAY_NOFAIL(planesrow, BytesPerLine);
d776db3
diff --git a/converter/ppm/ppmtopict.c b/converter/ppm/ppmtopict.c
9ec2837
index 36464b6..c91ccf2 100644
d776db3
--- a/converter/ppm/ppmtopict.c
d776db3
+++ b/converter/ppm/ppmtopict.c
d776db3
@@ -450,6 +450,8 @@ main(int argc, const char ** argv) {
d60ce20
     putShort(stdout, 0);            /* mode */
d60ce20
 
d60ce20
     /* Finally, write out the data. */
d60ce20
+    overflow_add(cols/MAX_COUNT, 1);
d60ce20
+    overflow_add(cols, cols/MAX_COUNT+1);
d776db3
     outBuf = malloc((unsigned)(cols+cols/MAX_COUNT+1));
d776db3
     for (row = 0, oc = 0; row < rows; ++row) {
d776db3
         unsigned int rowSize;
d776db3
diff --git a/converter/ppm/ppmtopj.c b/converter/ppm/ppmtopj.c
d776db3
index d116773..fc84cac 100644
d776db3
--- a/converter/ppm/ppmtopj.c
d776db3
+++ b/converter/ppm/ppmtopj.c
93a9cf2
@@ -179,6 +179,7 @@ char *argv[];
93a9cf2
 	pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
cvsdist 499931c
 
93a9cf2
 	pm_close( ifp );
d776db3
+        overflow2(cols,2);
93a9cf2
 	obuf = (unsigned char *) pm_allocrow(cols, sizeof(unsigned char));
93a9cf2
 	cbuf = (unsigned char *) pm_allocrow(cols * 2, sizeof(unsigned char));
cvsdist 499931c
 
d776db3
diff --git a/converter/ppm/ppmtopjxl.c b/converter/ppm/ppmtopjxl.c
9ec2837
index 90bcef0..72d0027 100644
d776db3
--- a/converter/ppm/ppmtopjxl.c
d776db3
+++ b/converter/ppm/ppmtopjxl.c
9ec2837
@@ -267,6 +267,9 @@ main(int argc, const char * argv[]) {
b3d2df7
     if (maxval > PCL_MAXVAL)
b3d2df7
         pm_error("color range too large; reduce with ppmcscale");
d776db3
 
b3d2df7
+    if (cols < 0 || rows < 0)
b3d2df7
+        pm_error("negative size is not possible");
d776db3
+
b3d2df7
     /* Figure out the colormap. */
b3d2df7
     pm_message("Computing colormap...");
d776db3
     chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors);
9ec2837
@@ -286,6 +289,8 @@ main(int argc, const char * argv[]) {
b3d2df7
         case 0: /* direct mode (no palette) */
b3d2df7
             bpp = bitsperpixel(maxval); /* bits per pixel */
b3d2df7
             bpg = bpp; bpb = bpp;
d776db3
+            overflow2(bpp, 3);
d776db3
+            overflow_add(bpp*3, 7);
b3d2df7
             bpp = (bpp*3+7)>>3;     /* bytes per pixel now */
b3d2df7
             bpr = (bpp<<3)-bpg-bpb; 
b3d2df7
             bpp *= cols;            /* bytes per row now */
9ec2837
@@ -295,9 +300,13 @@ main(int argc, const char * argv[]) {
b3d2df7
         case 3: case 7: pclindex++;
b3d2df7
         default:
b3d2df7
             bpp = 8/pclindex;
d776db3
+            overflow_add(cols, bpp);
d776db3
+            if(bpp == 0)
d776db3
+                pm_error("assert: no bpp");
b3d2df7
             bpp = (cols+bpp-1)/bpp;      /* bytes per row */
b3d2df7
         }
b3d2df7
     }
b3d2df7
+    overflow2(bpp,2);
b3d2df7
     inrow = (char *)malloc((unsigned)bpp);
b3d2df7
     outrow = (char *)malloc((unsigned)bpp*2);
b3d2df7
     runcnt = (signed char *)malloc((unsigned)bpp);
d776db3
diff --git a/converter/ppm/ppmtowinicon.c b/converter/ppm/ppmtowinicon.c
d776db3
index c673798..af2b445 100644
d776db3
--- a/converter/ppm/ppmtowinicon.c
d776db3
+++ b/converter/ppm/ppmtowinicon.c
93a9cf2
@@ -12,6 +12,7 @@
acf309e
 
93a9cf2
 #include <math.h>
93a9cf2
 #include <string.h>
93a9cf2
+#include <stdlib.h>
acf309e
 
b3d2df7
 #include "pm_c_util.h"
93a9cf2
 #include "winico.h"
d776db3
@@ -214,6 +215,7 @@ createAndBitmap (gray ** const ba, int const cols, int const rows,
93a9cf2
    MALLOCARRAY_NOFAIL(rowData, rows);
93a9cf2
    icBitmap->xBytes = xBytes;
93a9cf2
    icBitmap->data   = rowData;
93a9cf2
+   overflow2(xBytes, rows);
93a9cf2
    icBitmap->size   = xBytes * rows;
93a9cf2
    for (y=0;y
93a9cf2
       u1 * row;
d776db3
@@ -342,6 +344,7 @@ create4Bitmap (pixel ** const pa, int const cols, int const rows,
93a9cf2
    MALLOCARRAY_NOFAIL(rowData, rows);
93a9cf2
    icBitmap->xBytes = xBytes;
93a9cf2
    icBitmap->data   = rowData;
93a9cf2
+   overflow2(xBytes, rows);
93a9cf2
    icBitmap->size   = xBytes * rows;
acf309e
 
93a9cf2
    for (y=0;y
d776db3
@@ -402,6 +405,7 @@ create8Bitmap (pixel ** const pa, int const cols, int const rows,
93a9cf2
    MALLOCARRAY_NOFAIL(rowData, rows);
93a9cf2
    icBitmap->xBytes = xBytes;
93a9cf2
    icBitmap->data   = rowData;
93a9cf2
+   overflow2(xBytes, rows);
93a9cf2
    icBitmap->size   = xBytes * rows;
93a9cf2
 
93a9cf2
    for (y=0;y
d776db3
@@ -709,7 +713,11 @@ addEntryToIcon(MS_Ico       const MSIconData,
93a9cf2
     entry->bitcount      = bpp;
93a9cf2
     entry->ih            = createInfoHeader(entry, xorBitmap, andBitmap);
93a9cf2
     entry->colors        = palette->colors;
d776db3
-    entry->size_in_bytes = 
93a9cf2
+    overflow2(4, entry->color_count);
93a9cf2
+    overflow_add(xorBitmap->size, andBitmap->size);
93a9cf2
+    overflow_add(xorBitmap->size + andBitmap->size, 40);
93a9cf2
+    overflow_add(xorBitmap->size + andBitmap->size + 40, 4 * entry->color_count);
d776db3
+    entry->size_in_bytes =
93a9cf2
         xorBitmap->size + andBitmap->size + 40 + (4 * entry->color_count);
93a9cf2
     if (verbose) 
d776db3
         pm_message("entry->size_in_bytes = %d + %d + %d = %d",
d776db3
diff --git a/converter/ppm/ppmtoxpm.c b/converter/ppm/ppmtoxpm.c
d776db3
index 38d9997..904c98d 100644
d776db3
--- a/converter/ppm/ppmtoxpm.c
d776db3
+++ b/converter/ppm/ppmtoxpm.c
d776db3
@@ -197,6 +197,7 @@ genNumstr(unsigned int const input, int const digits) {
93a9cf2
     unsigned int i;
93a9cf2
 
93a9cf2
     /* Allocate memory for printed number.  Abort if error. */
93a9cf2
+    overflow_add(digits, 1);
93a9cf2
     if (!(str = (char *) malloc(digits + 1)))
93a9cf2
         pm_error("out of memory");
93a9cf2
 
d60ce20
@@ -314,6 +315,7 @@ genCmap(colorhist_vector const chv,
93a9cf2
     unsigned int charsPerPixel;
93a9cf2
     unsigned int xpmMaxval;
93a9cf2
     
93a9cf2
+    if (includeTransparent) overflow_add(ncolors, 1);
93a9cf2
     MALLOCARRAY(cmap, cmapSize);
93a9cf2
     if (cmapP == NULL)
93a9cf2
         pm_error("Out of memory allocating %u bytes for a color map.",
d776db3
diff --git a/converter/ppm/qrttoppm.c b/converter/ppm/qrttoppm.c
d776db3
index 935463e..653084c 100644
d776db3
--- a/converter/ppm/qrttoppm.c
d776db3
+++ b/converter/ppm/qrttoppm.c
93a9cf2
@@ -46,7 +46,7 @@ main( argc, argv )
93a9cf2
 
93a9cf2
     ppm_writeppminit( stdout, cols, rows, maxval, 0 );
93a9cf2
     pixelrow = ppm_allocrow( cols );
93a9cf2
-    buf = (unsigned char *) malloc( 3 * cols );
93a9cf2
+    buf = (unsigned char *) malloc2( 3 , cols );
93a9cf2
     if ( buf == (unsigned char *) 0 )
93a9cf2
 	pm_error( "out of memory" );
93a9cf2
 
d776db3
diff --git a/converter/ppm/sldtoppm.c b/converter/ppm/sldtoppm.c
d776db3
index 6ba4cb4..fc6a498 100644
d776db3
--- a/converter/ppm/sldtoppm.c
d776db3
+++ b/converter/ppm/sldtoppm.c
d776db3
@@ -464,6 +464,8 @@ slider(slvecfn   slvec,
b3d2df7
     
93a9cf2
     /* Allocate image buffer and clear it to black. */
b3d2df7
     
d776db3
+    overflow_add(ixdots, 1);
d776db3
+    overflow_add(iydots, 1);
93a9cf2
     pixels = ppm_allocarray(pixcols = ixdots + 1, pixrows = iydots + 1);
93a9cf2
     PPM_ASSIGN(rgbcolor, 0, 0, 0);
93a9cf2
     ppmd_filledrectangle(pixels, pixcols, pixrows, pixmaxval, 0, 0,
d776db3
diff --git a/converter/ppm/ximtoppm.c b/converter/ppm/ximtoppm.c
d776db3
index ce5e639..a39b689 100644
d776db3
--- a/converter/ppm/ximtoppm.c
d776db3
+++ b/converter/ppm/ximtoppm.c
b3d2df7
@@ -117,6 +117,7 @@ ReadXimHeader(FILE *     const in_fp,
93a9cf2
     header->bits_channel = atoi(a_head.bits_per_channel);
93a9cf2
     header->alpha_flag = atoi(a_head.alpha_channel);
93a9cf2
     if (strlen(a_head.author)) {
d776db3
+        overflow_add(strlen(a_head.author),1);
93a9cf2
         if (!(header->author = calloc((unsigned int)strlen(a_head.author)+1,
93a9cf2
                 1))) {
93a9cf2
             pm_message("ReadXimHeader: can't calloc author string" );
b3d2df7
@@ -126,6 +127,7 @@ ReadXimHeader(FILE *     const in_fp,
93a9cf2
         strncpy(header->author, a_head.author, strlen(a_head.author));
1f08c10
     }
93a9cf2
     if (strlen(a_head.date)) {
93a9cf2
+        overflow_add(strlen(a_head.date),1);
93a9cf2
         if (!(header->date =calloc((unsigned int)strlen(a_head.date)+1,1))){
93a9cf2
             pm_message("ReadXimHeader: can't calloc date string" );
93a9cf2
             return(0);
b3d2df7
@@ -134,6 +136,7 @@ ReadXimHeader(FILE *     const in_fp,
93a9cf2
         strncpy(header->date, a_head.date, strlen(a_head.date));
93a9cf2
     }
93a9cf2
     if (strlen(a_head.program)) {
93a9cf2
+        overflow_add(strlen(a_head.program),1);
93a9cf2
         if (!(header->program = calloc(
93a9cf2
                     (unsigned int)strlen(a_head.program) + 1, 1))) {
93a9cf2
             pm_message("ReadXimHeader: can't calloc program string" );
b3d2df7
@@ -160,6 +163,7 @@ ReadXimHeader(FILE *     const in_fp,
93a9cf2
     if (header->nchannels == 3 && header->bits_channel == 8)
93a9cf2
         header->ncolors = 0;
93a9cf2
     else if (header->nchannels == 1 && header->bits_channel == 8) {
d776db3
+        overflow2(header->ncolors, sizeof(Color));
93a9cf2
         header->colors = (Color *)calloc((unsigned int)header->ncolors,
93a9cf2
                 sizeof(Color));
93a9cf2
         if (header->colors == NULL) {
d776db3
diff --git a/editor/pamcut.c b/editor/pamcut.c
d776db3
index 7c41af3..72df687 100644
d776db3
--- a/editor/pamcut.c
d776db3
+++ b/editor/pamcut.c
d776db3
@@ -655,6 +655,8 @@ cutOneImage(FILE *             const ifP,
1f08c10
 
b3d2df7
     outpam = inpam;    /* Initial value -- most fields should be same */
b3d2df7
     outpam.file   = ofP;
93a9cf2
+    overflow_add(rightcol, 1);
b3d2df7
+    overflow_add(bottomrow, 1);
b3d2df7
     outpam.width  = rightcol - leftcol + 1;
b3d2df7
     outpam.height = bottomrow - toprow + 1;
b3d2df7
 
d776db3
diff --git a/editor/pnmgamma.c b/editor/pnmgamma.c
d776db3
index b357b0d..ec612d3 100644
d776db3
--- a/editor/pnmgamma.c
d776db3
+++ b/editor/pnmgamma.c
d776db3
@@ -596,6 +596,7 @@ createGammaTables(enum transferFunction const transferFunction,
93a9cf2
                   xelval **             const btableP) {
cvsdist 499931c
 
93a9cf2
     /* Allocate space for the tables. */
93a9cf2
+    overflow_add(maxval, 1);
93a9cf2
     MALLOCARRAY(*rtableP, maxval+1);
93a9cf2
     MALLOCARRAY(*gtableP, maxval+1);
93a9cf2
     MALLOCARRAY(*btableP, maxval+1);
d776db3
diff --git a/editor/pnmhisteq.c b/editor/pnmhisteq.c
d776db3
index 8af4201..0c8d6e5 100644
d776db3
--- a/editor/pnmhisteq.c
d776db3
+++ b/editor/pnmhisteq.c
d776db3
@@ -107,6 +107,7 @@ computeLuminosityHistogram(xel * const *   const xels,
93a9cf2
     unsigned int pixelCount;
93a9cf2
     unsigned int * lumahist;
cvsdist 499931c
 
93a9cf2
+    overflow_add(maxval, 1);
93a9cf2
     MALLOCARRAY(lumahist, maxval + 1);
93a9cf2
     if (lumahist == NULL)
93a9cf2
         pm_error("Out of storage allocating array for %u histogram elements",
d776db3
diff --git a/editor/pnmindex.csh b/editor/pnmindex.csh
d776db3
index c6f1e84..c513a84 100755
d776db3
--- a/editor/pnmindex.csh
d776db3
+++ b/editor/pnmindex.csh
d776db3
@@ -1,5 +1,7 @@
93a9cf2
 #!/bin/csh -f
93a9cf2
 #
93a9cf2
+echo "Unsafe code, needs debugging, do not ship"
93a9cf2
+exit 1
93a9cf2
 # pnmindex - build a visual index of a bunch of anymaps
93a9cf2
 #
93a9cf2
 # Copyright (C) 1991 by Jef Poskanzer.
d776db3
diff --git a/editor/pnmpad.c b/editor/pnmpad.c
9ec2837
index 168b73e..c248924 100644
d776db3
--- a/editor/pnmpad.c
d776db3
+++ b/editor/pnmpad.c
9ec2837
@@ -631,6 +631,8 @@ main(int argc, const char ** argv) {
cvsdist 499931c
 
1f08c10
     computePadSizes(cmdline, cols, rows, &lpad, &rpad, &tpad, &bpad);
1f08c10
 
1f08c10
+    overflow_add(cols, lpad);
1f08c10
+    overflow_add(cols + lpad, rpad);
1f08c10
     newcols = cols + lpad + rpad;
cvsdist 499931c
 
b3d2df7
     if (PNM_FORMAT_TYPE(format) == PBM_TYPE)
d776db3
diff --git a/editor/pnmremap.c b/editor/pnmremap.c
9ec2837
index ed758aa..73968be 100644
d776db3
--- a/editor/pnmremap.c
d776db3
+++ b/editor/pnmremap.c
9ec2837
@@ -430,6 +430,7 @@ initFserr(struct pam *   const pamP,
cvsdist 499931c
 
b3d2df7
     unsigned int const fserrSize = pamP->width + 2;
d776db3
 
93a9cf2
+    overflow_add(pamP->width, 2);
b3d2df7
     fserrP->width = pamP->width;
b3d2df7
 
93a9cf2
     MALLOCARRAY(fserrP->thiserr, pamP->depth);
9ec2837
@@ -467,6 +468,7 @@ floydInitRow(struct pam * const pamP, struct fserr * const fserrP) {
cvsdist 499931c
 
93a9cf2
     int col;
93a9cf2
     
93a9cf2
+    overflow_add(pamP->width, 2);
93a9cf2
     for (col = 0; col < pamP->width + 2; ++col) {
93a9cf2
         unsigned int plane;
93a9cf2
         for (plane = 0; plane < pamP->depth; ++plane) 
d776db3
diff --git a/editor/pnmscalefixed.c b/editor/pnmscalefixed.c
d776db3
index 884ca31..747cd8f 100644
d776db3
--- a/editor/pnmscalefixed.c
d776db3
+++ b/editor/pnmscalefixed.c
d776db3
@@ -214,6 +214,7 @@ compute_output_dimensions(const struct cmdline_info cmdline,
93a9cf2
                           const int rows, const int cols,
93a9cf2
                           int * newrowsP, int * newcolsP) {
21b205c
 
93a9cf2
+    overflow2(rows, cols);
93a9cf2
     if (cmdline.pixels) {
93a9cf2
         if (rows * cols <= cmdline.pixels) {
93a9cf2
             *newrowsP = rows;
d776db3
@@ -265,6 +266,8 @@ compute_output_dimensions(const struct cmdline_info cmdline,
cvsdist 499931c
 
93a9cf2
     if (*newcolsP < 1) *newcolsP = 1;
93a9cf2
     if (*newrowsP < 1) *newrowsP = 1;
d776db3
+
93a9cf2
+    overflow2(*newcolsP, *newrowsP);
93a9cf2
 }        
93a9cf2
 
93a9cf2
 
d776db3
@@ -446,6 +449,9 @@ main(int argc, char **argv ) {
93a9cf2
        unfilled.  We can address that by stretching, whereas the other
93a9cf2
        case would require throwing away some of the input.
93a9cf2
     */
d776db3
+
93a9cf2
+    overflow2(newcols, SCALE);
93a9cf2
+    overflow2(newrows, SCALE);
93a9cf2
     sxscale = SCALE * newcols / cols;
93a9cf2
     syscale = SCALE * newrows / rows;
93a9cf2
 
d776db3
diff --git a/editor/ppmdither.c b/editor/ppmdither.c
d776db3
index ec1b977..e701e09 100644
d776db3
--- a/editor/ppmdither.c
d776db3
+++ b/editor/ppmdither.c
d776db3
@@ -356,6 +356,11 @@ dithMatrix(unsigned int const dithPower) {
d60ce20
             (dithDim * sizeof(*dithMat)) + /* pointers */
d60ce20
             (dithDim * dithDim * sizeof(**dithMat)); /* data */
d776db3
         
d60ce20
+
d60ce20
+        overflow2(dithDim, sizeof(*dithMat));
d60ce20
+        overflow3(dithDim, dithDim, sizeof(**dithMat));
d60ce20
+        overflow_add(dithDim * sizeof(*dithMat), dithDim * dithDim * sizeof(**dithMat));
d60ce20
+
d60ce20
         dithMat = malloc(dithMatSize);
d60ce20
         
d60ce20
         if (dithMat == NULL) 
d776db3
diff --git a/editor/specialty/pamoil.c b/editor/specialty/pamoil.c
d776db3
index 6cb8d3a..6f4bde9 100644
d776db3
--- a/editor/specialty/pamoil.c
d776db3
+++ b/editor/specialty/pamoil.c
b3d2df7
@@ -112,6 +112,7 @@ main(int argc, char *argv[] ) {
b3d2df7
     tuples = pnm_readpam(ifp, &inpam, PAM_STRUCT_SIZE(tuple_type));
b3d2df7
     pm_close(ifp);
b3d2df7
 
b3d2df7
+    overflow_add(inpam.maxval, 1);
b3d2df7
     MALLOCARRAY(hist, inpam.maxval + 1);
b3d2df7
     if (hist == NULL)
b3d2df7
         pm_error("Unable to allocate memory for histogram.");
d776db3
diff --git a/generator/pbmtext.c b/generator/pbmtext.c
9ec2837
index cfb858a..e5c7bd1 100644
d776db3
--- a/generator/pbmtext.c
d776db3
+++ b/generator/pbmtext.c
9ec2837
@@ -122,8 +122,10 @@ parseCommandLine(int argc, const char ** argv,
93a9cf2
         
b3d2df7
         for (i = 1; i < argc; ++i) {
93a9cf2
             if (i > 1) {
93a9cf2
+                overflow_add(totaltextsize, 1);
93a9cf2
                 strcat(text, " ");
d776db3
-            } 
d776db3
+            }
93a9cf2
+            overflow_add(totaltextsize, strlen(argv[i]));
9ec2837
             totaltextsize += strlen(argv[i]) + 1;
9ec2837
             if (totaltextsize > MAXLINECHARS)
9ec2837
                 pm_error("input text too long");
9ec2837
@@ -744,6 +746,7 @@ getText(char          const cmdlineText[],
b3d2df7
                 pm_error("A line of input text is longer than %u characters."
9ec2837
                          "Cannot process", (unsigned int) MAXLINECHARS-1);
93a9cf2
             if (lineCount >= maxlines) {
d776db3
+                overflow2(maxlines, 2);
93a9cf2
                 maxlines *= 2;
9ec2837
                 REALLOCARRAY(textArray, maxlines);
9ec2837
                 if (textArray == NULL)
d776db3
diff --git a/lib/libpam.c b/lib/libpam.c
d776db3
index cc6368e..4e10572 100644
d776db3
--- a/lib/libpam.c
d776db3
+++ b/lib/libpam.c
d776db3
@@ -224,8 +224,9 @@ allocPamRow(const struct pam * const pamP) {
d60ce20
     unsigned int const bytesPerTuple = allocationDepth(pamP) * sizeof(sample);
93a9cf2
     tuple * tuplerow;
93a9cf2
 
93a9cf2
-    tuplerow = malloc(pamP->width * (sizeof(tuple *) + bytesPerTuple));
d776db3
-                      
93a9cf2
+    overflow_add(sizeof(tuple *), bytesPerTuple);
d60ce20
+    tuplerow = malloc2(pamP->width, (sizeof(tuple *) + bytesPerTuple));
d776db3
+
93a9cf2
     if (tuplerow != NULL) {
93a9cf2
         /* Now we initialize the pointers to the individual tuples
d776db3
            to make this a regulation C two dimensional array.  
d776db3
diff --git a/lib/libpammap.c b/lib/libpammap.c
9ec2837
index 2222491..ba27a4c 100644
d776db3
--- a/lib/libpammap.c
d776db3
+++ b/lib/libpammap.c
d776db3
@@ -108,7 +108,9 @@ allocTupleIntListItem(struct pam * const pamP) {
93a9cf2
     */
93a9cf2
     struct tupleint_list_item * retval;
93a9cf2
 
d776db3
-    unsigned int const size = 
93a9cf2
+    overflow2(pamP->depth, sizeof(sample));
93a9cf2
+    overflow_add(sizeof(*retval)-sizeof(retval->tupleint.tuple), pamP->depth*sizeof(sample));
d776db3
+    unsigned int const size =
93a9cf2
         sizeof(*retval) - sizeof(retval->tupleint.tuple) 
93a9cf2
         + pamP->depth * sizeof(sample);
1f08c10
 
d776db3
diff --git a/lib/libpm.c b/lib/libpm.c
d776db3
index 4374bbe..5ab7f83 100644
d776db3
--- a/lib/libpm.c
d776db3
+++ b/lib/libpm.c
d776db3
@@ -841,5 +841,53 @@ pm_parse_height(const char * const arg) {
d776db3
     return height;
d776db3
 }
1f08c10
 
93a9cf2
+/*
93a9cf2
+ *	Maths wrapping
1f08c10
+ */
d776db3
 
93a9cf2
+void __overflow2(int a, int b)
93a9cf2
+{
d776db3
+        if(a < 0 || b < 0)
d776db3
+                pm_error("object too large");
d776db3
+        if(b == 0)
d776db3
+                return;
d776db3
+        if(a > INT_MAX / b)
d776db3
+                pm_error("object too large");
93a9cf2
+}
93a9cf2
+
93a9cf2
+void overflow3(int a, int b, int c)
93a9cf2
+{
d776db3
+        overflow2(a,b);
d776db3
+        overflow2(a*b, c);
93a9cf2
+}
93a9cf2
+
93a9cf2
+void overflow_add(int a, int b)
93a9cf2
+{
d776db3
+        if( a > INT_MAX - b)
d776db3
+                pm_error("object too large");
93a9cf2
+}
93a9cf2
+
93a9cf2
+void *malloc2(int a, int b)
93a9cf2
+{
d776db3
+        overflow2(a, b);
d776db3
+        if(a*b == 0)
d776db3
+                pm_error("Zero byte allocation");
d776db3
+        return malloc(a*b);
93a9cf2
+}
93a9cf2
+
93a9cf2
+void *malloc3(int a, int b, int c)
93a9cf2
+{
d776db3
+        overflow3(a, b, c);
d776db3
+        if(a*b*c == 0)
d776db3
+                pm_error("Zero byte allocation");
d776db3
+        return malloc(a*b*c);
93a9cf2
+}
93a9cf2
+
93a9cf2
+void *realloc2(void * a, int b, int c)
93a9cf2
+{
d776db3
+        overflow2(b, c);
d776db3
+        if(b*c == 0)
d776db3
+                pm_error("Zero byte allocation");
d776db3
+        return realloc(a, b*c);
93a9cf2
+}
cvsdist 499931c
 
d776db3
diff --git a/lib/pm.h b/lib/pm.h
9ec2837
index 47cbfe8..5005df2 100644
d776db3
--- a/lib/pm.h
d776db3
+++ b/lib/pm.h
9ec2837
@@ -434,5 +434,12 @@ pm_parse_height(const char * const arg);
d776db3
 }
93a9cf2
 #endif
cvsdist 499931c
 
93a9cf2
+void *malloc2(int, int);
93a9cf2
+void *malloc3(int, int, int);
93a9cf2
+#define overflow2(a,b) __overflow2(a,b)
93a9cf2
+void __overflow2(int, int);
93a9cf2
+void overflow3(int, int, int);
93a9cf2
+void overflow_add(int, int);
93a9cf2
+
d776db3
 
93a9cf2
 #endif
d776db3
diff --git a/other/pnmcolormap.c b/other/pnmcolormap.c
d776db3
index 57db432..7195295 100644
d776db3
--- a/other/pnmcolormap.c
d776db3
+++ b/other/pnmcolormap.c
d776db3
@@ -840,6 +840,7 @@ colormapToSquare(struct pam * const pamP,
93a9cf2
             pamP->width = intsqrt;
93a9cf2
         else 
93a9cf2
             pamP->width = intsqrt + 1;
93a9cf2
+            overflow_add(intsqrt, 1);
93a9cf2
     }
93a9cf2
     {
93a9cf2
         unsigned int const intQuotient = colormap.size / pamP->width;
d776db3
diff --git a/urt/Runput.c b/urt/Runput.c
d776db3
index 3bc562a..645a376 100644
d776db3
--- a/urt/Runput.c
d776db3
+++ b/urt/Runput.c
d776db3
@@ -202,10 +202,11 @@ RunSetup(rle_hdr * the_hdr)
d776db3
     if ( the_hdr->background != 0 )
d776db3
     {
d776db3
 	register int i;
d776db3
-	register rle_pixel *background =
d776db3
-	    (rle_pixel *)malloc( (unsigned)(the_hdr->ncolors + 1) );
d776db3
-	register int *bg_color;
d776db3
-	/* 
d776db3
+        register rle_pixel *background;
d776db3
+        register int *bg_color;
93a9cf2
+
d776db3
+        overflow_add(the_hdr->ncolors,1);
d776db3
+        background = (rle_pixel *)malloc( (unsigned)(the_hdr->ncolors + 1) );	/*
d776db3
 	 * If even number of bg color bytes, put out one more to get to 
d776db3
 	 * 16 bit boundary.
d776db3
 	 */
d776db3
@@ -224,7 +225,7 @@ RunSetup(rle_hdr * the_hdr)
d776db3
 	/* Big-endian machines are harder */
d776db3
 	register int i, nmap = (1 << the_hdr->cmaplen) *
d776db3
 			       the_hdr->ncmap;
d776db3
-	register char *h_cmap = (char *)malloc( nmap * 2 );
d776db3
+        register char *h_cmap = (char *)malloc2( nmap, 2 );
d776db3
 	if ( h_cmap == NULL )
d776db3
 	{
d776db3
 	    fprintf( stderr,
d776db3
diff --git a/urt/rle.h b/urt/rle.h
9ec2837
index 0766d22..c80a5fa 100644
d776db3
--- a/urt/rle.h
d776db3
+++ b/urt/rle.h
9ec2837
@@ -160,6 +160,17 @@ rle_hdr             /* End of typedef. */
a3c9c4b
  */
d776db3
 extern rle_hdr rle_dflt_hdr;
d776db3
 
d776db3
+/*
d776db3
+ * Provided by pm library
d776db3
+ */
d776db3
+
d776db3
+extern void overflow_add(int, int);
d776db3
+#define overflow2(a,b) __overflow2(a,b)
d776db3
+extern void __overflow2(int, int);
d776db3
+extern void overflow3(int, int, int);
d776db3
+extern void *malloc2(int, int);
d776db3
+extern void *malloc3(int, int, int);
d776db3
+extern void *realloc2(void *, int, int);
d776db3
 
d776db3
 /* Declare RLE library routines. */
d776db3
 
d776db3
diff --git a/urt/rle_addhist.c b/urt/rle_addhist.c
d776db3
index b165175..e09ed94 100644
d776db3
--- a/urt/rle_addhist.c
d776db3
+++ b/urt/rle_addhist.c
d776db3
@@ -70,13 +70,18 @@ rle_addhist(char *          argv[],
acf309e
         return;
acf309e
     
acf309e
     length = 0;
acf309e
-    for (i = 0; argv[i]; ++i)
acf309e
+    for (i = 0; argv[i]; ++i) {
d776db3
+        overflow_add(length, strlen(argv[i]));
d776db3
+        overflow_add(length+1, strlen(argv[i]));
acf309e
         length += strlen(argv[i]) +1;   /* length of each arg plus space. */
acf309e
+    }
cvsdist 499931c
 
acf309e
     time(&temp);
acf309e
     timedate = ctime(&temp);
acf309e
     length += strlen(timedate);        /* length of date and time in ASCII. */
d776db3
-
acf309e
+    overflow_add(strlen(padding), 4);
acf309e
+    overflow_add(strlen(histoire), strlen(padding) + 4);
acf309e
+    overflow_add(length, strlen(histoire) + strlen(padding) + 4);
acf309e
     length += strlen(padding) + 3 + strlen(histoire) + 1;
acf309e
         /* length of padding, "on "  and length of history name plus "="*/
acf309e
     if (in_hdr) /* if we are interested in the old comments... */
d776db3
@@ -84,8 +89,10 @@ rle_addhist(char *          argv[],
acf309e
     else
acf309e
         old = NULL;
acf309e
     
acf309e
-    if (old && *old)
acf309e
+    if (old && *old) {
d776db3
+        overflow_add(length, strlen(old));
acf309e
         length += strlen(old);       /* add length if there. */
acf309e
+    }
cvsdist 499931c
 
acf309e
     ++length;                               /*Cater for the null. */
cvsdist 499931c
 
d776db3
diff --git a/urt/rle_getrow.c b/urt/rle_getrow.c
9ec2837
index 679811c..cc1f5cb 100644
d776db3
--- a/urt/rle_getrow.c
d776db3
+++ b/urt/rle_getrow.c
9ec2837
@@ -160,6 +160,7 @@ rle_get_setup(rle_hdr * const the_hdr) {
9ec2837
         char * cp;
93a9cf2
 
9ec2837
         VAXSHORT(comlen, infile); /* get comment length */
d776db3
+        overflow_add(comlen, 1);
93a9cf2
         evenlen = (comlen + 1) & ~1;    /* make it even */
9ec2837
         if (evenlen) {
9ec2837
             MALLOCARRAY(comment_buf, evenlen);
d776db3
diff --git a/urt/rle_hdr.c b/urt/rle_hdr.c
d776db3
index 1611324..7c9c010 100644
d776db3
--- a/urt/rle_hdr.c
d776db3
+++ b/urt/rle_hdr.c
d776db3
@@ -80,7 +80,10 @@ int img_num;
1f08c10
     /* Fill in with copies of the strings. */
1f08c10
     if ( the_hdr->cmd != pgmname )
1f08c10
     {
1f08c10
-	char *tmp = (char *)malloc( strlen( pgmname ) + 1 );
d776db3
+        char *tmp;
1f08c10
+
d776db3
+        overflow_add(strlen(pgmname), 1);
d776db3
+        tmp = malloc( strlen(pgmname) + 1 );
1f08c10
 	RLE_CHECK_ALLOC( pgmname, tmp, 0 );
1f08c10
 	strcpy( tmp, pgmname );
1f08c10
 	the_hdr->cmd = tmp;
d776db3
@@ -88,8 +91,10 @@ int img_num;
1f08c10
 
1f08c10
     if ( the_hdr->file_name != fname )
1f08c10
     {
1f08c10
-	char *tmp = (char *)malloc( strlen( fname ) + 1 );
d776db3
-	RLE_CHECK_ALLOC( pgmname, tmp, 0 );
d776db3
+        char *tmp;
d776db3
+        overflow_add(strlen(fname), 1);
d776db3
+        tmp = malloc( strlen( fname ) + 1 );
d776db3
+        RLE_CHECK_ALLOC( pgmname, tmp, 0 );
1f08c10
 	strcpy( tmp, fname );
1f08c10
 	the_hdr->file_name = tmp;
d776db3
     }
d776db3
@@ -153,6 +158,7 @@ rle_hdr *from_hdr, *to_hdr;
1f08c10
     if ( to_hdr->bg_color )
1f08c10
     {
1f08c10
 	int size = to_hdr->ncolors * sizeof(int);
d776db3
+        overflow2(to_hdr->ncolors, sizeof(int));
1f08c10
 	to_hdr->bg_color = (int *)malloc( size );
1f08c10
 	RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->bg_color, "background color" );
1f08c10
 	memcpy( to_hdr->bg_color, from_hdr->bg_color, size );
d776db3
@@ -161,7 +167,7 @@ rle_hdr *from_hdr, *to_hdr;
1f08c10
     if ( to_hdr->cmap )
1f08c10
     {
1f08c10
 	int size = to_hdr->ncmap * (1 << to_hdr->cmaplen) * sizeof(rle_map);
1f08c10
-	to_hdr->cmap = (rle_map *)malloc( size );
d776db3
+        to_hdr->cmap = (rle_map *)malloc3( to_hdr->ncmap, 1<<to_hdr->cmaplen, sizeof(rle_map));
1f08c10
 	RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->cmap, "color map" );
1f08c10
 	memcpy( to_hdr->cmap, from_hdr->cmap, size );
1f08c10
     }
d776db3
@@ -173,12 +179,17 @@ rle_hdr *from_hdr, *to_hdr;
d776db3
     {
1f08c10
 	int size = 0;
1f08c10
 	CONST_DECL char **cp;
d776db3
-	for ( cp=to_hdr->comments; *cp; cp++ )
d776db3
+        for ( cp=to_hdr->comments; *cp; cp++ )
d776db3
+        {
d776db3
+            overflow_add(size, 1);
1f08c10
 	    size++;		/* Count the comments. */
d776db3
+        }
1f08c10
 	/* Check if there are really any comments. */
1f08c10
 	if ( size )
1f08c10
 	{
d776db3
+            overflow_add(size, 1);
1f08c10
 	    size++;		/* Copy the NULL pointer, too. */
d776db3
+            overflow2(size, sizeof(char *));
1f08c10
 	    size *= sizeof(char *);
1f08c10
 	    to_hdr->comments = (CONST_DECL char **)malloc( size );
1f08c10
 	    RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->comments, "comments" );
d776db3
diff --git a/urt/rle_open_f.c b/urt/rle_open_f.c
d776db3
index ae8548b..c2ef37d 100644
d776db3
--- a/urt/rle_open_f.c
d776db3
+++ b/urt/rle_open_f.c
d776db3
@@ -163,65 +163,7 @@ dealWithSubprocess(const char *  const file_name,
d60ce20
                    FILE **       const fpP,
b3d2df7
                    bool *        const noSubprocessP,
b3d2df7
                    const char ** const errorP) {
d60ce20
-
b3d2df7
-#ifdef NO_OPEN_PIPES
b3d2df7
     *noSubprocessP = TRUE;
b3d2df7
-#else
b3d2df7
-    const char *cp;
b3d2df7
-
b3d2df7
-    reapChildren(catchingChildrenP, pids);
b3d2df7
-
b3d2df7
-    /*  Real file, not stdin or stdout.  If name ends in ".Z",
b3d2df7
-     *  pipe from/to un/compress (depending on r/w mode).
b3d2df7
-     *  
b3d2df7
-     *  If it starts with "|", popen that command.
b3d2df7
-     */
b3d2df7
-        
b3d2df7
-    cp = file_name + strlen(file_name) - 2;
b3d2df7
-    /* Pipe case. */
b3d2df7
-    if (file_name[0] == '|') {
b3d2df7
-        pid_t thepid;     /* PID from my_popen */
b3d2df7
-
b3d2df7
-        *noSubprocessP = FALSE;
b3d2df7
-
b3d2df7
-        *fpP = my_popen(file_name + 1, mode, &thepid);
b3d2df7
-        if (*fpP == NULL)
b3d2df7
-            *errorP = "%s: can't invoke <<%s>> for %s: ";
b3d2df7
-        else {
b3d2df7
-            /* One more child to catch, eventually. */
b3d2df7
-            if (*catchingChildrenP < MAX_CHILDREN)
b3d2df7
-                pids[(*catchingChildrenP)++] = thepid;
b3d2df7
-        }
b3d2df7
-    } else if (cp > file_name && *cp == '.' && *(cp + 1) == 'Z' ) {
b3d2df7
-        /* Compress case. */
b3d2df7
-        pid_t thepid;     /* PID from my_popen. */
b3d2df7
-        const char * command;
b3d2df7
-
b3d2df7
-        *noSubprocessP = FALSE;
b3d2df7
-        
b3d2df7
-        if (*mode == 'w')
d60ce20
-            pm_asprintf(&command, "compress > %s", file_name);
b3d2df7
-        else if (*mode == 'a')
d60ce20
-            pm_asprintf(&command, "compress >> %s", file_name);
b3d2df7
-        else
d60ce20
-            pm_asprintf(&command, "compress -d < %s", file_name);
b3d2df7
-        
b3d2df7
-        *fpP = my_popen(command, mode, &thepid);
b3d2df7
-
b3d2df7
-        if (*fpP == NULL)
b3d2df7
-            *errorP = "%s: can't invoke 'compress' program, "
b3d2df7
-                "trying to open %s for %s";
b3d2df7
-        else {
b3d2df7
-            /* One more child to catch, eventually. */
b3d2df7
-            if (*catchingChildrenP < MAX_CHILDREN)
b3d2df7
-                pids[(*catchingChildrenP)++] = thepid;
b3d2df7
-        }
d60ce20
-        pm_strfree(command);
b3d2df7
-    } else {
b3d2df7
-        *noSubprocessP = TRUE;
b3d2df7
-        *errorP = NULL;
b3d2df7
-    }
b3d2df7
-#endif
b3d2df7
 }
93a9cf2
 
93a9cf2
 
d776db3
diff --git a/urt/rle_putcom.c b/urt/rle_putcom.c
9ec2837
index ab2eb20..ce83615 100644
d776db3
--- a/urt/rle_putcom.c
d776db3
+++ b/urt/rle_putcom.c
d776db3
@@ -98,12 +98,14 @@ rle_putcom(const char * const value,
93a9cf2
         const char * v;
93a9cf2
         const char ** old_comments;
93a9cf2
         int i;
93a9cf2
-        for (i = 2, cp = the_hdr->comments; *cp != NULL; ++i, ++cp)
93a9cf2
+        for (i = 2, cp = the_hdr->comments; *cp != NULL; ++i, ++cp) {
d776db3
+            overflow_add(i, 1);
93a9cf2
             if (match(value, *cp) != NULL) {
93a9cf2
                 v = *cp;
93a9cf2
                 *cp = value;
93a9cf2
                 return v;
93a9cf2
             }
d776db3
+        }
93a9cf2
         /* Not found */
93a9cf2
         /* Can't realloc because somebody else might be pointing to this
93a9cf2
          * comments block.  Of course, if this were true, then the
d776db3
diff --git a/urt/scanargs.c b/urt/scanargs.c
d776db3
index f3af334..5e114bb 100644
d776db3
--- a/urt/scanargs.c
d776db3
+++ b/urt/scanargs.c
d776db3
@@ -62,9 +62,8 @@ typedef int *ptr;
a3c9c4b
 /* 
93a9cf2
  * Storage allocation macros
93a9cf2
  */
93a9cf2
-#define NEW( type, cnt )	(type *) malloc( (cnt) * sizeof( type ) )
93a9cf2
-#define RENEW( type, ptr, cnt )	(type *) realloc( ptr, (cnt) * sizeof( type ) )
d776db3
-
93a9cf2
+#define NEW( type, cnt )	(type *) malloc2( (cnt) , sizeof( type ) )
93a9cf2
+#define RENEW( type, ptr, cnt )	(type *) realloc2( ptr, (cnt), sizeof( type ) )
717bd45
 static CONST_DECL char * prformat( CONST_DECL char *, int );
717bd45
 static int isnum( CONST_DECL char *, int, int );
d776db3
 static int	_do_scanargs( int argc, char **argv, CONST_DECL char *format,