Blame SOURCES/netpbm-security-code.patch

294826
diff -ruNp a/analyzer/pgmtexture.c b/analyzer/pgmtexture.c
294826
--- a/analyzer/pgmtexture.c	2021-06-02 15:53:59.866205957 +0200
294826
+++ b/analyzer/pgmtexture.c	2021-06-02 16:13:04.906252648 +0200
294826
@@ -54,6 +54,7 @@ vector(unsigned int const nl,
294826
 
294826
     assert(nh >= nl);
294826
 
294826
+    overflow_add(nh - nl, 1);
294826
     MALLOCARRAY(v, (unsigned) (nh - nl + 1));
294826
 
294826
     if (v == NULL)
294826
@@ -85,6 +86,7 @@ matrix (unsigned int const nrl,
294826
     assert(nrh >= nrl);
294826
 
294826
     /* allocate pointers to rows */
294826
+    overflow_add(nrh - nrl, 1);
294826
     MALLOCARRAY(m, (unsigned) (nrh - nrl + 1));
294826
     if (m == NULL)
294826
         pm_error("Unable to allocate memory for a matrix.");
294826
@@ -93,6 +95,7 @@ matrix (unsigned int const nrl,
294826
 
294826
     assert (nch >= ncl);
294826
 
294826
+    overflow_add(nch - ncl, 1);
294826
     /* allocate rows and set pointers to them */
294826
     for (i = nrl; i <= nrh; ++i) {
294826
         MALLOCARRAY(m[i], (unsigned) (nch - ncl + 1));
294826
diff -ruNp a/converter/other/gemtopnm.c b/converter/other/gemtopnm.c
294826
--- a/converter/other/gemtopnm.c	2021-06-02 15:53:59.835205659 +0200
294826
+++ b/converter/other/gemtopnm.c	2021-06-02 16:15:29.598641385 +0200
294826
@@ -106,6 +106,7 @@ main(argc, argv)
294826
 
294826
     pnm_writepnminit( stdout, cols, rows, MAXVAL, type, 0 );
294826
 
294826
+    overflow_add(cols, padright);
294826
     {
294826
         /* allocate input row data structure */
294826
         int plane;
294826
diff -ruNp a/converter/other/jpegtopnm.c b/converter/other/jpegtopnm.c
294826
--- a/converter/other/jpegtopnm.c	2021-06-02 15:53:59.833205640 +0200
294826
+++ b/converter/other/jpegtopnm.c	2021-06-02 16:16:28.520206914 +0200
294826
@@ -862,6 +862,8 @@ convertImage(FILE *
294826
     /* Calculate output image dimensions so we can allocate space */
294826
     jpeg_calc_output_dimensions(cinfoP);
294826
 
294826
+    overflow2(cinfoP->output_width, cinfoP->output_components);
294826
+
294826
     /* Start decompressor */
294826
     jpeg_start_decompress(cinfoP);
294826
 
294826
diff -ruNp a/converter/other/pbmtopgm.c b/converter/other/pbmtopgm.c
294826
--- a/converter/other/pbmtopgm.c	2021-06-02 15:53:59.831205621 +0200
294826
+++ b/converter/other/pbmtopgm.c	2021-06-02 16:17:14.248645808 +0200
294826
@@ -60,6 +60,7 @@ main(int argc, char *argv[]) {
294826
 
294826
 
294826
     outrow = pgm_allocrow(cols) ;
294826
+    overflow2(width, height);
294826
     maxval = MIN(PGM_OVERALLMAXVAL, width*height);
294826
     pgm_writepgminit(stdout, cols, rows, maxval, 0) ;
294826
 
294826
diff -ruNp a/converter/other/pnmtoddif.c b/converter/other/pnmtoddif.c
294826
--- a/converter/other/pnmtoddif.c	2021-06-02 15:53:59.832205630 +0200
294826
+++ b/converter/other/pnmtoddif.c	2021-06-02 16:18:17.064248709 +0200
294826
@@ -629,6 +629,7 @@ main(int argc, char *argv[]) {
294826
     switch (PNM_FORMAT_TYPE(format)) {
294826
     case PBM_TYPE:
294826
         ip.bits_per_pixel = 1;
294826
+        overflow_add(cols, 7);
294826
         ip.bytes_per_line = (cols + 7) / 8;
294826
         ip.spectral = 2;
294826
         ip.components = 1;
294826
@@ -644,6 +645,7 @@ main(int argc, char *argv[]) {
294826
         ip.polarity = 2;
294826
         break;
294826
     case PPM_TYPE:
294826
+        overflow2(cols, 3);
294826
         ip.bytes_per_line = 3 * cols;
294826
         ip.bits_per_pixel = 24;
294826
         ip.spectral = 5;
294826
diff -ruNp a/converter/other/pnmtojpeg.c b/converter/other/pnmtojpeg.c
294826
--- a/converter/other/pnmtojpeg.c	2021-06-02 15:53:59.835205659 +0200
294826
+++ b/converter/other/pnmtojpeg.c	2021-06-02 19:57:44.413225834 +0200
294826
@@ -606,7 +606,11 @@ read_scan_script(j_compress_ptr const ci
294826
            want JPOOL_PERMANENT.
294826
         */
294826
         const unsigned int scan_info_size = nscans * sizeof(jpeg_scan_info);
294826
-        jpeg_scan_info * const scan_info =
294826
+        const jpeg_scan_info * scan_info;
294826
+
294826
+        overflow2(nscans, sizeof(jpeg_scan_info));
294826
+
294826
+        scan_info =
294826
             (jpeg_scan_info *)
294826
             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
294826
                                         scan_info_size);
294826
@@ -938,6 +942,9 @@ compute_rescaling_array(JSAMPLE ** const
294826
   const long half_maxval = maxval / 2;
294826
   long val;
294826
 
294826
+  overflow_add(maxval, 1);
294826
+  overflow2(maxval+1, sizeof(JSAMPLE));
294826
+
294826
   *rescale_p = (JSAMPLE *)
294826
     (cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_IMAGE,
294826
                               (size_t) (((long) maxval + 1L) *
294826
@@ -1016,6 +1023,7 @@ convert_scanlines(struct jpeg_compress_s
294826
     */
294826
 
294826
   /* Allocate the libpnm output and compressor input buffers */
294826
+  overflow2(cinfo_p->image_width, cinfo_p->input_components);
294826
   buffer = (*cinfo_p->mem->alloc_sarray)
294826
     ((j_common_ptr) cinfo_p, JPOOL_IMAGE,
294826
      (unsigned int) cinfo_p->image_width * cinfo_p->input_components,
294826
diff -ruNp a/converter/other/pnmtops.c b/converter/other/pnmtops.c
294826
--- a/converter/other/pnmtops.c	2021-06-02 15:53:59.835205659 +0200
294826
+++ b/converter/other/pnmtops.c	2021-06-02 20:02:06.055502227 +0200
294826
@@ -294,17 +294,21 @@ parseCommandLine(int argc, const char **
294826
     validateCompDimension(width, 72, "-width value");
294826
     validateCompDimension(height, 72, "-height value");
294826
 
294826
+    overflow2(width, 72);
294826
     cmdlineP->width  = width * 72;
294826
+    overflow2(height, 72);
294826
     cmdlineP->height = height * 72;
294826
 
294826
     if (imagewidthSpec) {
294826
         validateCompDimension(imagewidth, 72, "-imagewidth value");
294826
+        overflow2(imagewidth, 72);
294826
         cmdlineP->imagewidth = imagewidth * 72;
294826
     }
294826
     else
294826
         cmdlineP->imagewidth = 0;
294826
     if (imageheightSpec) {
294826
-        validateCompDimension(imagewidth, 72, "-imageheight value");
294826
+        validateCompDimension(imageheight, 72, "-imageheight value");
294826
+        overflow2(imageheight, 72);
294826
         cmdlineP->imageheight = imageheight * 72;
294826
     }
294826
     else
294826
diff -ruNp a/converter/other/rletopnm.c b/converter/other/rletopnm.c
294826
--- a/converter/other/rletopnm.c	2021-06-02 15:53:59.834205650 +0200
294826
+++ b/converter/other/rletopnm.c	2021-06-02 20:03:24.864187893 +0200
294826
@@ -19,6 +19,8 @@
294826
  * If you modify this software, you should include a notice giving the
294826
  * name of the person performing the modification, the date of modification,
294826
  * and the reason for such modification.
294826
+ *
294826
+ * 2002-12-19: Fix maths wrapping bugs. Alan Cox <alan@redhat.com>
294826
  */
294826
 /*
294826
  * rletopnm - A conversion program to convert from Utah's "rle" image format
294826
diff -ruNp a/converter/other/sirtopnm.c b/converter/other/sirtopnm.c
294826
--- a/converter/other/sirtopnm.c	2021-06-02 15:53:59.833205640 +0200
294826
+++ b/converter/other/sirtopnm.c	2021-06-02 20:03:50.280409024 +0200
294826
@@ -69,6 +69,7 @@ char* argv[];
294826
 	    }
294826
 	    break;
294826
 	case PPM_TYPE:
294826
+        overflow3(cols, rows, 3);
294826
 	    picsize = cols * rows * 3;
294826
 	    planesize = cols * rows;
294826
             if ( !( sirarray = (unsigned char*) malloc( picsize ) ) ) 
294826
diff -ruNp a/converter/other/tifftopnm.c b/converter/other/tifftopnm.c
294826
--- a/converter/other/tifftopnm.c	2021-06-02 15:53:59.834205650 +0200
294826
+++ b/converter/other/tifftopnm.c	2021-06-02 20:04:47.721908789 +0200
294826
@@ -1372,7 +1372,9 @@ convertRasterByRows(pnmOut *       const
294826
     if (scanbuf == NULL)
294826
         pm_error("can't allocate memory for scanline buffer");
294826
 
294826
-    MALLOCARRAY(samplebuf, cols * spp);
294826
+    /* samplebuf is unsigned int * !!! */
294826
+    samplebuf = (unsigned int *) malloc3(cols , sizeof(unsigned int) , spp);
294826
+
294826
     if (samplebuf == NULL)
294826
         pm_error("can't allocate memory for row buffer");
294826
 
294826
diff -ruNp a/converter/other/xwdtopnm.c b/converter/other/xwdtopnm.c
294826
--- a/converter/other/xwdtopnm.c	2021-06-02 15:53:59.833205640 +0200
294826
+++ b/converter/other/xwdtopnm.c	2021-06-02 20:23:21.807634239 +0200
294826
@@ -210,6 +210,10 @@ processX10Header(X10WDFileHeader *  cons
294826
         *colorsP = pnm_allocrow(2);
294826
         PNM_ASSIGN1((*colorsP)[0], 0);
294826
         PNM_ASSIGN1((*colorsP)[1], *maxvalP);
294826
+        overflow_add(h10P->pixmap_width, 15);
294826
+        if(h10P->pixmap_width < 0)
294826
+            pm_error("assert: negative width");
294826
+        overflow2((((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width), 8);
294826
         *padrightP =
294826
             (((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width) * 8;
294826
         *bits_per_itemP = 16;
294826
@@ -635,6 +639,7 @@ processX11Header(X11WDFileHeader *  cons
294826
 
294826
     *colsP = h11FixedP->pixmap_width;
294826
     *rowsP = h11FixedP->pixmap_height;
294826
+    overflow2(h11FixedP->bytes_per_line, 8);
294826
     *padrightP =
294826
         h11FixedP->bytes_per_line * 8 -
294826
         h11FixedP->pixmap_width * h11FixedP->bits_per_pixel;
294826
diff -ruNp a/converter/pbm/mdatopbm.c b/converter/pbm/mdatopbm.c
294826
--- a/converter/pbm/mdatopbm.c	2021-06-02 15:53:59.826205573 +0200
294826
+++ b/converter/pbm/mdatopbm.c	2021-06-02 20:23:21.807634239 +0200
294826
@@ -245,10 +245,13 @@ main(int argc, char **argv) {
294826
         pm_readlittleshort(infile, &yy;; nInCols = yy;
294826
     }
294826
     
294826
+    overflow2(nOutCols, 8);
294826
     nOutCols = 8 * nInCols;
294826
     nOutRows = nInRows;
294826
-    if (bScale) 
294826
+    if (bScale) {
294826
+        overflow2(nOutRows, 2);
294826
         nOutRows *= 2;
294826
+    }
294826
 
294826
     data = pbm_allocarray(nOutCols, nOutRows);
294826
     
294826
diff -ruNp a/converter/pbm/mgrtopbm.c b/converter/pbm/mgrtopbm.c
294826
--- a/converter/pbm/mgrtopbm.c	2021-06-02 15:53:59.828205592 +0200
294826
+++ b/converter/pbm/mgrtopbm.c	2021-06-02 20:23:21.808634248 +0200
294826
@@ -65,6 +65,8 @@ readMgrHeader(FILE *          const ifP,
294826
     if (head.h_high < ' ' || head.l_high < ' ')
294826
         pm_error("Invalid width field in MGR header");
294826
     
294826
+    overflow_add(*colsP, pad);
294826
+
294826
     *colsP = (((int)head.h_wide - ' ') << 6) + ((int)head.l_wide - ' ');
294826
     *rowsP = (((int)head.h_high - ' ') << 6) + ((int) head.l_high - ' ');
294826
     *padrightP = ( ( *colsP + pad - 1 ) / pad ) * pad - *colsP;
294826
diff -ruNp a/converter/pbm/pbmtogem.c b/converter/pbm/pbmtogem.c
294826
--- a/converter/pbm/pbmtogem.c	2021-06-02 15:53:59.828205592 +0200
294826
+++ b/converter/pbm/pbmtogem.c	2021-06-02 20:23:21.809634256 +0200
294826
@@ -79,6 +79,7 @@ putinit (int const rows, int const cols)
294826
   bitsperitem = 0;
294826
   bitshift = 7;
294826
   outcol = 0;
294826
+  overflow_add(cols, 7);
294826
   outmax = (cols + 7) / 8;
294826
   outrow = (unsigned char *) pm_allocrow (outmax, sizeof (unsigned char));
294826
   lastrow = (unsigned char *) pm_allocrow (outmax, sizeof (unsigned char));
294826
diff -ruNp a/converter/pbm/pbmtogo.c b/converter/pbm/pbmtogo.c
294826
--- a/converter/pbm/pbmtogo.c	2021-06-02 15:53:59.826205573 +0200
294826
+++ b/converter/pbm/pbmtogo.c	2021-06-02 20:23:21.809634256 +0200
294826
@@ -158,6 +158,7 @@ main(int           argc,
294826
     bitrow = pbm_allocrow(cols);
294826
 
294826
     /* Round cols up to the nearest multiple of 8. */
294826
+    overflow_add(cols, 7);
294826
     rucols = ( cols + 7 ) / 8;
294826
     bytesperrow = rucols;       /* GraphOn uses bytes */
294826
     rucols = rucols * 8;
294826
diff -ruNp a/converter/pbm/pbmtolj.c b/converter/pbm/pbmtolj.c
294826
--- a/converter/pbm/pbmtolj.c	2021-06-02 15:53:59.828205592 +0200
294826
+++ b/converter/pbm/pbmtolj.c	2021-06-02 20:23:21.810634265 +0200
294826
@@ -120,7 +120,11 @@ parseCommandLine(int argc, char ** argv,
294826
 static void
294826
 allocateBuffers(unsigned int const cols) {
294826
 
294826
+    overflow_add(cols, 8);
294826
     rowBufferSize = (cols + 7) / 8;
294826
+    overflow_add(rowBufferSize, 128);
294826
+    overflow_add(rowBufferSize, rowBufferSize+128);
294826
+    overflow_add(rowBufferSize+10, rowBufferSize/8);
294826
     packBufferSize = rowBufferSize + (rowBufferSize + 127) / 128 + 1;
294826
     deltaBufferSize = rowBufferSize + rowBufferSize / 8 + 10;
294826
 
294826
diff -ruNp a/converter/pbm/pbmtomda.c b/converter/pbm/pbmtomda.c
294826
--- a/converter/pbm/pbmtomda.c	2021-06-02 15:53:59.827205582 +0200
294826
+++ b/converter/pbm/pbmtomda.c	2021-06-02 20:23:21.810634265 +0200
294826
@@ -179,6 +179,7 @@ int main(int argc, char **argv)
294826
     
294826
     nOutRowsUnrounded = bScale ? nInRows/2 : nInRows;
294826
 
294826
+    overflow_add(nOutRowsUnrounded, 3);
294826
     nOutRows = ((nOutRowsUnrounded + 3) / 4) * 4;
294826
         /* MDA wants rows a multiple of 4 */   
294826
     nOutCols = nInCols / 8;
294826
diff -ruNp a/converter/pbm/pbmtoppa/pbm.c b/converter/pbm/pbmtoppa/pbm.c
294826
--- a/converter/pbm/pbmtoppa/pbm.c	2021-06-02 15:53:59.830205611 +0200
294826
+++ b/converter/pbm/pbmtoppa/pbm.c	2021-06-02 20:23:21.811634274 +0200
294826
@@ -11,185 +11,128 @@
294826
 #include <stdio.h>
294826
 #include <stdlib.h>
294826
 #include <string.h>
294826
-#include <limits.h>
294826
 
294826
-#include "pm.h"
294826
-#include "nstring.h"
294826
 #include "ppapbm.h"
294826
 
294826
-int
294826
-make_pbm_stat(pbm_stat * const pbmStatP,
294826
-              FILE *     const ifP) {
294826
-
294826
-    char line[1024];
294826
-    char * rc;
294826
-    int retval;
294826
-
294826
-    pbmStatP->fptr         = ifP;
294826
-    pbmStatP->version      = none;
294826
-    pbmStatP->current_line = 0;
294826
-    pbmStatP->unread       = 0;
294826
-
294826
-    rc = fgets(line, 1024, ifP);
294826
-    if (rc == NULL)
294826
-        retval = 0;
294826
-    else {
294826
-        line[strlen(line)-1] = 0;
294826
-
294826
-        if (streq(line,"P1"))
294826
-            pbmStatP->version=P1;
294826
-        if (streq(line,"P4"))
294826
-            pbmStatP->version=P4;
294826
-
294826
-        if (pbmStatP->version == none) {
294826
-            pm_message("unknown PBM magic '%s'", line);
294826
-            retval = 0;
294826
-        } else {
294826
-            do {
294826
-                char * rc;
294826
-                rc = fgets(line, 1024, ifP);
294826
-                if (rc == NULL)
294826
-                    return 0;
294826
-            } while (line[0] == '#');
294826
-            {
294826
-                int rc;
294826
-                rc = sscanf(line, "%d %d",
294826
-                            &pbmStatP->width, &pbmStatP->height);
294826
-                if (rc != 2)
294826
-                    retval = 0;
294826
-                else {
294826
-                    if (pbmStatP->width < 0) {
294826
-                        pm_message("Image has negative width");
294826
-                        retval = 0;
294826
-                    } else if (pbmStatP->width > INT_MAX/2) {
294826
-                        pm_message("Uncomputeably large width: %d",
294826
-                                   pbmStatP->width);
294826
-                        retval = 0;
294826
-                    } else if (pbmStatP->height < 0) {
294826
-                        pm_message("Image has negative height");
294826
-                        retval = 0;
294826
-                    } else if (pbmStatP->height > INT_MAX/2) {
294826
-                        pm_message("Uncomputeably large height: %d",
294826
-                                   pbmStatP->height);
294826
-                        retval = 0;
294826
-                    } else
294826
-                        retval = 1;
294826
-                }
294826
-            }
294826
-        }
294826
-    }
294826
-    return retval;
294826
-}
294826
-
294826
+int make_pbm_stat(pbm_stat* pbm,FILE* fptr)
294826
+{
294826
+  char line[1024];
294826
+
294826
+  pbm->fptr=fptr;
294826
+  pbm->version=none;
294826
+  pbm->current_line=0;
294826
+  pbm->unread = 0;
294826
+
294826
+  if (fgets (line, 1024, fptr) == NULL)
294826
+    return 0;
294826
+  line[strlen(line)-1] = 0;
294826
+
294826
+  if(!strcmp(line,"P1")) pbm->version=P1;
294826
+  if(!strcmp(line,"P4")) pbm->version=P4;
294826
+  if(pbm->version == none)
294826
+  {
294826
+    fprintf(stderr,"pbm_readheader(): unknown PBM magic '%s'\n",line);
294826
+    return 0;
294826
+  }
294826
+
294826
+  do
294826
+    if (fgets (line, 1024, fptr) == NULL)
294826
+      return 0;
294826
+  while (line[0] == '#');
294826
 
294826
+  if (2 != sscanf (line, "%d %d", &pbm->width, &pbm->height))
294826
+    return 0;
294826
 
294826
-static int
294826
-getbytes(FILE *          const ifP,
294826
-         unsigned int    const width,
294826
-         unsigned char * const data) {
294826
-
294826
-    unsigned char mask;
294826
-    unsigned char acc;
294826
-    unsigned char * place;
294826
-    unsigned int num;
294826
-    int retval;
294826
-
294826
-    if (width == 0)
294826
-        retval = 0;
294826
-    else {
294826
-        for (mask = 0x80, acc = 0, num = 0, place = data; num < width; ) {
294826
-            switch (getc(ifP)) {
294826
-            case EOF:
294826
-                return 0;
294826
-            case '1':
294826
-                acc |= mask;
294826
-                /* fall through */
294826
-            case '0':
294826
-                mask >>= 1;
294826
-                ++num;
294826
-                if (mask == 0x00) { /* if (num % 8 == 0) */
294826
-                    *place++ = acc;
294826
-                    acc = 0;
294826
-                    mask = 0x80;
294826
-                }
294826
-            }
294826
-        }
294826
-        if (width % 8 != 0)
294826
-            *place = acc;
294826
-
294826
-        retval = 1;
294826
-    }
294826
-    return retval;
294826
+  return 1;
294826
 }
294826
 
294826
-
294826
-
294826
-int
294826
-pbm_readline(pbm_stat *      const pbmStatP,
294826
-             unsigned char * const data) {
294826
-/*----------------------------------------------------------------------------
294826
-  Read a single line into data which must be at least (pbmStatP->width+7)/8
294826
-  bytes of storage.
294826
------------------------------------------------------------------------------*/
294826
-    int retval;
294826
-
294826
-    if (pbmStatP->current_line >= pbmStatP->height)
294826
-        retval = 0;
294826
-    else {
294826
-        if (pbmStatP->unread) {
294826
-            memcpy(data, pbmStatP->revdata, (pbmStatP->width+7)/8);
294826
-            ++pbmStatP->current_line;
294826
-            pbmStatP->unread = 0;
294826
-            free(pbmStatP->revdata);
294826
-            pbmStatP->revdata = NULL;
294826
-            retval = 1;
294826
-        } else {
294826
-            switch (pbmStatP->version) {
294826
-            case P1:
294826
-                if (getbytes(pbmStatP->fptr, pbmStatP->width, data)) {
294826
-                    pbmStatP->current_line++;
294826
-                    retval = 1;
294826
-                } else
294826
-                    retval = 0;
294826
-                break;
294826
-            case P4: {
294826
-                int tmp, tmp2;
294826
-                tmp = (pbmStatP->width+7)/8;
294826
-                tmp2 = fread(data,1,tmp,pbmStatP->fptr);
294826
-                if (tmp2 == tmp) {
294826
-                    ++pbmStatP->current_line;
294826
-                    retval = 1;
294826
-                } else {
294826
-                    pm_message("error reading line data (%d)", tmp2);
294826
-                    retval = 0;
294826
-                }
294826
-            } break;
294826
-
294826
-            default:
294826
-                pm_message("unknown PBM version");
294826
-                retval = 0;
294826
-            }
294826
-        }
294826
+static int getbytes(FILE *fptr,int width,unsigned char* data)
294826
+{
294826
+  unsigned char mask,acc,*place;
294826
+  int num;
294826
+
294826
+  if(!width) return 0;
294826
+  for(mask=0x80, acc=0, num=0, place=data; num
294826
+  {
294826
+    switch(getc(fptr))
294826
+    {
294826
+    case EOF:      
294826
+      return 0;
294826
+    case '1':
294826
+      acc|=mask;
294826
+      /* fall through */
294826
+    case '0':
294826
+      mask>>=1;
294826
+      num++;
294826
+      if(!mask) /* if(num%8 == 0) */
294826
+      {
294826
+	*place++ = acc;
294826
+	acc=0;
294826
+	mask=0x80;
294826
+      }
294826
     }
294826
-    return retval;
294826
+  }
294826
+  if(width%8)
294826
+    *place=acc;
294826
+  return 1;
294826
 }
294826
 
294826
+/* Reads a single line into data which must be at least (pbm->width+7)/8
294826
+   bytes of storage */
294826
+int pbm_readline(pbm_stat* pbm,unsigned char* data)
294826
+{
294826
+  int tmp,tmp2;
294826
+
294826
+  if(pbm->current_line >= pbm->height) return 0;
294826
+
294826
+  if (pbm->unread)
294826
+    {
294826
+      memcpy (data, pbm->revdata, (pbm->width+7)/8);
294826
+      pbm->current_line++;
294826
+      pbm->unread = 0;
294826
+      free (pbm->revdata);
294826
+      pbm->revdata = NULL;
294826
+      return 1;
294826
+    }
294826
 
294826
+  switch(pbm->version)
294826
+  {
294826
+  case P1:
294826
+    if(getbytes(pbm->fptr,pbm->width,data))
294826
+    {
294826
+      pbm->current_line++;
294826
+      return 1;
294826
+    }
294826
+    return 0;
294826
 
294826
-void
294826
-pbm_unreadline(pbm_stat * const pbmStatP,
294826
-               void *     const data) {
294826
-/*----------------------------------------------------------------------------
294826
-  Push a line back into the buffer; we read too much!
294826
------------------------------------------------------------------------------*/
294826
-    /* can store only one line in the unread buffer */
294826
-
294826
-    if (!pbmStatP->unread) {
294826
-        pbmStatP->unread = 1;
294826
-        pbmStatP->revdata = malloc ((pbmStatP->width+7)/8);
294826
-        memcpy(pbmStatP->revdata, data, (pbmStatP->width+7)/8);
294826
-        --pbmStatP->current_line;
294826
+  case P4:
294826
+    overflow_add(pbm->width, 7);
294826
+    tmp=(pbm->width+7)/8;
294826
+    tmp2=fread(data,1,tmp,pbm->fptr);
294826
+    if(tmp2 == tmp)
294826
+    {
294826
+      pbm->current_line++;
294826
+      return 1;
294826
     }
294826
-}
294826
+    fprintf(stderr,"pbm_readline(): error reading line data (%d)\n",tmp2);
294826
+    return 0;
294826
 
294826
+  default:
294826
+    fprintf(stderr,"pbm_readline(): unknown PBM version\n");
294826
+    return 0;
294826
+  }
294826
+}
294826
 
294826
+/* push a line back into the buffer; we read too much! */
294826
+void pbm_unreadline (pbm_stat *pbm, void *data)
294826
+{
294826
+  /* can only store one line in the unread buffer */
294826
+  if (pbm->unread)
294826
+    return;
294826
+
294826
+  pbm->unread = 1;
294826
+  overflow_add(pbm->width, 7);
294826
+  pbm->revdata = malloc ((pbm->width+7)/8);
294826
+  memcpy (pbm->revdata, data, (pbm->width+7)/8);
294826
+  pbm->current_line--;
294826
+}
294826
diff -ruNp a/converter/pbm/pbmtoppa/pbmtoppa.c b/converter/pbm/pbmtoppa/pbmtoppa.c
294826
--- a/converter/pbm/pbmtoppa/pbmtoppa.c	2021-06-02 15:53:59.829205601 +0200
294826
+++ b/converter/pbm/pbmtoppa/pbmtoppa.c	2021-06-02 20:23:21.811634274 +0200
294826
@@ -453,6 +453,7 @@ main(int argc, char *argv[]) {
294826
             pm_error("main(): unrecognized parameter '%s'", argv[argn]);
294826
     }
294826
 
294826
+    overflow_add(Width, 7);
294826
     Pwidth=(Width+7)/8;
294826
     printer.fptr=out;
294826
 
294826
diff -ruNp a/converter/pbm/pbmtoxbm.c b/converter/pbm/pbmtoxbm.c
294826
--- a/converter/pbm/pbmtoxbm.c	2021-06-02 15:53:59.829205601 +0200
294826
+++ b/converter/pbm/pbmtoxbm.c	2021-06-02 20:23:21.812634282 +0200
294826
@@ -352,6 +352,8 @@ convertRaster(FILE *          const ifP,
294826
 
294826
     unsigned char * bitrow;
294826
     unsigned int row;
294826
+    
294826
+    overflow_add(cols, padright);
294826
 
294826
     putinit(xbmVersion);
294826
 
294826
diff -ruNp a/converter/pbm/pbmto4425.c b/converter/pbm/pbmto4425.c
294826
--- a/converter/pbm/pbmto4425.c	2021-06-02 15:53:59.827205582 +0200
294826
+++ b/converter/pbm/pbmto4425.c	2021-06-02 20:23:21.808634248 +0200
294826
@@ -2,6 +2,7 @@
294826
 
294826
 #include "nstring.h"
294826
 #include "pbm.h"
294826
+#include <string.h>
294826
 
294826
 static char bit_table[2][3] = {
294826
 {1, 4, 0x10},
294826
@@ -160,7 +161,7 @@ main(int argc, char * argv[]) {
294826
     xres = vmap_width * 2;
294826
     yres = vmap_height * 3;
294826
 
294826
-    vmap = malloc(vmap_width * vmap_height * sizeof(char));
294826
+    vmap = malloc3(vmap_width, vmap_height, sizeof(char));
294826
     if(vmap == NULL)
294826
 	{
294826
         pm_error( "Cannot allocate memory" );
294826
diff -ruNp a/converter/pbm/pktopbm.c b/converter/pbm/pktopbm.c
294826
--- a/converter/pbm/pktopbm.c	2021-06-02 15:53:59.828205592 +0200
294826
+++ b/converter/pbm/pktopbm.c	2021-06-02 20:23:21.813634291 +0200
294826
@@ -280,6 +280,7 @@ main(int argc, char *argv[]) {
294826
         if (flagbyte == 7) {            /* long form preamble */
294826
             integer packetlength = get32() ;    /* character packet length */
294826
             car = get32() ;         /* character number */
294826
+            overflow_add(packetlength, pktopbm_pkloc);
294826
             endofpacket = packetlength + pktopbm_pkloc;
294826
                 /* calculate end of packet */
294826
             if ((car >= MAXPKCHAR) || !filename[car]) {
294826
diff -ruNp a/converter/pbm/thinkjettopbm.l b/converter/pbm/thinkjettopbm.l
294826
--- a/converter/pbm/thinkjettopbm.l	2021-06-02 15:53:59.828205592 +0200
294826
+++ b/converter/pbm/thinkjettopbm.l	2021-06-02 20:23:21.813634291 +0200
294826
@@ -114,7 +114,9 @@ DIG             [0-9]
294826
 <RASTERMODE>\033\*b{DIG}+W  {
294826
                             int l;
294826
                             if (rowCount >= rowCapacity) {
294826
-                                rowCapacity += 100;
294826
+				overflow_add(rowCapacity, 100);
294826
+                                 rowCapacity += 100;
294826
+				overflow2(rowCapacity, sizeof *rows);
294826
                                 rows = realloc (rows, rowCapacity * sizeof *rows);
294826
                                 if (rows == NULL)
294826
                                     pm_error ("Out of memory.");
294826
@@ -226,6 +228,8 @@ yywrap (void)
294826
     /*
294826
      * Quite simple since ThinkJet bit arrangement matches PBM
294826
      */
294826
+
294826
+    overflow2(maxRowLength, 8);
294826
     pbm_writepbminit(stdout, maxRowLength*8, rowCount, 0);
294826
 
294826
     packed_bitrow = malloc(maxRowLength);
294826
diff -ruNp a/converter/pbm/ybmtopbm.c b/converter/pbm/ybmtopbm.c
294826
--- a/converter/pbm/ybmtopbm.c	2021-06-02 15:53:59.827205582 +0200
294826
+++ b/converter/pbm/ybmtopbm.c	2021-06-02 20:23:21.814634300 +0200
294826
@@ -43,6 +43,7 @@ getinit(FILE *  const ifP,
294826
         pm_error("EOF / read error");
294826
 
294826
     *depthP = 1;
294826
+    overflow_add(*colsP, 15);
294826
 }
294826
 
294826
 
294826
diff -ruNp a/converter/pgm/lispmtopgm.c b/converter/pgm/lispmtopgm.c
294826
--- a/converter/pgm/lispmtopgm.c	2021-06-02 15:53:59.831205621 +0200
294826
+++ b/converter/pgm/lispmtopgm.c	2021-06-02 20:23:21.814634300 +0200
294826
@@ -58,6 +58,7 @@ main( argc, argv )
294826
         pm_error( "depth (%d bits) is too large", depth);
294826
 
294826
     pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 );
294826
+    overflow_add(cols, 7);
294826
     grayrow = pgm_allocrow( ( cols + 7 ) / 8 * 8 );
294826
 
294826
     for ( row = 0; row < rows; ++row )
294826
@@ -102,6 +103,8 @@ getinit( file, colsP, rowsP, depthP, pad
294826
     
294826
     if ( *depthP == 0 )
294826
 	*depthP = 1;	/* very old file */
294826
+
294826
+    overflow_add((int)colsP, 31);
294826
     
294826
     *padrightP = ( ( *colsP + 31 ) / 32 ) * 32 - *colsP;
294826
     
294826
diff -ruNp a/converter/pgm/psidtopgm.c b/converter/pgm/psidtopgm.c
294826
--- a/converter/pgm/psidtopgm.c	2021-06-02 15:53:59.830205611 +0200
294826
+++ b/converter/pgm/psidtopgm.c	2021-06-02 20:23:21.815634309 +0200
294826
@@ -78,6 +78,7 @@ main(int     argc,
294826
         pm_error("bits/sample (%d) is too large.", bitspersample);
294826
 
294826
     pgm_writepgminit(stdout, cols, rows, maxval, 0);
294826
+    overflow_add(cols, 7);
294826
     grayrow = pgm_allocrow((cols + 7) / 8 * 8);
294826
     for (row = 0; row < rows; ++row) {
294826
         unsigned int col;
294826
diff -ruNp a/converter/ppm/ilbmtoppm.c b/converter/ppm/ilbmtoppm.c
294826
--- a/converter/ppm/ilbmtoppm.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/ilbmtoppm.c	2021-06-02 20:23:21.817634326 +0200
294826
@@ -608,6 +608,7 @@ decode_row(FILE *          const ifP,
294826
     rawtype *chp;
294826
 
294826
     cols = bmhdP->w;
294826
+    overflow_add(cols, 15);
294826
     bytes = RowBytes(cols);
294826
     for( plane = 0; plane < nPlanes; plane++ ) {
294826
         int mask;
294826
@@ -695,6 +696,23 @@ decode_mask(FILE *          const ifP,
294826
  Multipalette handling
294826
  ****************************************************************************/
294826
 
294826
+static void *
294826
+xmalloc2(x, y)
294826
+    int x;
294826
+    int y;
294826
+{
294826
+    void *mem;
294826
+
294826
+    overflow2(x,y);
294826
+    if( x * y == 0 )
294826
+        return NULL;
294826
+
294826
+    mem = malloc2(x,y);
294826
+    if( mem == NULL )
294826
+        pm_error("out of memory allocating %d bytes", x * y);
294826
+    return mem;
294826
+}
294826
+
294826
 
294826
 static void
294826
 multi_adjust(ColorMap *            const cmapP,
294826
@@ -1363,6 +1381,9 @@ dcol_to_ppm(FILE *         const ifP,
294826
     if( redmaxval != maxval || greenmaxval != maxval || bluemaxval != maxval )
294826
         pm_message("scaling colors to %d bits", pm_maxvaltobits(maxval));
294826
 
294826
+    overflow_add(redmaxval, 1);
294826
+    overflow_add(greenmaxval, 1);
294826
+    overflow_add(bluemaxval, 1);
294826
     MALLOCARRAY_NOFAIL(redtable,   redmaxval   +1);
294826
     MALLOCARRAY_NOFAIL(greentable, greenmaxval +1);
294826
     MALLOCARRAY_NOFAIL(bluetable,  bluemaxval  +1);
294826
@@ -1802,7 +1823,9 @@ PCHG_ConvertSmall(PCHGHeader *    const
294826
             ChangeCount32 = *data++;
294826
             remDataSize -= 2;
294826
 
294826
+            overflow_add(ChangeCount16, ChangeCount32);
294826
             changes = ChangeCount16 + ChangeCount32;
294826
+            overflow_add(changes, 1);
294826
             for (i = 0; i < changes; ++i) {
294826
                 if (totalchanges >= pchgP->TotalChanges) goto fail;
294826
                 if (remDataSize < 2) goto fail;
294826
@@ -2067,6 +2090,9 @@ read_pchg(FILE *     const ifP,
294826
             cmap->mp_change[i] = NULL;
294826
         if( PCHG.StartLine < 0 ) {
294826
             int nch;
294826
+            if(PCHG.MaxReg < PCHG.MinReg)
294826
+                pm_error("assert: MinReg > MaxReg");
294826
+            overflow_add(PCHG.MaxReg-PCHG.MinReg, 2);
294826
             nch = PCHG.MaxReg - PCHG.MinReg +1;
294826
             MALLOCARRAY_NOFAIL(cmap->mp_init, nch + 1);
294826
             for( i = 0; i < nch; i++ )
294826
@@ -2143,6 +2169,7 @@ process_body( FILE *          const ifP,
294826
     if (typeid == ID_ILBM) {
294826
         int isdeep;
294826
 
294826
+        overflow_add(bmhdP->w, 15);
294826
         MALLOCARRAY_NOFAIL(ilbmrow, RowBytes(bmhdP->w));
294826
         *viewportmodesP |= fakeviewport;      /* -isham/-isehb */
294826
 
294826
diff -ruNp a/converter/ppm/imgtoppm.c b/converter/ppm/imgtoppm.c
294826
--- a/converter/ppm/imgtoppm.c	2021-06-02 15:53:59.845205755 +0200
294826
+++ b/converter/ppm/imgtoppm.c	2021-06-02 20:23:21.818634335 +0200
294826
@@ -84,6 +84,7 @@ main(int argc, char ** argv) {
294826
             len = atoi((char*) buf );
294826
             if ( fread( buf, len, 1, ifp ) != 1 )
294826
                 pm_error( "bad colormap buf" );
294826
+            overflow2(cmaplen, 3);
294826
             if ( cmaplen * 3 != len )
294826
             {
294826
                 pm_message(
294826
@@ -105,6 +106,7 @@ main(int argc, char ** argv) {
294826
                 pm_error( "bad pixel data header" );
294826
             buf[8] = '\0';
294826
             len = atoi((char*) buf );
294826
+            overflow2(cols, rows);
294826
             if ( len != cols * rows )
294826
                 pm_message(
294826
                     "pixel data length (%d) does not match image size (%d)",
294826
diff -ruNp a/converter/ppm/Makefile b/converter/ppm/Makefile
294826
--- a/converter/ppm/Makefile	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/Makefile	2021-06-02 20:23:21.815634309 +0200
294826
@@ -11,7 +11,7 @@ SUBDIRS = hpcdtoppm ppmtompeg
294826
 
294826
 PORTBINARIES =	411toppm eyuvtoppm gouldtoppm ilbmtoppm imgtoppm \
294826
 		leaftoppm mtvtoppm neotoppm \
294826
-		pcxtoppm pc1toppm pi1toppm picttoppm pjtoppm \
294826
+		pcxtoppm pc1toppm pi1toppm pjtoppm \
294826
 		ppmtoacad ppmtoapplevol ppmtoarbtxt ppmtoascii \
294826
 		ppmtobmp ppmtoeyuv ppmtogif ppmtoicr ppmtoilbm \
294826
 		ppmtoleaf ppmtolj ppmtomitsu ppmtoneo \
294826
diff -ruNp a/converter/ppm/pcxtoppm.c b/converter/ppm/pcxtoppm.c
294826
--- a/converter/ppm/pcxtoppm.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/pcxtoppm.c	2021-06-02 20:23:21.818634335 +0200
294826
@@ -409,6 +409,7 @@ pcx_planes_to_pixels(pixels, bitplanes,
294826
     /*
294826
      * clear the pixel buffer
294826
      */
294826
+    overflow2(bytesperline, 8);
294826
     npixels = (bytesperline * 8) / bitsperpixel;
294826
     p    = pixels;
294826
     while (--npixels >= 0)
294826
@@ -470,6 +471,7 @@ pcx_16col_to_ppm(FILE *       const ifP,
294826
     }
294826
 
294826
     /*  BytesPerLine should be >= BitsPerPixel * cols / 8  */
294826
+    overflow2(BytesPerLine, 8);
294826
     rawcols = BytesPerLine * 8 / BitsPerPixel;
294826
     if (headerCols > rawcols) {
294826
         pm_message("warning - BytesPerLine = %d, "
294826
diff -ruNp a/converter/ppm/picttoppm.c b/converter/ppm/picttoppm.c
294826
--- a/converter/ppm/picttoppm.c	2021-06-02 15:53:59.845205755 +0200
294826
+++ b/converter/ppm/picttoppm.c	2021-06-02 20:23:21.820634352 +0200
294826
@@ -1,3 +1,4 @@
294826
+#error "Unfixable. Don't ship me"
294826
 /*
294826
  * picttoppm.c -- convert a MacIntosh PICT file to PPM format.
294826
  *
294826
diff -ruNp a/converter/ppm/pjtoppm.c b/converter/ppm/pjtoppm.c
294826
--- a/converter/ppm/pjtoppm.c	2021-06-02 15:53:59.845205755 +0200
294826
+++ b/converter/ppm/pjtoppm.c	2021-06-02 20:23:21.821634361 +0200
294826
@@ -10,98 +10,66 @@
294826
 ** implied warranty.
294826
 */
294826
 
294826
-#include <stdbool.h>
294826
-
294826
 #include "ppm.h"
294826
-#include "pm_c_util.h"
294826
 #include "mallocvar.h"
294826
 
294826
 static char usage[] =  "[paintjetfile]";
294826
 
294826
-
294826
-
294826
-static unsigned int
294826
-uintProduct(unsigned int const multiplicand,
294826
-            unsigned int const multiplier) {
294826
-
294826
-    if (UINT_MAX / multiplier < multiplicand)
294826
-        pm_error("Airthmetic overflow");
294826
-
294826
-    return multiplicand * multiplier;
294826
-}
294826
-
294826
-
294826
-
294826
+static int egetc ARGS((FILE *fp));
294826
 static int
294826
-egetc(FILE * const ifP) {
294826
+egetc(fp)
294826
+    FILE *fp;
294826
+{
294826
     int c;
294826
-
294826
-    c = fgetc(ifP);
294826
-
294826
-    if (c == -1)
294826
+    if ((c = fgetc(fp)) == -1)
294826
         pm_error("unexpected end of file");
294826
-
294826
-    return c;
294826
+    return(c);
294826
 }
294826
 
294826
-
294826
-
294826
 int
294826
-main(int argc, const char ** argv) {
294826
-
294826
+main(argc, argv)
294826
+    int argc;
294826
+    char *argv[];
294826
+{
294826
     int cmd, val;
294826
     char buffer[BUFSIZ];
294826
-    int planes = 3;
294826
-    unsigned int rows;
294826
-    unsigned int rowsX;
294826
-    unsigned int cols;
294826
-    bool colsIsSet;
294826
+    int planes = 3, rows = -1, cols = -1;
294826
+    int r = 0, c = 0, p = 0, i;
294826
     unsigned char **image = NULL;
294826
     int *imlen;
294826
-    FILE * ifP;
294826
+    FILE *fp = stdin;
294826
     int mode;
294826
-    bool modeIsSet;
294826
     int argn;
294826
     unsigned char bf[3];
294826
-    pixel * pixrow;
294826
-    int c;
294826
-    int row;
294826
-    int plane;
294826
+    pixel *pixrow;
294826
 
294826
-    pm_proginit(&argc, argv);
294826
 
294826
+    ppm_init(&argc, argv);
294826
     argn = 1;
294826
     if (argn != argc)
294826
-        ifP = pm_openr(argv[argn++]);
294826
+        fp = pm_openr(argv[argn++]);
294826
     else
294826
-        ifP = stdin;
294826
+        fp = stdin;
294826
 
294826
     if (argn != argc)
294826
         pm_usage(usage);
294826
 
294826
-    row = 0;  /* initial value */
294826
-    plane = 0;  /* initial value */
294826
-    modeIsSet = false;  /* initial value */
294826
-    colsIsSet = false;  /* initial value */
294826
-    rowsX = 0;  /* initial value */
294826
-
294826
-    while ((c = fgetc(ifP)) != -1) {
294826
+    while ((c = fgetc(fp)) != -1) {
294826
         if (c != '\033')
294826
             continue;
294826
-        switch (c = egetc(ifP)) {
294826
+        switch (c = egetc(fp)) {
294826
         case 'E':   /* reset */
294826
             break;
294826
-        case '*': {
294826
-            unsigned int i;
294826
-            cmd = egetc(ifP);
294826
+        case '*':
294826
+            cmd = egetc(fp);
294826
             for (i = 0; i < BUFSIZ; i++) {
294826
-                if (!isdigit(c = egetc(ifP)) && c != '+' && c != '-')
294826
+                if (!isdigit(c = egetc(fp)) && c != '+' && c != '-')
294826
                     break;
294826
                 buffer[i] = c;
294826
             }
294826
             if (i != 0) {
294826
                 buffer[i] = '\0';
294826
-                if (sscanf(buffer, "%d", &val) != 1)
294826
+                if (sscanf(buffer, "%d", &val) != 1) 
294826
                     pm_error("bad value `%s' at <ESC>*%c%c", buffer, cmd, c);
294826
             }
294826
             else
294826
@@ -125,18 +93,18 @@ main(int argc, const char ** argv) {
294826
             case 'r':
294826
                 switch (c) {
294826
                 case 'S':   /* width */
294826
-                    if (val < 0)
294826
+                    if (val < 0) {
294826
                         pm_error("invalid width value");
294826
-                    else {
294826
+                    } else {
294826
                         cols = val;
294826
-                        colsIsSet = true;
294826
                     }
294826
                     break;
294826
                 case 'T':   /* height */
294826
-                    if (val < 0)
294826
+                    if (val < 0) {
294826
                         pm_error ("invalid height value");
294826
-                    else
294826
-                        rowsX = val;
294826
+                    } else {
294826
+                        rows = val;
294826
+                    }
294826
                     break;
294826
                 case 'U':   /* planes */
294826
                     planes = val;
294826
@@ -163,40 +131,43 @@ main(int argc, const char ** argv) {
294826
                     if (val != 0 && val != 1)
294826
                         pm_error("unimplemented transmission mode %d", val);
294826
                     mode = val;
294826
-                    modeIsSet = true;
294826
                     break;
294826
                 case 'V':   /* send plane */
294826
                 case 'W':   /* send last plane */
294826
-                    if (row >= rowsX || image == NULL) {
294826
-                        if (row >= rowsX)
294826
-                            rowsX += 100;
294826
+                    if (rows == -1 || r >= rows || image == NULL) {
294826
+                        if (rows == -1 || r >= rows) {
294826
+                            overflow_add(rows, 100);
294826
+                            rows += 100;
294826
+                        }
294826
+
294826
                         if (image == NULL) {
294826
-                            MALLOCARRAY(image, uintProduct(rowsX, planes));
294826
-                            MALLOCARRAY(imlen, uintProduct(rowsX, planes));
294826
-                        } else {
294826
-                            REALLOCARRAY(image, uintProduct(rowsX, planes));
294826
-                            REALLOCARRAY(imlen, uintProduct(rowsX, planes));
294826
+                            image = (unsigned char **)
294826
+                                malloc3(rows , planes , sizeof(unsigned char *));
294826
+                            imlen = (int *) malloc3(rows , planes,  sizeof(int));
294826
                         }
294826
+                        else {
294826
+                            overflow2(rows,planes);
294826
+                            image = (unsigned char **)
294826
+                                realloc2(image, rows * planes,
294826
+                                    sizeof(unsigned char *));
294826
+                            imlen = (int *) realloc2(imlen, rows * planes, sizeof(int));                        }
294826
                     }
294826
                     if (image == NULL || imlen == NULL)
294826
                         pm_error("out of memory");
294826
-                    if (plane >= planes)
294826
+                    if (p == planes) 
294826
                         pm_error("too many planes");
294826
-                    if (!colsIsSet)
294826
-                        pm_error("missing width value");
294826
-
294826
-                    cols = MAX(cols, val);
294826
-                    imlen[row * planes + plane] = val;
294826
-                    MALLOCARRAY(image[row * planes + plane], val);
294826
-                    if (image[row * planes + plane] == NULL)
294826
+                    cols = cols > val ? cols : val;
294826
+                    imlen[r * planes + p] = val;
294826
+                    MALLOCARRAY(image[r * planes + p], val);
294826
+                    if (image[r * planes + p] == NULL) 
294826
                         pm_error("out of memory");
294826
-                    if (fread(image[row * planes + plane], 1, val, ifP) != val)
294826
+                    if (fread(image[r * planes + p], 1, val, fp) != val) 
294826
                         pm_error("short data");
294826
                     if (c == 'V')
294826
-                        ++plane;
294826
+                        p++;
294826
                     else {
294826
-                        plane = 0;
294826
-                        ++row;
294826
+                        p = 0;
294826
+                        r++;
294826
                     }
294826
                     break;
294826
                 default:
294826
@@ -205,7 +176,7 @@ main(int argc, const char ** argv) {
294826
                 }
294826
                 break;
294826
             case 'p': /* Position */
294826
-                if (plane != 0)
294826
+                if (p != 0) 
294826
                     pm_error("changed position in the middle of "
294826
                              "transferring planes");
294826
                 switch (c) {
294826
@@ -214,15 +185,15 @@ main(int argc, const char ** argv) {
294826
                     break;
294826
                 case 'Y':
294826
                     if (buffer[0] == '+')
294826
-                        val = row + val;
294826
+                        val = r + val;
294826
                     if (buffer[0] == '-')
294826
-                        val = row - val;
294826
-                    for (; val > row; ++row)
294826
-                        for (plane = 0; plane < 3; ++plane) {
294826
-                            imlen[row * planes + plane] = 0;
294826
-                            image[row * planes + plane] = NULL;
294826
+                        val = r - val;
294826
+                    for (; val > r; r++) 
294826
+                        for (p = 0; p < 3; p++) {
294826
+                            imlen[r * planes + p] = 0;
294826
+                            image[r * planes + p] = NULL;
294826
                         }
294826
-                    row = val;
294826
+                    r = val;
294826
                     break;
294826
                 default:
294826
                     pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
294826
@@ -231,86 +202,65 @@ main(int argc, const char ** argv) {
294826
             default:
294826
                 pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
294826
                 break;
294826
-             }
294826
-        } /* case */
294826
-        } /* switch */
294826
+            }
294826
+        }
294826
     }
294826
-    pm_close(ifP);
294826
-
294826
-    if (!modeIsSet)
294826
-        pm_error("Input does not contain a 'bM' transmission mode order");
294826
-
294826
-    rows = row;
294826
+    pm_close(fp);
294826
+    rows = r;
294826
     if (mode == 1) {
294826
-        unsigned int const newcols = 10240;
294826
-            /* It could not be larger than that! */
294826
-
294826
-        unsigned char * buf;
294826
-        unsigned int row;
294826
-
294826
-        for (row = 0, cols = 0; row < rows; ++row) {
294826
-            unsigned int plane;
294826
-            if (image[row * planes] == NULL)
294826
+        unsigned char *buf;
294826
+        int newcols = 0;
294826
+        newcols = 10240; /* It could not be larger that that! */
294826
+        cols = 0;
294826
+        for (r = 0; r < rows; r++) {
294826
+            if (image[r * planes] == NULL)
294826
                 continue;
294826
-            for (plane = 0; plane < planes; ++plane) {
294826
-                unsigned int i;
294826
-                unsigned int col;
294826
+            for (p = 0; p < planes; p++) {
294826
                 MALLOCARRAY(buf, newcols);
294826
-                if (buf == NULL)
294826
+                if (buf == NULL) 
294826
                     pm_error("out of memory");
294826
-                for (i = 0, col = 0;
294826
-                     col < imlen[plane + row * planes];
294826
-                     col += 2)
294826
-                    for (cmd = image[plane + row * planes][col],
294826
-                             val = image[plane + row * planes][col+1];
294826
-                         cmd >= 0 && i < newcols; cmd--, i++)
294826
+                for (i = 0, c = 0; c < imlen[p + r * planes]; c += 2)
294826
+                    for (cmd = image[p + r * planes][c],
294826
+                             val = image[p + r * planes][c+1]; 
294826
+                         cmd >= 0 && i < newcols; cmd--, i++) {
294826
                         buf[i] = val;
294826
-                cols = MAX(cols, i);
294826
-                free(image[plane + row * planes]);
294826
-                /*
294826
-                 * This is less than what we have so it realloc should
294826
+                        overflow_add(i, 1);
294826
+                    }
294826
+                cols = cols > i ? cols : i;
294826
+                free(image[p + r * planes]);
294826
+                /* 
294826
+                 * This is less than what we have so it realloc should 
294826
                  * not return null. Even if it does, tough! We will
294826
                  * lose a line, and probably die on the next line anyway
294826
                  */
294826
-                image[plane + row * planes] = realloc(buf, i);
294826
+                image[p + r * planes] = (unsigned char *) realloc(buf, i);
294826
             }
294826
         }
294826
+        overflow2(cols, 8);
294826
         cols *= 8;
294826
     }
294826
-
294826
+            
294826
+       
294826
     ppm_writeppminit(stdout, cols, rows, (pixval) 255, 0);
294826
     pixrow = ppm_allocrow(cols);
294826
-
294826
-    for (row = 0; row < rows; ++row) {
294826
-        if (image[row * planes] == NULL) {
294826
-            unsigned int col;
294826
-            for (col = 0; col < cols; ++col)
294826
-                PPM_ASSIGN(pixrow[col], 0, 0, 0);
294826
+    for (r = 0; r < rows; r++) {
294826
+        if (image[r * planes] == NULL) {
294826
+            for (c = 0; c < cols; c++)
294826
+                PPM_ASSIGN(pixrow[c], 0, 0, 0);
294826
             continue;
294826
         }
294826
-        {
294826
-            unsigned int col;
294826
-            unsigned int cmd;
294826
-            for (cmd = 0, col = 0; col < cols; col += 8, ++cmd) {
294826
-                unsigned int i;
294826
-                for (i = 0; i < 8 && col + i < cols; ++i) {
294826
-                    unsigned int plane;
294826
-                    for (plane = 0; plane < planes; ++plane)
294826
-                        if (mode == 0 && cmd >= imlen[row * planes + plane])
294826
-                            bf[plane] = 0;
294826
-                        else
294826
-                            bf[plane] = (image[row * planes + plane][cmd] &
294826
-                                     (1 << (7 - i))) ? 255 : 0;
294826
-                    PPM_ASSIGN(pixrow[col + i], bf[0], bf[1], bf[2]);
294826
-                }
294826
+        for (cmd = 0, c = 0; c < cols; c += 8, cmd++) 
294826
+            for (i = 0; i < 8 && c + i < cols; i++) {
294826
+                for (p = 0; p < planes; p++) 
294826
+                    if (mode == 0 && cmd >= imlen[r * planes + p])
294826
+                        bf[p] = 0;
294826
+                    else
294826
+                        bf[p] = (image[r * planes + p][cmd] & 
294826
+                                 (1 << (7 - i))) ? 255 : 0;
294826
+                PPM_ASSIGN(pixrow[c + i], bf[0], bf[1], bf[2]);
294826
             }
294826
-        }
294826
-        ppm_writeppmrow(stdout, pixrow, cols, 255, 0);
294826
+        ppm_writeppmrow(stdout, pixrow, cols, (pixval) 255, 0);
294826
     }
294826
     pm_close(stdout);
294826
-
294826
-    return 0;
294826
+    exit(0);
294826
 }
294826
-
294826
-
294826
-
294826
diff -ruNp a/converter/ppm/ppmtoeyuv.c b/converter/ppm/ppmtoeyuv.c
294826
--- a/converter/ppm/ppmtoeyuv.c	2021-06-02 15:53:59.845205755 +0200
294826
+++ b/converter/ppm/ppmtoeyuv.c	2021-06-02 20:23:21.821634361 +0200
294826
@@ -114,6 +114,7 @@ create_multiplication_tables(const pixva
294826
 
294826
     int index;
294826
 
294826
+    overflow_add(maxval, 1);
294826
     MALLOCARRAY_NOFAIL(mult299   , maxval+1);
294826
     MALLOCARRAY_NOFAIL(mult587   , maxval+1);
294826
     MALLOCARRAY_NOFAIL(mult114   , maxval+1);
294826
diff -ruNp a/converter/ppm/ppmtolj.c b/converter/ppm/ppmtolj.c
294826
--- a/converter/ppm/ppmtolj.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/ppmtolj.c	2021-06-02 20:23:21.821634361 +0200
294826
@@ -182,6 +182,7 @@ int main(int argc, char *argv[]) {
294826
     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
294826
     pixelrow = ppm_allocrow( cols );
294826
 
294826
+    overflow2(cols, 6);
294826
     obuf = (unsigned char *) pm_allocrow(cols * 3, sizeof(unsigned char));
294826
     cbuf = (unsigned char *) pm_allocrow(cols * 6, sizeof(unsigned char));
294826
     if (mode == C_TRANS_MODE_DELTA)
294826
diff -ruNp a/converter/ppm/ppmtomitsu.c b/converter/ppm/ppmtomitsu.c
294826
--- a/converter/ppm/ppmtomitsu.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/ppmtomitsu.c	2021-06-02 20:23:21.822634370 +0200
294826
@@ -685,6 +685,8 @@ main(int argc, char * argv[]) {
294826
         medias = MSize_User;
294826
 
294826
     if (dpi300) {
294826
+        overflow2(medias.maxcols, 2);
294826
+        overflow2(medias.maxrows, 2);
294826
         medias.maxcols *= 2;
294826
         medias.maxrows *= 2;
294826
     }
294826
diff -ruNp a/converter/ppm/ppmtopcx.c b/converter/ppm/ppmtopcx.c
294826
--- a/converter/ppm/ppmtopcx.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/ppmtopcx.c	2021-06-02 20:23:21.823634378 +0200
294826
@@ -31,13 +31,13 @@
294826
 #define PCX_MAXVAL      (pixval)255
294826
 
294826
 
294826
-struct CmdlineInfo {
294826
+struct cmdlineInfo {
294826
     /* All the information the user supplied in the command line,
294826
        in a form easy for the program to use.
294826
     */
294826
-    const char * inputFilespec;  /* '-' if stdin */
294826
+    const char *inputFilespec;  /* '-' if stdin */
294826
     unsigned int truecolor;   /* -24bit option */
294826
-    unsigned int use8Bit; /* -8bit option */
294826
+    unsigned int use_8_bit; /* -8bit option */
294826
     unsigned int planes;    /* zero means minimum */
294826
     unsigned int packed;
294826
     unsigned int verbose;
294826
@@ -49,16 +49,16 @@ struct CmdlineInfo {
294826
 
294826
 
294826
 
294826
-struct PcxCmapEntry {
294826
+struct pcxCmapEntry {
294826
     unsigned char r;
294826
     unsigned char g;
294826
     unsigned char b;
294826
 };
294826
 
294826
-static struct PcxCmapEntry
294826
+static struct pcxCmapEntry
294826
 pcxCmapEntryFromPixel(pixel const colorPixel) {
294826
 
294826
-    struct PcxCmapEntry retval;
294826
+    struct pcxCmapEntry retval;
294826
 
294826
     retval.r = PPM_GETR(colorPixel);
294826
     retval.g = PPM_GETG(colorPixel);
294826
@@ -70,8 +70,8 @@ pcxCmapEntryFromPixel(pixel const colorP
294826
 
294826
 
294826
 static void
294826
-parseCommandLine(int argc, const char ** argv,
294826
-                 struct CmdlineInfo * const cmdlineP) {
294826
+parseCommandLine(int argc, char ** argv,
294826
+                 struct cmdlineInfo * const cmdlineP) {
294826
 /*----------------------------------------------------------------------------
294826
    parse program command line described in Unix standard form by argc
294826
    and argv.  Return the information in the options as *cmdlineP.
294826
@@ -82,7 +82,7 @@ parseCommandLine(int argc, const char **
294826
    Note that the strings we return are stored in the storage that
294826
    was passed to us as the argv array.  We also trash *argv.
294826
 -----------------------------------------------------------------------------*/
294826
-    optEntry * option_def;
294826
+    optEntry *option_def;
294826
         /* Instructions to pm_optParseOptions3 on how to parse our options.
294826
          */
294826
     optStruct3 opt;
294826
@@ -97,7 +97,7 @@ parseCommandLine(int argc, const char **
294826
     OPTENT3(0, "24bit",     OPT_FLAG,   NULL,
294826
             &cmdlineP->truecolor,    0);
294826
     OPTENT3(0, "8bit",      OPT_FLAG,   NULL,
294826
-            &cmdlineP->use8Bit,    0);
294826
+            &cmdlineP->use_8_bit,    0);
294826
     OPTENT3(0, "planes",    OPT_UINT,   &cmdlineP->planes,
294826
             &planesSpec,             0);
294826
     OPTENT3(0, "packed",    OPT_FLAG,   NULL,
294826
@@ -115,7 +115,7 @@ parseCommandLine(int argc, const char **
294826
     opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
294826
     opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
294826
 
294826
-    pm_optParseOptions3( &argc, (char **)argv, opt, sizeof(opt), 0 );
294826
+    pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 );
294826
         /* Uses and sets argc, argv, and some of *cmdline_p and others. */
294826
 
294826
     if (!xposSpec)
294826
@@ -141,7 +141,7 @@ parseCommandLine(int argc, const char **
294826
             pm_error("-planes is meaningless with -packed.");
294826
         if (cmdlineP->truecolor)
294826
             pm_error("-planes is meaningless with -24bit");
294826
-        if (cmdlineP->use8Bit)
294826
+        if (cmdlineP->use_8_bit)
294826
             pm_error("-planes is meaningless with -8bit");
294826
     }
294826
 
294826
@@ -151,7 +151,7 @@ parseCommandLine(int argc, const char **
294826
     if (!paletteSpec)
294826
         cmdlineP->palette = NULL;
294826
 
294826
-    if (cmdlineP->use8Bit && cmdlineP->truecolor)
294826
+    if (cmdlineP->use_8_bit && cmdlineP->truecolor)
294826
         pm_error("You cannot specify both -8bit and -truecolor");
294826
 
294826
     if (argc-1 < 1)
294826
@@ -172,8 +172,8 @@ parseCommandLine(int argc, const char **
294826
  * Write out a two-byte little-endian word to the PCX file
294826
  */
294826
 static void
294826
-putword(unsigned int const w,
294826
-        FILE *       const fp) {
294826
+Putword(int    const w,
294826
+        FILE * const fp) {
294826
 
294826
     int rc;
294826
 
294826
@@ -189,13 +189,12 @@ putword(unsigned int const w,
294826
  * Write out a byte to the PCX file
294826
  */
294826
 static void
294826
-putbyte(unsigned int const b,
294826
-        FILE *       const ofP) {
294826
+Putbyte(int    const b,
294826
+        FILE * const fp) {
294826
 
294826
     int rc;
294826
 
294826
-    rc = fputc(b & 0xff, ofP);
294826
-
294826
+    rc = fputc(b & 0xff, fp);
294826
     if (rc == EOF)
294826
         pm_error("Error writing byte to output file.");
294826
 }
294826
@@ -204,9 +203,9 @@ putbyte(unsigned int const b,
294826
 
294826
 static void
294826
 extractPlane(unsigned char * const rawrow,
294826
-             unsigned int    const cols,
294826
+             int             const cols,
294826
              unsigned char * const buf,
294826
-             unsigned int    const plane) {
294826
+             int             const plane) {
294826
 /*----------------------------------------------------------------------------
294826
    From the image row 'rawrow', which is an array of 'cols' palette indices
294826
    (as unsigned 8 bit integers), extract plane number 'plane' and return
294826
@@ -224,7 +223,9 @@ extractPlane(unsigned char * const rawro
294826
 
294826
     cp = buf;  /* initial value */
294826
 
294826
-    for (col = 0, cbit = 7, byteUnderConstruction = 0x00; col < cols; ++col) {
294826
+    cbit = 7;
294826
+    byteUnderConstruction = 0x00;
294826
+    for (col = 0; col < cols; ++col) {
294826
         if (rawrow[col] & planeMask)
294826
             byteUnderConstruction |= (1 << cbit);
294826
 
294826
@@ -246,17 +247,14 @@ extractPlane(unsigned char * const rawro
294826
 
294826
 
294826
 static void
294826
-packBits(unsigned char * const rawrow,
294826
-         unsigned int    const width,
294826
+PackBits(unsigned char * const rawrow,
294826
+         int             const width,
294826
          unsigned char * const buf,
294826
-         unsigned int    const bits) {
294826
+         int             const bits) {
294826
 
294826
-    unsigned int x;
294826
-    int i;
294826
-    int shift;
294826
+    int x, i, shift;
294826
 
294826
-    shift = -1;
294826
-    i = -1;
294826
+    shift = i = -1;
294826
 
294826
     for (x = 0; x < width; ++x) {
294826
         if (shift < 0) {
294826
@@ -272,73 +270,71 @@ packBits(unsigned char * const rawrow,
294826
 
294826
 
294826
 static void
294826
-writeHeader(FILE *              const ofP,
294826
-            unsigned int        const cols,
294826
-            unsigned int        const rows,
294826
-            unsigned int        const bitsPerPixel,
294826
-            unsigned int        const planes,
294826
-            struct PcxCmapEntry const cmap16[],
294826
-            unsigned int        const xPos,
294826
-            unsigned int        const yPos) {
294826
-
294826
-    unsigned int bytesPerLine;
294826
-
294826
-    putbyte(PCX_MAGIC, ofP);        /* .PCX magic number            */
294826
-    putbyte(0x05, ofP);             /* PC Paintbrush version        */
294826
-    putbyte(0x01, ofP);             /* .PCX run length encoding     */
294826
-    putbyte(bitsPerPixel, ofP);     /* bits per pixel               */
294826
-
294826
-    putword(xPos, ofP);             /* x1   - image left            */
294826
-    putword(yPos, ofP);             /* y1   - image top             */
294826
-    putword(xPos+cols-1, ofP);      /* x2   - image right           */
294826
-    putword(yPos+rows-1, ofP);      /* y2   - image bottom          */
294826
+write_header(FILE *              const fp,
294826
+             int                 const cols,
294826
+             int                 const rows,
294826
+             int                 const BitsPerPixel,
294826
+             int                 const Planes,
294826
+             struct pcxCmapEntry const cmap16[],
294826
+             unsigned int        const xPos,
294826
+             unsigned int        const yPos) {
294826
+
294826
+    int i, BytesPerLine;
294826
+
294826
+    Putbyte(PCX_MAGIC, fp);        /* .PCX magic number            */
294826
+    Putbyte(0x05, fp);             /* PC Paintbrush version        */
294826
+    Putbyte(0x01, fp);             /* .PCX run length encoding     */
294826
+    Putbyte(BitsPerPixel, fp);     /* bits per pixel               */
294826
+
294826
+    Putword(xPos, fp);             /* x1   - image left            */
294826
+    Putword(yPos, fp);             /* y1   - image top             */
294826
+    Putword(xPos+cols-1, fp);      /* x2   - image right           */
294826
+    Putword(yPos+rows-1, fp);      /* y2   - image bottom          */
294826
 
294826
-    putword(cols, ofP);             /* horizontal resolution        */
294826
-    putword(rows, ofP);             /* vertical resolution          */
294826
+    Putword(cols, fp);             /* horizontal resolution        */
294826
+    Putword(rows, fp);             /* vertical resolution          */
294826
 
294826
     /* Write out the Color Map for images with 16 colors or less */
294826
-    if (cmap16) {
294826
-        unsigned int i;
294826
+    if (cmap16)
294826
         for (i = 0; i < 16; ++i) {
294826
-            putbyte(cmap16[i].r, ofP);
294826
-            putbyte(cmap16[i].g, ofP);
294826
-            putbyte(cmap16[i].b, ofP);
294826
+            Putbyte(cmap16[i].r, fp);
294826
+            Putbyte(cmap16[i].g, fp);
294826
+            Putbyte(cmap16[i].b, fp);
294826
         }
294826
-    } else {
294826
+    else {
294826
         unsigned int i;
294826
         for (i = 0; i < 16; ++i) {
294826
-            putbyte(0, ofP);
294826
-            putbyte(0, ofP);
294826
-            putbyte(0, ofP);
294826
+            Putbyte(0, fp);
294826
+            Putbyte(0, fp);
294826
+            Putbyte(0, fp);
294826
         }
294826
     }
294826
-    putbyte(0, ofP);                /* reserved byte                */
294826
-    putbyte(planes, ofP);           /* number of color planes       */
294826
+    Putbyte(0, fp);                /* reserved byte                */
294826
+    Putbyte(Planes, fp);           /* number of color planes       */
294826
 
294826
-    bytesPerLine = ((cols * bitsPerPixel) + 7) / 8;
294826
-    putword(bytesPerLine, ofP);    /* number of bytes per scanline */
294826
+    BytesPerLine = ((cols * BitsPerPixel) + 7) / 8;
294826
+    Putword(BytesPerLine, fp);    /* number of bytes per scanline */
294826
 
294826
-    putword(1, ofP);                /* palette info                 */
294826
+    Putword(1, fp);                /* palette info                 */
294826
 
294826
     {
294826
         unsigned int i;
294826
         for (i = 0; i < 58; ++i)        /* fill to end of header        */
294826
-            putbyte(0, ofP);
294826
+            Putbyte(0, fp);
294826
     }
294826
 }
294826
 
294826
 
294826
 
294826
 static void
294826
-pcxEncode(FILE *                const ofP,
294826
+PCXEncode(FILE *                const fp,
294826
           const unsigned char * const buf,
294826
-          unsigned int          const size) {
294826
+          int                   const Size) {
294826
 
294826
-    const unsigned char * const end = buf + size;
294826
+    const unsigned char * const end = buf + Size;
294826
 
294826
     const unsigned char * currentP;
294826
-    unsigned int          previous;
294826
-    unsigned int          count;
294826
+    int previous, count;
294826
 
294826
     currentP = buf;
294826
     previous = *currentP++;
294826
@@ -351,19 +347,19 @@ pcxEncode(FILE *                const of
294826
         else {
294826
             if (count > 1 || (previous & 0xc0) == 0xc0) {
294826
                 count |= 0xc0;
294826
-                putbyte ( count , ofP );
294826
+                Putbyte ( count , fp );
294826
             }
294826
-            putbyte(previous, ofP);
294826
+            Putbyte(previous, fp);
294826
             previous = c;
294826
-            count = 1;
294826
+            count   = 1;
294826
         }
294826
     }
294826
 
294826
     if (count > 1 || (previous & 0xc0) == 0xc0) {
294826
         count |= 0xc0;
294826
-        putbyte(count, ofP);
294826
+        Putbyte ( count , fp );
294826
     }
294826
-    putbyte(previous, ofP);
294826
+    Putbyte(previous, fp);
294826
 }
294826
 
294826
 
294826
@@ -390,46 +386,63 @@ indexOfColor(colorhash_table const cht,
294826
 
294826
 
294826
 static void
294826
-writeRaster16Color(FILE * const ofP,
294826
-                   pixel **            const pixels,
294826
-                   unsigned int        const cols,
294826
-                   unsigned int        const rows,
294826
-                   unsigned int        const planes,
294826
-                   colorhash_table     const cht,
294826
-                   bool                const packbits,
294826
-                   unsigned int        const bitsPerPixel) {
294826
-
294826
-    unsigned int const bytesPerLine = ((cols * bitsPerPixel) + 7) / 8;
294826
-
294826
-    unsigned char * indexRow;  /* malloc'ed */
294826
-    /* indexRow[x] is the palette index of the pixel at column x of
294826
-       the row currently being processed
294826
-    */
294826
-    unsigned char * planesrow; /* malloc'ed */
294826
-    /* This is the input for a single row to the compressor */
294826
+ppmTo16ColorPcx(pixel **            const pixels,
294826
+                int                 const cols,
294826
+                int                 const rows,
294826
+                struct pcxCmapEntry const pcxcmap[],
294826
+                int                 const colors,
294826
+                colorhash_table     const cht,
294826
+                bool                const packbits,
294826
+                unsigned int        const planesRequested,
294826
+                unsigned int        const xPos,
294826
+                unsigned int        const yPos) {
294826
 
294826
-    unsigned int row;
294826
+    int Planes, BytesPerLine, BitsPerPixel;
294826
+    unsigned char *indexRow;  /* malloc'ed */
294826
+        /* indexRow[x] is the palette index of the pixel at column x of
294826
+           the row currently being processed
294826
+        */
294826
+    unsigned char *planesrow; /* malloc'ed */
294826
+        /* This is the input for a single row to the compressor */
294826
+    int row;
294826
 
294826
+    if (packbits) {
294826
+        Planes = 1;
294826
+        if (colors > 4)        BitsPerPixel = 4;
294826
+        else if (colors > 2)   BitsPerPixel = 2;
294826
+        else                   BitsPerPixel = 1;
294826
+    } else {
294826
+        BitsPerPixel = 1;
294826
+        if (planesRequested)
294826
+            Planes = planesRequested;
294826
+        else {
294826
+            if (colors > 8)        Planes = 4;
294826
+            else if (colors > 4)   Planes = 3;
294826
+            else if (colors > 2)   Planes = 2;
294826
+            else                   Planes = 1;
294826
+        }
294826
+    }
294826
+    overflow2(BitsPerPixel, cols);
294826
+    overflow_add(BitsPerPixel * cols, 7);
294826
+    BytesPerLine = ((cols * BitsPerPixel) + 7) / 8;
294826
     MALLOCARRAY_NOFAIL(indexRow, cols);
294826
-    MALLOCARRAY(planesrow, bytesPerLine);
294826
-
294826
-    if (!planesrow)
294826
-        pm_error("Failed to allocate buffer for a line of %u bytes",
294826
-                 bytesPerLine);
294826
+    MALLOCARRAY_NOFAIL(planesrow, BytesPerLine);
294826
 
294826
+    write_header(stdout, cols, rows, BitsPerPixel, Planes, pcxcmap,
294826
+                 xPos, yPos);
294826
     for (row = 0; row < rows; ++row) {
294826
-        unsigned int col;
294826
+        int col;
294826
         for (col = 0; col < cols; ++col)
294826
             indexRow[col] = indexOfColor(cht, pixels[row][col]);
294826
 
294826
         if (packbits) {
294826
-            packBits(indexRow, cols, planesrow, bitsPerPixel);
294826
-            pcxEncode(ofP, planesrow, bytesPerLine);
294826
+            PackBits(indexRow, cols, planesrow, BitsPerPixel);
294826
+            PCXEncode(stdout, planesrow, BytesPerLine);
294826
         } else {
294826
             unsigned int plane;
294826
-            for (plane = 0; plane < planes; ++plane) {
294826
+            for (plane = 0; plane < Planes; ++plane) {
294826
                 extractPlane(indexRow, cols, planesrow, plane);
294826
-                pcxEncode(stdout, planesrow, bytesPerLine);
294826
+                PCXEncode(stdout, planesrow, BytesPerLine);
294826
             }
294826
         }
294826
     }
294826
@@ -440,142 +453,83 @@ writeRaster16Color(FILE * const ofP,
294826
 
294826
 
294826
 static void
294826
-ppmTo16ColorPcx(pixel **            const pixels,
294826
-                unsigned int        const cols,
294826
-                unsigned int        const rows,
294826
-                struct PcxCmapEntry const pcxcmap[],
294826
-                unsigned int        const colorCt,
294826
-                colorhash_table     const cht,
294826
-                bool                const packbits,
294826
-                unsigned int        const planesRequested,
294826
-                unsigned int        const xPos,
294826
-                unsigned int        const yPos) {
294826
-
294826
-    unsigned int planes;
294826
-    unsigned int bitsPerPixel;
294826
-
294826
-    if (packbits) {
294826
-        planes = 1;
294826
-        if (colorCt > 4)        bitsPerPixel = 4;
294826
-        else if (colorCt > 2)   bitsPerPixel = 2;
294826
-        else                    bitsPerPixel = 1;
294826
-    } else {
294826
-        bitsPerPixel = 1;
294826
-        if (planesRequested)
294826
-            planes = planesRequested;
294826
-        else {
294826
-            if (colorCt > 8)        planes = 4;
294826
-            else if (colorCt > 4)   planes = 3;
294826
-            else if (colorCt > 2)   planes = 2;
294826
-            else                   planes = 1;
294826
-        }
294826
-    }
294826
-
294826
-    writeHeader(stdout, cols, rows, bitsPerPixel, planes, pcxcmap,
294826
-                xPos, yPos);
294826
-
294826
-    writeRaster16Color(stdout, pixels, cols, rows, planes, cht, packbits,
294826
-                       bitsPerPixel);
294826
-}
294826
-
294826
-
294826
-
294826
-static void
294826
 ppmTo256ColorPcx(pixel **            const pixels,
294826
-                 unsigned int        const cols,
294826
-                 unsigned int        const rows,
294826
-                 struct PcxCmapEntry const pcxcmap[],
294826
-                 unsigned int        const colorCt,
294826
+                 int                 const cols,
294826
+                 int                 const rows,
294826
+                 struct pcxCmapEntry const pcxcmap[],
294826
+                 int                 const colors,
294826
                  colorhash_table     const cht,
294826
                  unsigned int        const xPos,
294826
                  unsigned int        const yPos) {
294826
 
294826
-    unsigned char * rawrow;
294826
-    unsigned int    row;
294826
-
294826
-    MALLOCARRAY(rawrow, cols);
294826
+    int row;
294826
+    unsigned int i;
294826
+    unsigned char *rawrow;
294826
 
294826
-    if (!rawrow)
294826
-        pm_error("Failed to allocate a buffer for %u columns", cols);
294826
+    rawrow = (unsigned char *)pm_allocrow(cols, sizeof(unsigned char));
294826
 
294826
     /* 8 bits per pixel, 1 plane */
294826
-    writeHeader(stdout, cols, rows, 8, 1, NULL, xPos, yPos);
294826
+    write_header(stdout, cols, rows, 8, 1, NULL, xPos, yPos);
294826
     for (row = 0; row < rows; ++row) {
294826
-        unsigned int col;
294826
+        int col;
294826
         for (col = 0; col < cols; ++col)
294826
             rawrow[col] = indexOfColor(cht, pixels[row][col]);
294826
-        pcxEncode(stdout, rawrow, cols);
294826
-
294826
+        PCXEncode(stdout, rawrow, cols);
294826
     }
294826
-    putbyte(PCX_256_COLORS, stdout);
294826
-
294826
-    {
294826
-        unsigned int i;
294826
-
294826
-        for (i = 0; i < MAXCOLORS; ++i) {
294826
-            putbyte(pcxcmap[i].r, stdout);
294826
-            putbyte(pcxcmap[i].g, stdout);
294826
-            putbyte(pcxcmap[i].b, stdout);
294826
-        }
294826
+    Putbyte(PCX_256_COLORS, stdout);
294826
+    for (i = 0; i < MAXCOLORS; ++i) {
294826
+        Putbyte(pcxcmap[i].r, stdout);
294826
+        Putbyte(pcxcmap[i].g, stdout);
294826
+        Putbyte(pcxcmap[i].b, stdout);
294826
     }
294826
-    free(rawrow);
294826
+    pm_freerow((void*)rawrow);
294826
 }
294826
 
294826
 
294826
 
294826
 static void
294826
 ppmToTruecolorPcx(pixel **     const pixels,
294826
-                  unsigned int const cols,
294826
-                  unsigned int const rows,
294826
+                  int          const cols,
294826
+                  int          const rows,
294826
                   pixval       const maxval,
294826
                   unsigned int const xPos,
294826
                   unsigned int const yPos) {
294826
 
294826
-    unsigned char * redrow;
294826
-    unsigned char * grnrow;
294826
-    unsigned char * blurow;
294826
-    unsigned int    row;
294826
-
294826
-    MALLOCARRAY(redrow, cols);
294826
-    MALLOCARRAY(grnrow, cols);
294826
-    MALLOCARRAY(blurow, cols);
294826
+    unsigned char *redrow, *greenrow, *bluerow;
294826
+    int col, row;
294826
 
294826
-    if (!redrow || !grnrow || !blurow)
294826
-        pm_error("Unable to allocate buffer for a row of %u pixels", cols);
294826
+    redrow   = (unsigned char *)pm_allocrow(cols, sizeof(unsigned char));
294826
+    greenrow = (unsigned char *)pm_allocrow(cols, sizeof(unsigned char));
294826
+    bluerow  = (unsigned char *)pm_allocrow(cols, sizeof(unsigned char));
294826
 
294826
     /* 8 bits per pixel, 3 planes */
294826
-    writeHeader(stdout, cols, rows, 8, 3, NULL, xPos, yPos);
294826
-
294826
-    for (row = 0; row < rows; ++row) {
294826
-        pixel * const pixrow = pixels[row];
294826
-
294826
-        unsigned int col;
294826
-
294826
-        for (col = 0; col < cols; ++col) {
294826
-            pixel const pix = pixrow[col];
294826
-
294826
-            if (maxval != PCX_MAXVAL) {
294826
-                redrow[col] = (long)PPM_GETR(pix) * PCX_MAXVAL / maxval;
294826
-                grnrow[col] = (long)PPM_GETG(pix) * PCX_MAXVAL / maxval;
294826
-                blurow[col] = (long)PPM_GETB(pix) * PCX_MAXVAL / maxval;
294826
-            } else {
294826
-                redrow[col] = PPM_GETR(pix);
294826
-                grnrow[col] = PPM_GETG(pix);
294826
-                blurow[col] = PPM_GETB(pix);
294826
+    write_header(stdout, cols, rows, 8, 3, NULL, xPos, yPos);
294826
+    for( row = 0; row < rows; row++ ) {
294826
+        register pixel *pP = pixels[row];
294826
+        for( col = 0; col < cols; col++, pP++ ) {
294826
+            if( maxval != PCX_MAXVAL ) {
294826
+                redrow[col]   = (long)PPM_GETR(*pP) * PCX_MAXVAL / maxval;
294826
+                greenrow[col] = (long)PPM_GETG(*pP) * PCX_MAXVAL / maxval;
294826
+                bluerow[col]  = (long)PPM_GETB(*pP) * PCX_MAXVAL / maxval;
294826
+            }
294826
+            else {
294826
+                redrow[col]   = PPM_GETR(*pP);
294826
+                greenrow[col] = PPM_GETG(*pP);
294826
+                bluerow[col]  = PPM_GETB(*pP);
294826
             }
294826
         }
294826
-        pcxEncode(stdout, redrow, cols);
294826
-        pcxEncode(stdout, grnrow, cols);
294826
-        pcxEncode(stdout, blurow, cols);
294826
+        PCXEncode(stdout, redrow, cols);
294826
+        PCXEncode(stdout, greenrow, cols);
294826
+        PCXEncode(stdout, bluerow, cols);
294826
     }
294826
-    free(blurow);
294826
-    free(grnrow);
294826
-    free(redrow);
294826
+    pm_freerow((void*)bluerow);
294826
+    pm_freerow((void*)greenrow);
294826
+    pm_freerow((void*)redrow);
294826
 }
294826
 
294826
 
294826
 
294826
-static const struct PcxCmapEntry
294826
+static const struct pcxCmapEntry
294826
 stdPalette[] = {
294826
     {   0,   0,   0 },
294826
     {   0,   0, 170 },
294826
@@ -653,19 +607,20 @@ putPcxColorInHash(colorhash_table const
294826
 
294826
 
294826
 static void
294826
-generateStandardPalette(struct PcxCmapEntry ** const pcxcmapP,
294826
+generateStandardPalette(struct pcxCmapEntry ** const pcxcmapP,
294826
                         pixval                 const maxval,
294826
                         colorhash_table *      const chtP,
294826
-                        unsigned int *         const colorsP) {
294826
+                        int *                  const colorsP) {
294826
 
294826
     unsigned int const stdPaletteSize = 16;
294826
-
294826
-    unsigned int          colorIndex;
294826
-    struct PcxCmapEntry * pcxcmap;
294826
-    colorhash_table       cht;
294826
+    unsigned int colorIndex;
294826
+    struct pcxCmapEntry * pcxcmap;
294826
+    colorhash_table cht;
294826
 
294826
     MALLOCARRAY_NOFAIL(pcxcmap, MAXCOLORS);
294826
 
294826
+    *pcxcmapP = pcxcmap;
294826
+
294826
     cht = ppm_alloccolorhash();
294826
 
294826
     for (colorIndex = 0; colorIndex < stdPaletteSize; ++colorIndex) {
294826
@@ -692,7 +647,6 @@ generateStandardPalette(struct PcxCmapEn
294826
         pcxcmap[colorIndex].b = 0;
294826
     }
294826
 
294826
-    *pcxcmapP = pcxcmap;
294826
     *chtP = cht;
294826
     *colorsP = stdPaletteSize;
294826
 }
294826
@@ -721,10 +675,11 @@ readPpmPalette(const char *   const pale
294826
                  *paletteSizeP, MAXCOLORS);
294826
 
294826
     {
294826
-        unsigned int j;
294826
-        unsigned int row;
294826
-        for (row = 0, j = 0; row < rows; ++row) {
294826
-            unsigned int col;
294826
+        int j;
294826
+        int row;
294826
+        j = 0;  /* initial value */
294826
+        for (row = 0; row < rows; ++row) {
294826
+            int col;
294826
             for (col = 0; col < cols; ++col)
294826
                 (*ppmPaletteP)[j++] = pixels[row][col];
294826
         }
294826
@@ -735,16 +690,16 @@ readPpmPalette(const char *   const pale
294826
 
294826
 
294826
 static void
294826
-readPaletteFromFile(struct PcxCmapEntry ** const pcxcmapP,
294826
+readPaletteFromFile(struct pcxCmapEntry ** const pcxcmapP,
294826
                     const char *           const paletteFileName,
294826
                     pixval                 const maxval,
294826
                     colorhash_table *      const chtP,
294826
-                    unsigned int *         const colorsP) {
294826
+                    int *                  const colorsP) {
294826
 
294826
     unsigned int colorIndex;
294826
     pixel ppmPalette[MAXCOLORS];
294826
     unsigned int paletteSize;
294826
-    struct PcxCmapEntry * pcxcmap;
294826
+    struct pcxCmapEntry * pcxcmap;
294826
     colorhash_table cht;
294826
 
294826
     readPpmPalette(paletteFileName, &ppmPalette, &paletteSize);
294826
@@ -777,7 +732,7 @@ readPaletteFromFile(struct PcxCmapEntry
294826
 
294826
 static void
294826
 moveBlackToIndex0(colorhist_vector const chv,
294826
-                  unsigned int     const colorCt) {
294826
+                  int              const colors) {
294826
 /*----------------------------------------------------------------------------
294826
    If black is in the palette, make it at Index 0.
294826
 -----------------------------------------------------------------------------*/
294826
@@ -789,7 +744,7 @@ moveBlackToIndex0(colorhist_vector const
294826
 
294826
     blackPresent = FALSE;  /* initial assumption */
294826
 
294826
-    for (i = 0; i < colorCt; ++i)
294826
+    for (i = 0; i < colors; ++i)
294826
         if (PPM_EQUAL(chv[i].color, blackPixel))
294826
             blackPresent = TRUE;
294826
 
294826
@@ -798,11 +753,10 @@ moveBlackToIndex0(colorhist_vector const
294826
            beginning of the table and if the color is already elsewhere in
294826
            the table, removes it.
294826
         */
294826
-        int colorCt2;
294826
-
294826
-        colorCt2 = colorCt;
294826
-        ppm_addtocolorhist(chv, &colorCt2, MAXCOLORS, &blackPixel, 0, 0);
294826
-        assert(colorCt2 == colorCt);
294826
+        int colors2;
294826
+        colors2 = colors;
294826
+        ppm_addtocolorhist(chv, &colors2, MAXCOLORS, &blackPixel, 0, 0);
294826
+        assert(colors2 == colors);
294826
     }
294826
 }
294826
 
294826
@@ -810,12 +764,12 @@ moveBlackToIndex0(colorhist_vector const
294826
 
294826
 static void
294826
 makePcxColormapFromImage(pixel **               const pixels,
294826
-                         unsigned int           const cols,
294826
-                         unsigned int           const rows,
294826
+                         int                    const cols,
294826
+                         int                    const rows,
294826
                          pixval                 const maxval,
294826
-                         struct PcxCmapEntry ** const pcxcmapP,
294826
+                         struct pcxCmapEntry ** const pcxcmapP,
294826
                          colorhash_table *      const chtP,
294826
-                         unsigned int *         const colorCtP,
294826
+                         int *                  const colorsP,
294826
                          bool *                 const tooManyColorsP) {
294826
 /*----------------------------------------------------------------------------
294826
    Make a colormap (palette) for the PCX header that can be used
294826
@@ -831,29 +785,29 @@ makePcxColormapFromImage(pixel **
294826
    Iff there are too many colors to do that (i.e. more than 256),
294826
    return *tooManyColorsP == TRUE.
294826
 -----------------------------------------------------------------------------*/
294826
-    int colorCt;
294826
+    int colors;
294826
     colorhist_vector chv;
294826
 
294826
     pm_message("computing colormap...");
294826
 
294826
-    chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colorCt);
294826
+    chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors);
294826
     if (chv == NULL)
294826
         *tooManyColorsP = TRUE;
294826
     else {
294826
-        unsigned int i;
294826
-        struct PcxCmapEntry * pcxcmap;
294826
+        int i;
294826
+        struct pcxCmapEntry * pcxcmap;
294826
 
294826
         *tooManyColorsP = FALSE;
294826
 
294826
-        pm_message("%d colors found", colorCt);
294826
+        pm_message("%d colors found", colors);
294826
 
294826
-        moveBlackToIndex0(chv, colorCt);
294826
+        moveBlackToIndex0(chv, colors);
294826
 
294826
         MALLOCARRAY_NOFAIL(pcxcmap, MAXCOLORS);
294826
 
294826
         *pcxcmapP = pcxcmap;
294826
 
294826
-        for (i = 0; i < colorCt; ++i) {
294826
+        for (i = 0; i < colors; ++i) {
294826
             pixel p;
294826
 
294826
             PPM_DEPTH(p, chv[i].color, maxval, PCX_MAXVAL);
294826
@@ -870,9 +824,9 @@ makePcxColormapFromImage(pixel **
294826
             pcxcmap[i].b = 0;
294826
         }
294826
 
294826
-        *chtP = ppm_colorhisttocolorhash(chv, colorCt);
294826
+        *chtP = ppm_colorhisttocolorhash(chv, colors);
294826
 
294826
-        *colorCtP = colorCt;
294826
+        *colorsP = colors;
294826
 
294826
         ppm_freecolorhist(chv);
294826
     }
294826
@@ -882,43 +836,43 @@ makePcxColormapFromImage(pixel **
294826
 
294826
 static void
294826
 ppmToPalettePcx(pixel **            const pixels,
294826
-                unsigned int        const cols,
294826
-                unsigned int        const rows,
294826
+                int                 const cols,
294826
+                int                 const rows,
294826
                 pixval              const maxval,
294826
                 unsigned int        const xPos,
294826
                 unsigned int        const yPos,
294826
-                struct PcxCmapEntry const pcxcmap[],
294826
+                struct pcxCmapEntry const pcxcmap[],
294826
                 colorhash_table     const cht,
294826
-                unsigned int        const colorCt,
294826
+                int                 const colors,
294826
                 bool                const packbits,
294826
                 unsigned int        const planes,
294826
-                bool                const use8Bit) {
294826
+                bool                const use_8_bit) {
294826
 
294826
     /* convert image */
294826
-    if (colorCt <= 16 && !use8Bit )
294826
-        ppmTo16ColorPcx(pixels, cols, rows, pcxcmap, colorCt, cht,
294826
+    if( colors <= 16 && !use_8_bit )
294826
+        ppmTo16ColorPcx(pixels, cols, rows, pcxcmap, colors, cht,
294826
                         packbits, planes, xPos, yPos);
294826
     else
294826
-        ppmTo256ColorPcx(pixels, cols, rows, pcxcmap, colorCt, cht,
294826
+        ppmTo256ColorPcx(pixels, cols, rows, pcxcmap, colors, cht,
294826
                          xPos, yPos);
294826
 }
294826
 
294826
 
294826
 
294826
 int
294826
-main(int argc, const char *argv[]) {
294826
+main(int argc, char *argv[]) {
294826
 
294826
-    struct CmdlineInfo cmdline;
294826
-    FILE * ifP;
294826
+    struct cmdlineInfo cmdline;
294826
+    FILE* ifP;
294826
     int rows, cols;
294826
     pixval maxval;
294826
     pixel **pixels;
294826
-    struct PcxCmapEntry * pcxcmap;
294826
+    struct pcxCmapEntry * pcxcmap;
294826
     colorhash_table cht;
294826
     bool truecolor;
294826
-    unsigned int colorCt;
294826
+    int colors;
294826
 
294826
-    pm_proginit(&argc, argv);
294826
+    ppm_init(&argc, argv);
294826
 
294826
     parseCommandLine(argc, argv, &cmdline);
294826
 
294826
@@ -931,15 +885,15 @@ main(int argc, const char *argv[]) {
294826
     else {
294826
         if (cmdline.stdpalette) {
294826
             truecolor = FALSE;
294826
-            generateStandardPalette(&pcxcmap, maxval, &cht, &colorCt);
294826
+            generateStandardPalette(&pcxcmap, maxval, &cht, &colors);
294826
         } else if (cmdline.palette) {
294826
             truecolor = FALSE;
294826
             readPaletteFromFile(&pcxcmap, cmdline.palette, maxval,
294826
-                                &cht, &colorCt);
294826
+                                &cht, &colors);
294826
         } else {
294826
             bool tooManyColors;
294826
             makePcxColormapFromImage(pixels, cols, rows, maxval,
294826
-                                     &pcxcmap, &cht, &colorCt,
294826
+                                     &pcxcmap, &cht, &colors,
294826
                                      &tooManyColors);
294826
 
294826
             if (tooManyColors) {
294826
@@ -958,8 +912,8 @@ main(int argc, const char *argv[]) {
294826
     else {
294826
         ppmToPalettePcx(pixels, cols, rows, maxval,
294826
                         cmdline.xpos, cmdline.ypos,
294826
-                        pcxcmap, cht, colorCt, cmdline.packed,
294826
-                        cmdline.planes, cmdline.use8Bit);
294826
+                        pcxcmap, cht, colors, cmdline.packed,
294826
+                        cmdline.planes, cmdline.use_8_bit);
294826
 
294826
         ppm_freecolorhash(cht);
294826
         free(pcxcmap);
294826
diff -ruNp a/converter/ppm/ppmtopict.c b/converter/ppm/ppmtopict.c
294826
--- a/converter/ppm/ppmtopict.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/ppmtopict.c	2021-06-02 20:23:21.824634387 +0200
294826
@@ -450,6 +450,8 @@ main(int argc, const char ** argv) {
294826
     putShort(stdout, 0);            /* mode */
294826
 
294826
     /* Finally, write out the data. */
294826
+    overflow_add(cols/MAX_COUNT, 1);
294826
+    overflow_add(cols, cols/MAX_COUNT+1);
294826
     outBuf = malloc((unsigned)(cols+cols/MAX_COUNT+1));
294826
     for (row = 0, oc = 0; row < rows; ++row) {
294826
         unsigned int rowSize;
294826
diff -ruNp a/converter/ppm/ppmtopj.c b/converter/ppm/ppmtopj.c
294826
--- a/converter/ppm/ppmtopj.c	2021-06-02 15:53:59.845205755 +0200
294826
+++ b/converter/ppm/ppmtopj.c	2021-06-02 20:23:21.824634387 +0200
294826
@@ -179,6 +179,7 @@ char *argv[];
294826
 	pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
294826
 
294826
 	pm_close( ifp );
294826
+        overflow2(cols,2);
294826
 	obuf = (unsigned char *) pm_allocrow(cols, sizeof(unsigned char));
294826
 	cbuf = (unsigned char *) pm_allocrow(cols * 2, sizeof(unsigned char));
294826
 
294826
diff -ruNp a/converter/ppm/ppmtopjxl.c b/converter/ppm/ppmtopjxl.c
294826
--- a/converter/ppm/ppmtopjxl.c	2021-06-02 15:53:59.845205755 +0200
294826
+++ b/converter/ppm/ppmtopjxl.c	2021-06-02 20:23:21.825634396 +0200
294826
@@ -267,6 +267,9 @@ main(int argc, const char * argv[]) {
294826
     if (maxval > PCL_MAXVAL)
294826
         pm_error("color range too large; reduce with ppmcscale");
294826
 
294826
+    if (cols < 0 || rows < 0)
294826
+        pm_error("negative size is not possible");
294826
+
294826
     /* Figure out the colormap. */
294826
     pm_message("Computing colormap...");
294826
     chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors);
294826
@@ -286,6 +289,8 @@ main(int argc, const char * argv[]) {
294826
         case 0: /* direct mode (no palette) */
294826
             bpp = bitsperpixel(maxval); /* bits per pixel */
294826
             bpg = bpp; bpb = bpp;
294826
+            overflow2(bpp, 3);
294826
+            overflow_add(bpp*3, 7);
294826
             bpp = (bpp*3+7)>>3;     /* bytes per pixel now */
294826
             bpr = (bpp<<3)-bpg-bpb; 
294826
             bpp *= cols;            /* bytes per row now */
294826
@@ -295,9 +300,13 @@ main(int argc, const char * argv[]) {
294826
         case 3: case 7: pclindex++;
294826
         default:
294826
             bpp = 8/pclindex;
294826
+            overflow_add(cols, bpp);
294826
+            if(bpp == 0)
294826
+                pm_error("assert: no bpp");
294826
             bpp = (cols+bpp-1)/bpp;      /* bytes per row */
294826
         }
294826
     }
294826
+    overflow2(bpp,2);
294826
     inrow = (char *)malloc((unsigned)bpp);
294826
     outrow = (char *)malloc((unsigned)bpp*2);
294826
     runcnt = (signed char *)malloc((unsigned)bpp);
294826
diff -ruNp a/converter/ppm/ppmtowinicon.c b/converter/ppm/ppmtowinicon.c
294826
--- a/converter/ppm/ppmtowinicon.c	2021-06-02 15:53:59.845205755 +0200
294826
+++ b/converter/ppm/ppmtowinicon.c	2021-06-02 20:23:21.825634396 +0200
294826
@@ -12,6 +12,7 @@
294826
 
294826
 #include <math.h>
294826
 #include <string.h>
294826
+#include <stdlib.h>
294826
 
294826
 #include "pm_c_util.h"
294826
 #include "winico.h"
294826
@@ -214,6 +215,7 @@ createAndBitmap (gray ** const ba, int c
294826
    MALLOCARRAY_NOFAIL(rowData, rows);
294826
    icBitmap->xBytes = xBytes;
294826
    icBitmap->data   = rowData;
294826
+   overflow2(xBytes, rows);
294826
    icBitmap->size   = xBytes * rows;
294826
    for (y=0;y
294826
       u1 * row;
294826
@@ -342,6 +344,7 @@ create4Bitmap (pixel ** const pa, int co
294826
    MALLOCARRAY_NOFAIL(rowData, rows);
294826
    icBitmap->xBytes = xBytes;
294826
    icBitmap->data   = rowData;
294826
+   overflow2(xBytes, rows);
294826
    icBitmap->size   = xBytes * rows;
294826
 
294826
    for (y=0;y
294826
@@ -402,6 +405,7 @@ create8Bitmap (pixel ** const pa, int co
294826
    MALLOCARRAY_NOFAIL(rowData, rows);
294826
    icBitmap->xBytes = xBytes;
294826
    icBitmap->data   = rowData;
294826
+   overflow2(xBytes, rows);
294826
    icBitmap->size   = xBytes * rows;
294826
 
294826
    for (y=0;y
294826
@@ -709,7 +713,11 @@ addEntryToIcon(MS_Ico       const MSIcon
294826
     entry->bitcount      = bpp;
294826
     entry->ih            = createInfoHeader(entry, xorBitmap, andBitmap);
294826
     entry->colors        = palette->colors;
294826
-    entry->size_in_bytes = 
294826
+    overflow2(4, entry->color_count);
294826
+    overflow_add(xorBitmap->size, andBitmap->size);
294826
+    overflow_add(xorBitmap->size + andBitmap->size, 40);
294826
+    overflow_add(xorBitmap->size + andBitmap->size + 40, 4 * entry->color_count);
294826
+    entry->size_in_bytes =
294826
         xorBitmap->size + andBitmap->size + 40 + (4 * entry->color_count);
294826
     if (verbose) 
294826
         pm_message("entry->size_in_bytes = %d + %d + %d = %d",
294826
diff -ruNp a/converter/ppm/ppmtoxpm.c b/converter/ppm/ppmtoxpm.c
294826
--- a/converter/ppm/ppmtoxpm.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/ppmtoxpm.c	2021-06-02 20:23:21.826634405 +0200
294826
@@ -198,6 +198,7 @@ genNumstr(unsigned int const input, int
294826
     unsigned int i;
294826
 
294826
     /* Allocate memory for printed number.  Abort if error. */
294826
+    overflow_add(digits, 1);
294826
     if (!(str = (char *) malloc(digits + 1)))
294826
         pm_error("out of memory");
294826
 
294826
@@ -315,6 +316,7 @@ genCmap(colorhist_vector const chv,
294826
     unsigned int charsPerPixel;
294826
     unsigned int xpmMaxval;
294826
     
294826
+    if (includeTransparent) overflow_add(ncolors, 1);
294826
     MALLOCARRAY(cmap, cmapSize);
294826
     if (cmapP == NULL)
294826
         pm_error("Out of memory allocating %u bytes for a color map.",
294826
diff -ruNp a/converter/ppm/qrttoppm.c b/converter/ppm/qrttoppm.c
294826
--- a/converter/ppm/qrttoppm.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/qrttoppm.c	2021-06-02 20:23:21.826634405 +0200
294826
@@ -46,7 +46,7 @@ main( argc, argv )
294826
 
294826
     ppm_writeppminit( stdout, cols, rows, maxval, 0 );
294826
     pixelrow = ppm_allocrow( cols );
294826
-    buf = (unsigned char *) malloc( 3 * cols );
294826
+    buf = (unsigned char *) malloc2( 3 , cols );
294826
     if ( buf == (unsigned char *) 0 )
294826
 	pm_error( "out of memory" );
294826
 
294826
diff -ruNp a/converter/ppm/sldtoppm.c b/converter/ppm/sldtoppm.c
294826
--- a/converter/ppm/sldtoppm.c	2021-06-02 15:53:59.845205755 +0200
294826
+++ b/converter/ppm/sldtoppm.c	2021-06-02 20:23:21.827634413 +0200
294826
@@ -154,127 +154,85 @@ vscale(int * const px,
294826
 
294826
 
294826
 
294826
-static void
294826
-upcase(const char * const sname,
294826
-       char *       const uname) {
294826
-
294826
-    unsigned int i;
294826
-    const char * ip;
294826
-
294826
-    for (i = 0, ip = sname; i < 31; ++i) {
294826
-        char const ch = *ip++;
294826
-
294826
-        if (ch != EOS)
294826
-            uname[i] = islower(ch) ? toupper(ch) : ch;
294826
-    }
294826
-    uname[i] = EOS;
294826
-}
294826
-
294826
-
294826
-
294826
-static void
294826
-skipBytes(FILE *       const fileP,
294826
-          unsigned int const count) {
294826
-
294826
-    unsigned int i;
294826
-
294826
-    for (i = 0; i < count; ++i)
294826
-        getc(fileP);
294826
-}
294826
-
294826
-
294826
-
294826
-static void
294826
-scanDirectory(FILE *       const slFileP,
294826
-              long         const dirPos,
294826
-              bool         const dirOnly,
294826
-              const char * const uname,
294826
-              bool *       const foundP) {
294826
-/*----------------------------------------------------------------------------
294826
-   Scan the directory at the current position in *slFileP, either listing
294826
-   the directory ('dirOnly' true) or searching for a slide named
294826
-   'uname' ('dirOnly' false).
294826
-
294826
-   'dirPos' is the offset in the file of the directory, i.e. the current
294826
-   position of *slFileP.
294826
-
294826
-   In the latter case, return as *foundP whether the slide name is there.
294826
------------------------------------------------------------------------------*/
294826
-    bool found;
294826
-    bool eof;
294826
-    long pos;
294826
-    unsigned char libent[36];
294826
-
294826
-    for (found = false, eof = false, pos = dirPos; !found && !eof; ) {
294826
-        size_t readCt;
294826
-        readCt = fread(libent, 36, 1, slFileP);
294826
-        if (readCt != 1)
294826
-            eof = true;
294826
-        else {
294826
-            /* The directory entry is 32 bytes of NUL-terminated slide name
294826
-               followed by 4 bytes of offset of the next directory entry.
294826
-            */
294826
-            const char * const slideName = (const char *)(&libent[0]);
294826
-            if (pm_strnlen(slideName, 32) == 32)
294826
-                pm_error("Invalid input: slide name field is not "
294826
-                         "nul-terminated");
294826
-            else {
294826
-                if (strlen(slideName) == 0)
294826
-                    eof = true;
294826
-                else {
294826
-                    pos += 36;
294826
-                    if (dirOnly) {
294826
-                        pm_message("  %s", slideName);
294826
-                    } else if (streq(slideName, uname)) {
294826
-                        long const dpos =
294826
-                            (((((libent[35] << 8) | libent[34]) << 8) |
294826
-                              libent[33]) << 8) | libent[32];
294826
-
294826
-                        if ((slFileP == stdin) ||
294826
-                            (fseek(slFileP, dpos, 0) == -1)) {
294826
-
294826
-                            skipBytes(slFileP, dpos - pos);
294826
-                        }
294826
-                        found = true;
294826
-                    }
294826
-                }
294826
-            }
294826
-        }
294826
-    }
294826
-    *foundP = found;
294826
-}
294826
-
294826
 /*  SLIDEFIND  --  Find  a  slide  in  a  library  or,  if  DIRONLY is
294826
            nonzero, print a directory listing of the  library.
294826
            If  UCASEN  is nonzero, the requested slide name is
294826
            converted to upper case. */
294826
 
294826
 static void
294826
-slidefind(const char * const slideName,
294826
-          bool         const dirOnly,
294826
+slidefind(const char * const sname,
294826
+          bool         const dironly,
294826
           bool         const ucasen) {
294826
 
294826
-    char uname[32];  /* upper case translation of 'slideName' */
294826
-    char header[32]; /* (supposed) header read from file */
294826
+    char uname[32];
294826
+    unsigned char libent[36];
294826
+    long pos;
294826
     bool found;
294826
+    bool eof;
294826
 
294826
-    if (dirOnly)
294826
+    if (dironly)
294826
         pm_message("Slides in library:");
294826
     else {
294826
-        upcase(slideName, uname);
294826
+        unsigned int i;
294826
+        const char * ip;
294826
+        
294826
+        ip = sname; /* initial value */
294826
+        
294826
+        for (i = 0; i < 31; ++i) {
294826
+            char const ch = *ip++;
294826
+            if (ch == EOS)
294826
+                break;
294826
+
294826
+            {
294826
+                char const upperCh =
294826
+                    ucasen && islower(ch) ? toupper(ch) : ch;
294826
+                
294826
+                uname[i] = upperCh;
294826
+            }
294826
+        }
294826
+        uname[i] = EOS;
294826
     }
294826
 
294826
     /* Read slide library header and verify. */
294826
-
294826
-    if ((fread(header, 32, 1, slfile) != 1) ||
294826
-        (!STRSEQ(header, "AutoCAD Slide Library 1.0\r\n\32"))) {
294826
+    
294826
+    if ((fread(libent, 32, 1, slfile) != 1) ||
294826
+        (!streq((char *)libent, "AutoCAD Slide Library 1.0\015\012\32"))) {
294826
         pm_error("not an AutoCAD slide library file.");
294826
     }
294826
+    pos = 32;
294826
+    
294826
+    /* Search for a slide with the requested name or list the directory */
294826
+    
294826
+    for (found = false, eof = false; !found && !eof; ) {
294826
+        size_t readCt;
294826
+        readCt = fread(libent, 36, 1, slfile);
294826
+        if (readCt != 1)
294826
+            eof = true;
294826
+        else if (strlen((char *)libent) == 0)
294826
+            eof = true;
294826
+    }
294826
+    if (!eof) {
294826
+        pos += 36;
294826
+        if (dironly) {
294826
+            pm_message("  %s", libent);
294826
+        } else if (streq((char *)libent, uname)) {
294826
+            long dpos;
294826
+
294826
+            dpos = (((((libent[35] << 8) | libent[34]) << 8) |
294826
+                     libent[33]) << 8) | libent[32];
294826
+
294826
+            if ((slfile == stdin) || (fseek(slfile, dpos, 0) == -1)) {
294826
+                dpos -= pos;
294826
+        
294826
+                while (dpos-- > 0)
294826
+                    getc(slfile);
294826
+            }
294826
+            found = true;
294826
+        }
294826
+    }
294826
 
294826
-    scanDirectory(slfile, 32, dirOnly, ucasen ? uname : slideName, &found);
294826
-
294826
-    if (!found && !dirOnly)
294826
-        pm_error("slide '%s' not in library.", slideName);
294826
+    if (!found && !dironly)
294826
+        pm_error("slide '%s' not in library.", sname);
294826
 }
294826
 
294826
 
294826
@@ -392,7 +350,7 @@ slider(slvecfn   slvec,
294826
 
294826
     /* Verify that slide format is compatible with this program. */
294826
 
294826
-    if (!STRSEQ(slfrof.slh, slhi.slh))
294826
+    if (streq(slfrof.slh, slhi.slh))
294826
         pm_error("this is not an AutoCAD slide file.");
294826
 
294826
     /* Verify that the number format and file level in the header  are
294826
@@ -506,6 +464,8 @@ slider(slvecfn   slvec,
294826
 
294826
     /* Allocate image buffer and clear it to black. */
294826
 
294826
+    overflow_add(ixdots, 1);
294826
+    overflow_add(iydots, 1);
294826
     pixels = ppm_allocarray(pixcols = ixdots + 1, pixrows = iydots + 1);
294826
     PPM_ASSIGN(rgbcolor, 0, 0, 0);
294826
     ppmd_filledrectangle(pixels, pixcols, pixrows, pixmaxval, 0, 0,
294826
diff -ruNp a/converter/ppm/ximtoppm.c b/converter/ppm/ximtoppm.c
294826
--- a/converter/ppm/ximtoppm.c	2021-06-02 15:53:59.846205765 +0200
294826
+++ b/converter/ppm/ximtoppm.c	2021-06-02 20:23:21.828634422 +0200
294826
@@ -22,9 +22,7 @@
294826
 #include "shhopt.h"
294826
 #include "nstring.h"
294826
 
294826
-
294826
-
294826
-struct CmdlineInfo {
294826
+struct cmdlineInfo {
294826
     /* All the information the user supplied in the command line,
294826
        in a form easy for the program to use.
294826
     */
294826
@@ -36,8 +34,8 @@ struct CmdlineInfo {
294826
 
294826
 
294826
 static void
294826
-parseCommandLine(int argc, const char ** argv,
294826
-                 struct CmdlineInfo *cmdlineP) {
294826
+parseCommandLine(int argc, char ** argv,
294826
+                 struct cmdlineInfo *cmdlineP) {
294826
 /*----------------------------------------------------------------------------
294826
    Note that many of the strings that this function returns in the
294826
    *cmdlineP structure are actually in the supplied argv array.  And
294826
@@ -59,7 +57,7 @@ parseCommandLine(int argc, const char **
294826
     opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
294826
     opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
294826
 
294826
-    pm_optParseOptions3(&argc, (char**)argv, opt, sizeof(opt), 0);
294826
+    pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
294826
         /* Uses and sets argc, argv, and all of *cmdlineP. */
294826
 
294826
     if (!alphaoutSpec)
294826
@@ -102,11 +100,6 @@ ReadXimHeader(FILE *     const in_fp,
294826
         pm_message("ReadXimHeader: unable to read file header" );
294826
         return(0);
294826
     }
294826
-    /* Force broken ASCIIZ strings to at least be valid ASCIIZ */
294826
-    a_head.author [sizeof(a_head.author)  - 1] = '\0';
294826
-    a_head.date   [sizeof(a_head.date)    - 1] = '\0';
294826
-    a_head.program[sizeof(a_head.program) - 1] = '\0';
294826
-
294826
     if (atoi(a_head.header_size) != sizeof(ImageHeader)) {
294826
         pm_message("ReadXimHeader: header size mismatch" );
294826
         return(0);
294826
@@ -120,15 +113,39 @@ ReadXimHeader(FILE *     const in_fp,
294826
     header->ncolors = atoi(a_head.num_colors);
294826
     header->nchannels = atoi(a_head.num_channels);
294826
     header->bytes_per_line = atoi(a_head.bytes_per_line);
294826
-#if 0
294826
-    header->npics = atoi(a_head.num_pictures);
294826
-#endif
294826
+/*    header->npics = atoi(a_head.num_pictures);
294826
+*/
294826
     header->bits_channel = atoi(a_head.bits_per_channel);
294826
     header->alpha_flag = atoi(a_head.alpha_channel);
294826
-    header->author = pm_strdup(a_head.author);
294826
-    header->date = pm_strdup(a_head.date);
294826
-    header->program = pm_strdup(a_head.program);
294826
-
294826
+    if (strlen(a_head.author)) {
294826
+        overflow_add(strlen(a_head.author),1);
294826
+        if (!(header->author = calloc((unsigned int)strlen(a_head.author)+1,
294826
+                1))) {
294826
+            pm_message("ReadXimHeader: can't calloc author string" );
294826
+            return(0);
294826
+        }
294826
+    header->width = atoi(a_head.image_width);
294826
+        strncpy(header->author, a_head.author, strlen(a_head.author));
294826
+    }
294826
+    if (strlen(a_head.date)) {
294826
+        overflow_add(strlen(a_head.date),1);
294826
+        if (!(header->date =calloc((unsigned int)strlen(a_head.date)+1,1))){
294826
+            pm_message("ReadXimHeader: can't calloc date string" );
294826
+            return(0);
294826
+        }
294826
+    header->width = atoi(a_head.image_width);
294826
+        strncpy(header->date, a_head.date, strlen(a_head.date));
294826
+    }
294826
+    if (strlen(a_head.program)) {
294826
+        overflow_add(strlen(a_head.program),1);
294826
+        if (!(header->program = calloc(
294826
+                    (unsigned int)strlen(a_head.program) + 1, 1))) {
294826
+            pm_message("ReadXimHeader: can't calloc program string" );
294826
+            return(0);
294826
+        }
294826
+    header->width = atoi(a_head.image_width);
294826
+        strncpy(header->program, a_head.program,strlen(a_head.program));
294826
+    }
294826
     /* Do double checking for backwards compatibility */
294826
     if (header->npics == 0)
294826
         header->npics = 1;
294826
@@ -147,6 +164,7 @@ ReadXimHeader(FILE *     const in_fp,
294826
     if (header->nchannels == 3 && header->bits_channel == 8)
294826
         header->ncolors = 0;
294826
     else if (header->nchannels == 1 && header->bits_channel == 8) {
294826
+        overflow2(header->ncolors, sizeof(Color));
294826
         header->colors = (Color *)calloc((unsigned int)header->ncolors,
294826
                 sizeof(Color));
294826
         if (header->colors == NULL) {
294826
@@ -197,8 +215,7 @@ ReadImageChannel(FILE *         const in
294826
         }
294826
         /* return to the beginning of the next image's buffer */
294826
         if (fseek(infp, marker, 0) == -1) {
294826
-            pm_message("ReadImageChannel: can't fseek to location "
294826
-                       "in image buffer");
294826
+            pm_message("ReadImageChannel: can't fseek to location in image buffer" );
294826
             return(0);
294826
         }
294826
         free((char *)line);
294826
@@ -307,26 +324,28 @@ ReadXimImage(FILE *     const in_fp,
294826
 ***********************************************************************/
294826
 
294826
 static int
294826
-ReadXim(FILE *     const in_fp,
294826
-        XimImage * const xim) {
294826
-
294826
+ReadXim(in_fp, xim)
294826
+    FILE *in_fp;
294826
+    XimImage *xim;
294826
+{
294826
     if (!ReadXimHeader(in_fp, xim)) {
294826
         pm_message("can't read xim header" );
294826
-        return 0;
294826
-    } else if (!ReadXimImage(in_fp, xim)) {
294826
+    return(0);
294826
+    }
294826
+    if (!ReadXimImage(in_fp, xim)) {
294826
         pm_message("can't read xim data" );
294826
-        return 0;
294826
-    } else
294826
-        return 1;
294826
+    return(0);
294826
+    }
294826
+    return(1);
294826
 }
294826
 
294826
 
294826
 
294826
 int
294826
-main(int          argc,
294826
-     const char **argv) {
294826
+main(int argc,
294826
+     char *argv[]) {
294826
 
294826
-    struct CmdlineInfo cmdline;
294826
+    struct cmdlineInfo cmdline;
294826
     FILE *ifP, *imageout_file, *alpha_file;
294826
     XimImage xim;
294826
     pixel *pixelrow, colormap[256];
294826
@@ -338,7 +357,7 @@ main(int          argc,
294826
     pixval maxval;
294826
     bool success;
294826
 
294826
-    pm_proginit(&argc, argv);
294826
+    ppm_init(&argc, argv);
294826
 
294826
     parseCommandLine(argc, argv, &cmdline);
294826
 
294826
diff -ruNp a/editor/pamcut.c b/editor/pamcut.c
294826
--- a/editor/pamcut.c	2021-06-02 15:53:59.809205409 +0200
294826
+++ b/editor/pamcut.c	2021-06-02 20:23:21.828634422 +0200
294826
@@ -799,6 +799,8 @@ cutOneImage(FILE *             const ifP
294826
 
294826
     outpam = inpam;    /* Initial value -- most fields should be same */
294826
     outpam.file   = ofP;
294826
+    overflow_add(rightcol, 1);
294826
+    overflow_add(bottomrow, 1);
294826
     outpam.width  = rightcol - leftcol + 1;
294826
     outpam.height = bottomrow - toprow + 1;
294826
 
294826
diff -ruNp a/editor/pnmgamma.c b/editor/pnmgamma.c
294826
--- a/editor/pnmgamma.c	2021-06-02 15:53:59.805205371 +0200
294826
+++ b/editor/pnmgamma.c	2021-06-02 20:23:21.829634431 +0200
294826
@@ -596,6 +596,7 @@ createGammaTables(enum transferFunction
294826
                   xelval **             const btableP) {
294826
 
294826
     /* Allocate space for the tables. */
294826
+    overflow_add(maxval, 1);
294826
     MALLOCARRAY(*rtableP, maxval+1);
294826
     MALLOCARRAY(*gtableP, maxval+1);
294826
     MALLOCARRAY(*btableP, maxval+1);
294826
diff -ruNp a/editor/pnmhisteq.c b/editor/pnmhisteq.c
294826
--- a/editor/pnmhisteq.c	2021-06-02 15:53:59.800205323 +0200
294826
+++ b/editor/pnmhisteq.c	2021-06-02 20:23:21.829634431 +0200
294826
@@ -107,6 +107,7 @@ computeLuminosityHistogram(xel * const *
294826
     unsigned int pixelCount;
294826
     unsigned int * lumahist;
294826
 
294826
+    overflow_add(maxval, 1);
294826
     MALLOCARRAY(lumahist, maxval + 1);
294826
     if (lumahist == NULL)
294826
         pm_error("Out of storage allocating array for %u histogram elements",
294826
diff -ruNp a/editor/pnmindex.csh b/editor/pnmindex.csh
294826
--- a/editor/pnmindex.csh	2021-06-02 15:53:59.801205332 +0200
294826
+++ b/editor/pnmindex.csh	2021-06-02 20:23:21.829634431 +0200
294826
@@ -1,5 +1,7 @@
294826
 #!/bin/csh -f
294826
 #
294826
+echo "Unsafe code, needs debugging, do not ship"
294826
+exit 1
294826
 # pnmindex - build a visual index of a bunch of anymaps
294826
 #
294826
 # Copyright (C) 1991 by Jef Poskanzer.
294826
diff -ruNp a/editor/pnmpad.c b/editor/pnmpad.c
294826
--- a/editor/pnmpad.c	2021-06-02 15:53:59.808205400 +0200
294826
+++ b/editor/pnmpad.c	2021-06-02 20:23:21.829634431 +0200
294826
@@ -654,6 +654,8 @@ main(int argc, const char ** argv) {
294826
 
294826
     computePadSizes(cmdline, cols, rows, &lpad, &rpad, &tpad, &bpad);
294826
 
294826
+    overflow_add(cols, lpad);
294826
+    overflow_add(cols + lpad, rpad);
294826
     newcols = cols + lpad + rpad;
294826
 
294826
     if (cmdline.reportonly)
294826
diff -ruNp a/editor/pnmremap.c b/editor/pnmremap.c
294826
--- a/editor/pnmremap.c	2021-06-02 15:53:59.809205409 +0200
294826
+++ b/editor/pnmremap.c	2021-06-02 20:23:21.830634440 +0200
294826
@@ -470,6 +470,7 @@ fserr_init(struct pam *   const pamP,
294826
 
294826
     unsigned int const fserrSize = pamP->width + 2;
294826
 
294826
+    overflow_add(pamP->width, 2);
294826
     fserrP->width = pamP->width;
294826
 
294826
     MALLOCARRAY(fserrP->thiserr, pamP->depth);
294826
@@ -508,6 +509,7 @@ floydInitRow(struct pam *   const pamP,
294826
 
294826
     unsigned int col;
294826
 
294826
+    overflow_add(pamP->width, 2);
294826
     for (col = 0; col < pamP->width + 2; ++col) {
294826
         unsigned int plane;
294826
         for (plane = 0; plane < pamP->depth; ++plane)
294826
diff -ruNp a/editor/pnmscalefixed.c b/editor/pnmscalefixed.c
294826
--- a/editor/pnmscalefixed.c	2021-06-02 15:53:59.800205323 +0200
294826
+++ b/editor/pnmscalefixed.c	2021-06-02 20:23:21.830634440 +0200
294826
@@ -214,6 +214,7 @@ compute_output_dimensions(const struct c
294826
                           const int rows, const int cols,
294826
                           int * newrowsP, int * newcolsP) {
294826
 
294826
+    overflow2(rows, cols);
294826
     if (cmdline.pixels) {
294826
         if (rows * cols <= cmdline.pixels) {
294826
             *newrowsP = rows;
294826
@@ -265,6 +266,8 @@ compute_output_dimensions(const struct c
294826
 
294826
     if (*newcolsP < 1) *newcolsP = 1;
294826
     if (*newrowsP < 1) *newrowsP = 1;
294826
+
294826
+    overflow2(*newcolsP, *newrowsP);
294826
 }        
294826
 
294826
 
294826
@@ -446,6 +449,9 @@ main(int argc, char **argv ) {
294826
        unfilled.  We can address that by stretching, whereas the other
294826
        case would require throwing away some of the input.
294826
     */
294826
+
294826
+    overflow2(newcols, SCALE);
294826
+    overflow2(newrows, SCALE);
294826
     sxscale = SCALE * newcols / cols;
294826
     syscale = SCALE * newrows / rows;
294826
 
294826
diff -ruNp a/editor/ppmdither.c b/editor/ppmdither.c
294826
--- a/editor/ppmdither.c	2021-06-02 15:53:59.803205352 +0200
294826
+++ b/editor/ppmdither.c	2021-06-02 20:23:21.831634448 +0200
294826
@@ -356,6 +356,11 @@ dithMatrix(unsigned int const dithPower)
294826
             (dithDim * sizeof(*dithMat)) + /* pointers */
294826
             (dithDim * dithDim * sizeof(**dithMat)); /* data */
294826
         
294826
+
294826
+        overflow2(dithDim, sizeof(*dithMat));
294826
+        overflow3(dithDim, dithDim, sizeof(**dithMat));
294826
+        overflow_add(dithDim * sizeof(*dithMat), dithDim * dithDim * sizeof(**dithMat));
294826
+
294826
         dithMat = malloc(dithMatSize);
294826
         
294826
         if (dithMat == NULL) 
294826
diff -ruNp a/editor/specialty/pamoil.c b/editor/specialty/pamoil.c
294826
--- a/editor/specialty/pamoil.c	2021-06-02 15:53:59.817205486 +0200
294826
+++ b/editor/specialty/pamoil.c	2021-06-02 20:23:21.831634448 +0200
294826
@@ -112,6 +112,7 @@ main(int argc, char *argv[] ) {
294826
     tuples = pnm_readpam(ifp, &inpam, PAM_STRUCT_SIZE(tuple_type));
294826
     pm_close(ifp);
294826
 
294826
+    overflow_add(inpam.maxval, 1);
294826
     MALLOCARRAY(hist, inpam.maxval + 1);
294826
     if (hist == NULL)
294826
         pm_error("Unable to allocate memory for histogram.");
294826
diff -ruNp a/lib/libpam.c b/lib/libpam.c
294826
--- a/lib/libpam.c	2021-06-02 15:53:59.867205967 +0200
294826
+++ b/lib/libpam.c	2021-06-02 20:23:21.832634457 +0200
294826
@@ -225,7 +225,8 @@ allocPamRow(const struct pam * const pam
294826
     unsigned int const bytesPerTuple = allocationDepth(pamP) * sizeof(sample);
294826
     tuple * tuplerow;
294826
 
294826
-    tuplerow = malloc(pamP->width * (sizeof(tuple *) + bytesPerTuple));
294826
+    overflow_add(sizeof(tuple *), bytesPerTuple);
294826
+    tuplerow = malloc2(pamP->width, (sizeof(tuple *) + bytesPerTuple));
294826
 
294826
     if (tuplerow != NULL) {
294826
         /* Now we initialize the pointers to the individual tuples
294826
diff -ruNp a/lib/libpammap.c b/lib/libpammap.c
294826
--- a/lib/libpammap.c	2021-06-02 15:53:59.867205967 +0200
294826
+++ b/lib/libpammap.c	2021-06-02 20:23:00.658449503 +0200
294826
@@ -108,6 +108,8 @@ allocTupleIntListItem(struct pam * const
294826
     */
294826
     struct tupleint_list_item * retval;
294826
 
294826
+    overflow2(pamP->depth, sizeof(sample));
294826
+    overflow_add(sizeof(*retval)-sizeof(retval->tupleint.tuple), pamP->depth*sizeof(sample));
294826
     unsigned int const size =
294826
         sizeof(*retval) - sizeof(retval->tupleint.tuple)
294826
         + pamP->depth * sizeof(sample);
294826
diff -ruNp a/lib/libpm.c b/lib/libpm.c
294826
--- a/lib/libpm.c	2021-06-02 15:53:59.867205967 +0200
294826
+++ b/lib/libpm.c	2021-06-02 20:23:21.832634457 +0200
294826
@@ -888,5 +888,53 @@ pm_parse_height(const char * const arg)
294826
     return height;
294826
 }
294826
 
294826
+/*
294826
+ *	Maths wrapping
294826
+ */
294826
 
294826
+void __overflow2(int a, int b)
294826
+{
294826
+        if(a < 0 || b < 0)
294826
+                pm_error("object too large");
294826
+        if(b == 0)
294826
+                return;
294826
+        if(a > INT_MAX / b)
294826
+                pm_error("object too large");
294826
+}
294826
+
294826
+void overflow3(int a, int b, int c)
294826
+{
294826
+        overflow2(a,b);
294826
+        overflow2(a*b, c);
294826
+}
294826
+
294826
+void overflow_add(int a, int b)
294826
+{
294826
+        if( a > INT_MAX - b)
294826
+                pm_error("object too large");
294826
+}
294826
+
294826
+void *malloc2(int a, int b)
294826
+{
294826
+        overflow2(a, b);
294826
+        if(a*b == 0)
294826
+                pm_error("Zero byte allocation");
294826
+        return malloc(a*b);
294826
+}
294826
+
294826
+void *malloc3(int a, int b, int c)
294826
+{
294826
+        overflow3(a, b, c);
294826
+        if(a*b*c == 0)
294826
+                pm_error("Zero byte allocation");
294826
+        return malloc(a*b*c);
294826
+}
294826
+
294826
+void *realloc2(void * a, int b, int c)
294826
+{
294826
+        overflow2(b, c);
294826
+        if(b*c == 0)
294826
+                pm_error("Zero byte allocation");
294826
+        return realloc(a, b*c);
294826
+}
294826
 
294826
diff -ruNp a/lib/pm.h b/lib/pm.h
294826
--- a/lib/pm.h	2021-06-02 15:53:59.866205957 +0200
294826
+++ b/lib/pm.h	2021-06-02 20:23:21.833634466 +0200
294826
@@ -442,5 +442,12 @@ pm_parse_height(const char * const arg);
294826
 }
294826
 #endif
294826
 
294826
+void *malloc2(int, int);
294826
+void *malloc3(int, int, int);
294826
+#define overflow2(a,b) __overflow2(a,b)
294826
+void __overflow2(int, int);
294826
+void overflow3(int, int, int);
294826
+void overflow_add(int, int);
294826
+
294826
 
294826
 #endif
294826
diff -ruNp a/other/pnmcolormap.c b/other/pnmcolormap.c
294826
--- a/other/pnmcolormap.c	2021-06-02 15:53:59.882206111 +0200
294826
+++ b/other/pnmcolormap.c	2021-06-02 20:23:21.833634466 +0200
294826
@@ -1002,6 +1002,7 @@ colormapToSquare(struct pam * const pamP
294826
             pamP->width = intsqrt;
294826
         else
294826
             pamP->width = intsqrt + 1;
294826
+            overflow_add(intsqrt, 1);
294826
     }
294826
     {
294826
         unsigned int const intQuotient = colormap.size / pamP->width;
294826
diff -ruNp a/urt/rle_addhist.c b/urt/rle_addhist.c
294826
--- a/urt/rle_addhist.c	2021-06-02 15:53:59.825205563 +0200
294826
+++ b/urt/rle_addhist.c	2021-06-02 20:23:21.834634475 +0200
294826
@@ -70,13 +70,18 @@ rle_addhist(char *          argv[],
294826
         return;
294826
     
294826
     length = 0;
294826
-    for (i = 0; argv[i]; ++i)
294826
+    for (i = 0; argv[i]; ++i) {
294826
+        overflow_add(length, strlen(argv[i]));
294826
+        overflow_add(length+1, strlen(argv[i]));
294826
         length += strlen(argv[i]) +1;   /* length of each arg plus space. */
294826
+    }
294826
 
294826
     time(&temp);
294826
     timedate = ctime(&temp);
294826
     length += strlen(timedate);        /* length of date and time in ASCII. */
294826
-
294826
+    overflow_add(strlen(padding), 4);
294826
+    overflow_add(strlen(histoire), strlen(padding) + 4);
294826
+    overflow_add(length, strlen(histoire) + strlen(padding) + 4);
294826
     length += strlen(padding) + 3 + strlen(histoire) + 1;
294826
         /* length of padding, "on "  and length of history name plus "="*/
294826
     if (in_hdr) /* if we are interested in the old comments... */
294826
@@ -84,8 +89,10 @@ rle_addhist(char *          argv[],
294826
     else
294826
         old = NULL;
294826
     
294826
-    if (old && *old)
294826
+    if (old && *old) {
294826
+        overflow_add(length, strlen(old));
294826
         length += strlen(old);       /* add length if there. */
294826
+    }
294826
 
294826
     ++length;                               /*Cater for the null. */
294826
 
294826
diff -ruNp a/urt/rle_getrow.c b/urt/rle_getrow.c
294826
--- a/urt/rle_getrow.c	2021-06-02 15:53:59.825205563 +0200
294826
+++ b/urt/rle_getrow.c	2021-06-02 20:23:21.834634475 +0200
294826
@@ -164,6 +164,7 @@ rle_get_setup(rle_hdr * const the_hdr) {
294826
         char * cp;
294826
 
294826
         VAXSHORT(comlen, infile); /* get comment length */
294826
+        overflow_add(comlen, 1);
294826
         evenlen = (comlen + 1) & ~1;    /* make it even */
294826
         if (evenlen) {
294826
             MALLOCARRAY(comment_buf, evenlen);
294826
diff -ruNp a/urt/rle.h b/urt/rle.h
294826
--- a/urt/rle.h	2021-06-02 15:53:59.825205563 +0200
294826
+++ b/urt/rle.h	2021-06-02 20:23:21.834634475 +0200
294826
@@ -153,6 +153,17 @@ rle_hdr             /* End of typedef. *
294826
  */
294826
 extern rle_hdr rle_dflt_hdr;
294826
 
294826
+/*
294826
+ * Provided by pm library
294826
+ */
294826
+
294826
+extern void overflow_add(int, int);
294826
+#define overflow2(a,b) __overflow2(a,b)
294826
+extern void __overflow2(int, int);
294826
+extern void overflow3(int, int, int);
294826
+extern void *malloc2(int, int);
294826
+extern void *malloc3(int, int, int);
294826
+extern void *realloc2(void *, int, int);
294826
 
294826
 /* Declare RLE library routines. */
294826
 
294826
diff -ruNp a/urt/rle_hdr.c b/urt/rle_hdr.c
294826
--- a/urt/rle_hdr.c	2021-06-02 15:53:59.825205563 +0200
294826
+++ b/urt/rle_hdr.c	2021-06-02 20:23:21.835634483 +0200
294826
@@ -148,7 +148,7 @@ rle_hdr_cp(rle_hdr * const fromHdrP,
294826
     if (toHdrP->cmap) {
294826
         size_t const size =
294826
             toHdrP->ncmap * (1 << toHdrP->cmaplen) * sizeof(rle_map);
294826
-        toHdrP->cmap = malloc(size);
294826
+        toHdrP->cmap = malloc3(toHdrP->ncmap, 1<<toHdrP->cmaplen, sizeof(rle_map));
294826
         if (!toHdrP->cmap)
294826
             pm_error("Failed to allocate memory for %u color maps "
294826
                      "of length %u", toHdrP->ncmap, 1 << toHdrP->cmaplen);
294826
@@ -164,11 +164,16 @@ rle_hdr_cp(rle_hdr * const fromHdrP,
294826
 
294826
         /* Count the comments. */
294826
         for (cp = toHdrP->comments, size = 0; *cp; ++cp)
294826
+	{
294826
+            overflow_add(size,1);
294826
             ++size;
294826
+        }
294826
 
294826
         /* Check if there are really any comments. */
294826
         if (size > 0) {
294826
+            overflow_add(size,1);
294826
             ++size;     /* Copy the NULL pointer, too. */
294826
+            overflow2(size, sizeof(char *));
294826
             size *= sizeof(char *);
294826
             toHdrP->comments = malloc(size);
294826
             if (!toHdrP->comments)
294826
diff -ruNp a/urt/rle_putcom.c b/urt/rle_putcom.c
294826
--- a/urt/rle_putcom.c	2021-06-02 15:53:59.824205554 +0200
294826
+++ b/urt/rle_putcom.c	2021-06-02 20:25:22.620707940 +0200
294826
@@ -98,12 +98,14 @@ rle_putcom(const char * const value,
294826
         const char * v;
294826
         const char ** old_comments;
294826
         int i;
294826
-        for (i = 2, cp = the_hdr->comments; *cp != NULL; ++i, ++cp)
294826
+        for (i = 2, cp = the_hdr->comments; *cp != NULL; ++i, ++cp) {
294826
+            overflow_add(i, 1);
294826
             if (match(value, *cp) != NULL) {
294826
                 v = *cp;
294826
                 *cp = value;
294826
                 return v;
294826
             }
294826
+        }
294826
         /* Not found */
294826
         /* Can't realloc because somebody else might be pointing to this
294826
          * comments block.  Of course, if this were true, then the
294826
diff -ruNp a/urt/Runput.c b/urt/Runput.c
294826
--- a/urt/Runput.c	2021-06-02 15:53:59.825205563 +0200
294826
+++ b/urt/Runput.c	2021-06-02 20:23:21.834634475 +0200
294826
@@ -202,10 +202,11 @@ RunSetup(rle_hdr * the_hdr)
294826
     if ( the_hdr->background != 0 )
294826
     {
294826
 	register int i;
294826
-	register rle_pixel *background =
294826
-	    (rle_pixel *)malloc( (unsigned)(the_hdr->ncolors + 1) );
294826
-	register int *bg_color;
294826
-	/* 
294826
+        register rle_pixel *background;
294826
+        register int *bg_color;
294826
+
294826
+        overflow_add(the_hdr->ncolors,1);
294826
+        background = (rle_pixel *)malloc( (unsigned)(the_hdr->ncolors + 1) );	/*
294826
 	 * If even number of bg color bytes, put out one more to get to 
294826
 	 * 16 bit boundary.
294826
 	 */
294826
@@ -224,7 +225,7 @@ RunSetup(rle_hdr * the_hdr)
294826
 	/* Big-endian machines are harder */
294826
 	register int i, nmap = (1 << the_hdr->cmaplen) *
294826
 			       the_hdr->ncmap;
294826
-	register char *h_cmap = (char *)malloc( nmap * 2 );
294826
+        register char *h_cmap = (char *)malloc2( nmap, 2 );
294826
 	if ( h_cmap == NULL )
294826
 	{
294826
 	    fprintf( stderr,
294826
diff -ruNp a/urt/scanargs.c b/urt/scanargs.c
294826
--- a/urt/scanargs.c	2021-06-02 15:53:59.825205563 +0200
294826
+++ b/urt/scanargs.c	2021-06-02 20:56:36.681068294 +0200
294826
@@ -62,8 +62,8 @@ typedef int *ptr;
294826
 /*
294826
  * Storage allocation macros
294826
  */
294826
-#define NEW( type, cnt )        (type *) malloc( (cnt) * sizeof( type ) )
294826
-#define RENEW( type, ptr, cnt ) (type *) realloc( ptr, (cnt) * sizeof( type ) )
294826
+#define NEW( type, cnt )        (type *) malloc2( (cnt), sizeof( type ) )
294826
+#define RENEW( type, ptr, cnt ) (type *) realloc2( ptr, (cnt), sizeof( type ) )
294826
 
294826
 static CONST_DECL char * prformat( CONST_DECL char *, int );
294826
 static int isnum( CONST_DECL char *, int, int );
294826
@@ -114,7 +114,7 @@ va_list argl;
294826
 
294826
     bool    required;
294826
     int     excnt;                      /* which flag is set */
294826
-    unsigned int exflag;                /* How many of a set of exclusive
294826
+    bool    exflag;                     /* when set, one of a set of exclusive
294826
                                            flags is set */
294826
 
294826
     bool    list_of;                    /* set if parsing off a list of args */
294826
@@ -258,7 +258,7 @@ reswitch:
294826
                     /* go back to label */
294826
                         ncp = cp-1;     /* remember */
294826
                         cp -= 3;
294826
-                        for (excnt = exflag = 0
294826
+                        for (excnt = 0, exflag = NO 
294826
                                 ; *cp != ' ' && !(*cp=='-' &&(cp[-1]=='!'||cp[-1]=='%'));
294826
                                 (--cp, excnt++))
294826
                         {
294826
@@ -273,7 +273,7 @@ reswitch:
294826
                                             ERROR ("extra flags ignored");
294826
                                         if (exflag)
294826
                                             ERROR ("more than one exclusive flag chosen");
294826
-                                        exflag++;
294826
+                                        exflag = YES;
294826
                                         required = NO;
294826
                                         check += cnt;
294826
                                         arg_used[cnt] = 1;