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