Blame SOURCES/netpbm-security-code.patch

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