File: 10_netpbm-security-code.patch

package info (click to toggle)
netpbm-free 2%3A11.12.00-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,728 kB
  • sloc: ansic: 205,127; sh: 11,414; perl: 3,246; makefile: 1,609; python: 292; lex: 269; csh: 243; pascal: 13
file content (853 lines) | stat: -rw-r--r-- 27,500 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
Author: Laurent Bigonville
        Updated by Andreas Tille
Last-Update: 2023-12-30
Description: Add security code
Forwarded: 2023-08-17 mail to maintainer
Origin: vendor, https://src.fedoraproject.org/rpms/netpbm.git

--- a/converter/other/jpegtopnm.c
+++ b/converter/other/jpegtopnm.c
@@ -892,10 +892,12 @@ convertImage(FILE *
         tellDetails(*cinfoP, maxval, format);
 
     /* Calculate output image dimensions so we can allocate space */
     jpeg_calc_output_dimensions(cinfoP);
 
+    overflow2(cinfoP->output_width, cinfoP->output_components);
+
     /* Start decompressor */
     jpeg_start_decompress(cinfoP);
 
     if (ofP)
         /* Write pnm output header */
--- a/converter/other/pbmtopgm.c
+++ b/converter/other/pbmtopgm.c
@@ -86,10 +86,11 @@ main(int argc, const char ** argv) {
 
     left = cmdline.convCols / 2;  right = cmdline.convCols - left;
     up   = cmdline.convRows / 2;  down  = cmdline.convRows - up;
 
     outrow = pgm_allocrow(cols) ;
+    overflow2(cmdline.convCols, cmdline.convRows);
     maxval = MIN(PGM_OVERALLMAXVAL, cmdline.convCols * cmdline.convRows);
     pgm_writepgminit(stdout, cols, rows, maxval, 0) ;
 
     for (row = 0; row < rows; ++row) {
         unsigned int const t = (row > up) ? (row - up) : 0;
--- a/converter/other/pnmtoddif.c
+++ b/converter/other/pnmtoddif.c
@@ -625,10 +625,11 @@ main(int argc, char *argv[]) {
     ip.v_res = ver_resolution;
 
     switch (PNM_FORMAT_TYPE(format)) {
     case PBM_TYPE:
         ip.bits_per_pixel = 1;
+        overflow_add(cols, 7);
         ip.bytes_per_line = (cols + 7) / 8;
         ip.spectral = 2;
         ip.components = 1;
         ip.bits_per_component = 1;
         ip.polarity = 1;
@@ -640,10 +641,11 @@ main(int argc, char *argv[]) {
         ip.components = 1;
         ip.bits_per_component = 8;
         ip.polarity = 2;
         break;
     case PPM_TYPE:
+        overflow2(cols, 3);
         ip.bytes_per_line = 3 * cols;
         ip.bits_per_pixel = 24;
         ip.spectral = 5;
         ip.components = 3;
         ip.bits_per_component = 8;
--- a/converter/other/rletopnm.c
+++ b/converter/other/rletopnm.c
@@ -17,10 +17,12 @@
  * source is available for no extra charge.
  *
  * If you modify this software, you should include a notice giving the
  * name of the person performing the modification, the date of modification,
  * and the reason for such modification.
+ *
+ *  2002-12-19: Fix maths wrapping bugs. Alan Cox <alan@redhat.com>
  */
 /*
  * rletopnm - A conversion program to convert from Utah's "rle" image format
  *            to pbmplus ppm or pgm image formats.
  *
--- a/converter/other/xwdtopnm.c
+++ b/converter/other/xwdtopnm.c
@@ -220,10 +220,14 @@ processX10Header(X10WDFileHeader *  cons
         *visualclassP = StaticGray;
         *maxvalP = 1;
         *colorsP = pnm_allocrow(2);
         PNM_ASSIGN1((*colorsP)[0], 0);
         PNM_ASSIGN1((*colorsP)[1], *maxvalP);
+        overflow_add(h10P->pixmap_width, 15);
+        if(h10P->pixmap_width < 0)
+            pm_error("assert: negative width");
+        overflow2((((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width), 8);
         *padrightP =
             (((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width) * 8;
         *bitsPerItemP = 16;
         *bitsPerPixelP = 1;
     } else if (h10P->window_ncolors == 0) {
--- a/converter/pbm/mdatopbm.c
+++ b/converter/pbm/mdatopbm.c
@@ -281,14 +281,17 @@ main(int argc, const char **argv) {
         short yy;
         pm_readlittleshort(ifP, &yy); nInRows = yy;
         pm_readlittleshort(ifP, &yy); nInCols = yy;
     }
 
+    overflow2(nInCols, 8);
     nOutCols = 8 * nInCols;
     nOutRows = nInRows;
-    if (bScale)
+    if (bScale) {
+	overflow2(nOutRows, 2);
         nOutRows *= 2;
+    }
 
     data = pbm_allocarray(nOutCols, nOutRows);
 
     MALLOCARRAY_NOFAIL(mdrow, nInCols);
 
--- a/converter/pbm/pbmtogem.c
+++ b/converter/pbm/pbmtogem.c
@@ -77,10 +77,11 @@ putinit (int const rows, int const cols)
     pm_error ("write error");
   item = 0;
   bitsperitem = 0;
   bitshift = 7;
   outcol = 0;
+  overflow_add(cols, 7);
   outmax = (cols + 7) / 8;
   outrow = (unsigned char *) pm_allocrow (outmax, sizeof (unsigned char));
   lastrow = (unsigned char *) pm_allocrow (outmax, sizeof (unsigned char));
   linerepeat = -1;
 }
--- a/converter/pbm/pbmtogo.c
+++ b/converter/pbm/pbmtogo.c
@@ -156,10 +156,11 @@ main(int           argc,
                  "(%u pixels)", cols, GRAPHON_WIDTH);
 
     bitrow = pbm_allocrow(cols);
 
     /* Round cols up to the nearest multiple of 8. */
+    overflow_add(cols, 7);
     rucols = ( cols + 7 ) / 8;
     bytesperrow = rucols;       /* GraphOn uses bytes */
     rucols = rucols * 8;
     padright = rucols - cols;
 
--- a/converter/pbm/pbmtolj.c
+++ b/converter/pbm/pbmtolj.c
@@ -118,11 +118,15 @@ parseCommandLine(int argc, const char **
 
 
 static void
 allocateBuffers(unsigned int const cols) {
 
+    overflow_add(cols, 8);
     rowBufferSize = (cols + 7) / 8;
+    overflow_add(rowBufferSize, 128);
+    overflow_add(rowBufferSize, rowBufferSize+128);
+    overflow_add(rowBufferSize+10, rowBufferSize/8);
     packBufferSize = rowBufferSize + (rowBufferSize + 127) / 128 + 1;
     deltaBufferSize = rowBufferSize + rowBufferSize / 8 + 10;
 
     MALLOCARRAY_NOFAIL(prevRowBuffer, rowBufferSize);
     MALLOCARRAY_NOFAIL(rowBuffer, rowBufferSize);
--- a/converter/pbm/pbmtomda.c
+++ b/converter/pbm/pbmtomda.c
@@ -202,10 +202,11 @@ main(int argc, const char ** argv) {
 
     bits = pbm_readpbm(ifP, &nInCols, &nInRows);
 
     nOutRowsUnrounded = cmdline.dscale ? nInRows/2 : nInRows;
 
+    overflow_add(nOutRowsUnrounded, 3);
     nOutRows = ((nOutRowsUnrounded + 3) / 4) * 4;
         /* MDA wants rows a multiple of 4 */
     nOutCols = nInCols / 8;
 
     rc = fwrite(header, 1, 128, stdout);
--- a/converter/pbm/pbmtoppa/pbmtoppa.c
+++ b/converter/pbm/pbmtoppa/pbmtoppa.c
@@ -454,10 +454,11 @@ main(int argc, char *argv[]) {
         }
         else
             pm_error("main(): unrecognized parameter '%s'", argv[argn]);
     }
 
+    overflow_add(Width, 7);
     Pwidth = (Width+7)/8;
     printer.fptr = ofP;
 
     return printPbm(ifP);
 }
--- a/converter/pbm/thinkjettopbm.l
+++ b/converter/pbm/thinkjettopbm.l
@@ -230,10 +230,12 @@ yywrap (void)
         pm_error("A row has an uncomputably large number of columns: %d",
                  maxRowLength);
     /*
      * Quite simple since ThinkJet bit arrangement matches PBM
      */
+
+    overflow2(maxRowLength, 8);
     pbm_writepbminit(stdout, maxRowLength*8, rowCount, 0);
 
     packed_bitrow = malloc(maxRowLength);
     if (packed_bitrow == NULL) pm_error("Out of memory");
 
--- a/converter/pbm/ybmtopbm.c
+++ b/converter/pbm/ybmtopbm.c
@@ -49,10 +49,11 @@ getinit(FILE *         const ifP,
         pm_error("invalid height value in YBM file");
 
     *colsP = (unsigned int) cols;
     *rowsP = (unsigned int) rows;
     *depthP = 1;
+    overflow_add(*colsP, 15);
 }
 
 
 
 int
--- a/converter/pgm/lispmtopgm.c
+++ b/converter/pgm/lispmtopgm.c
@@ -165,10 +165,11 @@ main(int argc, const char ** argv) {
 
     maxval = (1 << depth);
 
     pgm_writepgminit(stdout, cols, rows, maxval, 0);
 
+    overflow_add(cols, 7);
     grayrow = pgm_allocrow(ROUNDUP(cols, 8));
 
     for (row = 0; row < rows; ++row) {
         unsigned int col;
 
--- a/converter/pgm/psidtopgm.c
+++ b/converter/pgm/psidtopgm.c
@@ -79,10 +79,11 @@ main(int           argc,
     if (maxval > PGM_OVERALLMAXVAL)
         pm_error("bits/sample (%d) is too large.", bitspersample);
 
     pgm_writepgminit(stdout, cols, rows, maxval, 0);
 
+    overflow_add(cols, 7);
     grayrow = pgm_allocrow(ROUNDUP(cols, 8));
 
     for (row = 0; row < rows; ++row) {
         unsigned int col;
         for (col = 0; col < cols; ) {
--- a/converter/ppm/Makefile
+++ b/converter/ppm/Makefile
@@ -9,11 +9,11 @@ include $(BUILDDIR)/config.mk
 
 SUBDIRS = hpcdtoppm ppmtompeg
 
 PORTBINARIES =	411toppm eyuvtoppm gouldtoppm ilbmtoppm imgtoppm \
 		leaftoppm mtvtoppm neotoppm \
-		pcxtoppm pc1toppm pi1toppm picttoppm pjtoppm \
+		pcxtoppm pc1toppm pi1toppm pjtoppm \
 		ppmtoacad ppmtoapplevol ppmtoarbtxt ppmtoascii \
 		ppmtobmp ppmtoeyuv ppmtogif ppmtoicr ppmtoilbm \
 		ppmtoleaf ppmtolj ppmtomitsu ppmtoneo \
 		ppmtopcx ppmtopi1 ppmtopict ppmtopj \
 		ppmtopjxl ppmtoppm ppmtopuzz ppmtorgb3 \
--- a/converter/ppm/ilbmtoppm.c
+++ b/converter/ppm/ilbmtoppm.c
@@ -615,10 +615,11 @@ decodeRow(FILE *          const ifP,
     int bytes;
     unsigned char * ilP;
     rawtype * chP;
 
     cols  = bmhdP->w;
+    overflow_add(cols, 15);
     bytes = ilbm_rowByteCt(cols);
 
     for (plane = 0; plane < nPlanes; ++plane) {
         int mask;
         int cbit;
@@ -709,10 +710,27 @@ decodeMask(FILE *          const ifP,
 
 /****************************************************************************
  Multipalette handling
  ****************************************************************************/
 
+static void *
+xmalloc2(x, y)
+    int x;
+    int y;
+{
+    void *mem;
+
+    overflow2(x,y);
+    if( x * y == 0 )
+        return NULL;
+
+    mem = malloc2(x,y);
+    if( mem == NULL )
+        pm_error("out of memory allocating %d bytes", x * y);
+    return mem;
+}
+
 
 static void
 multi_adjust(ColorMap *            const cmapP,
              unsigned int          const row,
              const PaletteChange * const palchange) {
@@ -1383,10 +1401,13 @@ dcol_to_ppm(FILE *         const ifP,
         pm_error("too many planes");
 
     if (redmaxval != maxval || greenmaxval != maxval || bluemaxval != maxval)
         pm_message("scaling colors to %u bits", pm_maxvaltobits(maxval));
 
+    overflow_add(redmaxval, 1);
+    overflow_add(greenmaxval, 1);
+    overflow_add(bluemaxval, 1);
     MALLOCARRAY_NOFAIL(redtable,   redmaxval   + 1);
     MALLOCARRAY_NOFAIL(greentable, greenmaxval + 1);
     MALLOCARRAY_NOFAIL(bluetable,  bluemaxval  + 1);
 
     {
--- a/converter/ppm/imgtoppm.c
+++ b/converter/ppm/imgtoppm.c
@@ -83,10 +83,11 @@ main(int argc, const char ** argv) {
                 pm_error("bad colormap header");
             buf[8] = '\0';
             len = atoi((char*) buf);
             if (fread(buf, len, 1, ifP) != 1)
                 pm_error("bad colormap buf");
+	    overflow2(cmaplen, 3);
             if (len != cmaplen * 3) {
                 pm_message(
                     "cmaplen (%u) and colormap buf length (%u) do not match",
                     cmaplen, len);
                 if (len > cmaplen * 3)
@@ -103,10 +104,11 @@ main(int argc, const char ** argv) {
 
             if (fread(buf, 8, 1, ifP) != 1)
                 pm_error("bad pixel data header");
             buf[8] = '\0';
             len = atoi((char*) buf);
+	    overflow2(cols, rows);
             if (len != cols * rows)
                 pm_message(
                     "pixel data length (%u) does not match image size (%u)",
                     len, cols * rows);
 
--- a/converter/ppm/picttoppm.c
+++ b/converter/ppm/picttoppm.c
@@ -1,5 +1,6 @@
+#error "Unfixable. Don't ship me"
 /*
  * picttoppm.c -- convert a MacIntosh PICT file to PPM format.
  *
  * Copyright 1989,1992,1993 George Phillips
  *
--- a/converter/ppm/pjtoppm.c
+++ b/converter/ppm/pjtoppm.c
@@ -73,12 +73,14 @@ modifyImageMode1(unsigned int     const
                      col < imlen[row * planes + plane];
                      col += 2) {
                     int cmd, val;
                     for (cmd = image[row * planes + plane][col],
                              val = image[plane + row * planes][col+1];
-                         cmd >= 0 && i < newcols; --cmd, ++i)
-                        buf[i] = val;
+                         cmd >= 0 && i < newcols; --cmd, ++i) {
+			    overflow_add(i, 1);
+                            buf[i] = val;
+		    }
                 }
                 cols = MAX(cols, i);
                 free(image[row * planes + plane]);
 
                 /*
@@ -263,12 +265,14 @@ main(int argc, const char ** argv) {
                     modeIsSet = true;
                     break;
                 case 'V':   /* send plane */
                 case 'W':   /* send last plane */
                     if (row >= rowsX || image == NULL) {
-                        if (row >= rowsX)
+                        if (row >= rowsX) {
+			    overflow_add(rowsX, 100);
                             rowsX += 100;
+			}
                         REALLOCARRAY(image, uintProduct(rowsX, planes));
                         REALLOCARRAY(imlen, uintProduct(rowsX, planes));
                     }
                     if (image == NULL || imlen == NULL)
                         pm_error("out of memory");
--- a/converter/ppm/ppmtoeyuv.c
+++ b/converter/ppm/ppmtoeyuv.c
@@ -115,10 +115,11 @@ chrominanceBlue(pixel const p) {
 
 
 static void
 createMultiplicationTables(pixval const maxval) {
 
+    overflow_add(maxval, 1);
     MALLOCARRAY_NOFAIL(mult299   , maxval+1);
     MALLOCARRAY_NOFAIL(mult587   , maxval+1);
     MALLOCARRAY_NOFAIL(mult114   , maxval+1);
     MALLOCARRAY_NOFAIL(mult16874 , maxval+1);
     MALLOCARRAY_NOFAIL(mult33126 , maxval+1);
--- a/converter/ppm/ppmtomitsu.c
+++ b/converter/ppm/ppmtomitsu.c
@@ -66,10 +66,12 @@ mediaSize(const char * const media,
         }
     } else
         medias = MSize_User;
 
     if (dpi300) {
+        overflow2(medias.maxcols, 2);
+        overflow2(medias.maxrows, 2);
         medias.maxcols *= 2;
         medias.maxrows *= 2;
     }
 
     return medias;
--- a/converter/ppm/ppmtopjxl.c
+++ b/converter/ppm/ppmtopjxl.c
@@ -536,10 +536,13 @@ main(int argc, const char * argv[]) {
     if (maxval > PCL_MAXVAL)
         pm_error("color range too large; reduce with ppmcscale");
 
     computeColormap(pixels, cols, rows, MAXCOLORS, &chv, &cht, &colorCt);
 
+    if (cols < 0 || rows < 0)
+        pm_error("negative size is not possible");
+
     computeColorDownloadingMode(
         colorCt, cols, maxval,
         &bytesPerRow, &colorMapped,
         &bitsPerPixelRed, &bitsPerPixelGrn, &bitsPerPixelBlu, &bitsPerIndex);
 
--- a/converter/ppm/ppmtowinicon.c
+++ b/converter/ppm/ppmtowinicon.c
@@ -205,10 +205,11 @@ newAndBitmap(gray **      const ba,
     MALLOCVAR_NOFAIL(icBitmapP);
 
     MALLOCARRAY_NOFAIL(rowData, rows);
     icBitmapP->xBytes = xByteCt;
     icBitmapP->data   = rowData;
+    overflow2(xByteCt,rows);
     icBitmapP->size   = xByteCt * rows;
     for (row = 0; row < rows; ++row) {
         u1 * thisRow;  /* malloc'ed */
         unsigned int byteOn;
         unsigned int bitOn;
@@ -414,10 +415,11 @@ newBitmap(unsigned int    const bpp,
 
     MALLOCARRAY_NOFAIL(rowData, rows);
 
     icBitmapP->xBytes = xByteCt;
     icBitmapP->data   = rowData;
+    overflow2(xByteCt,rows);
     icBitmapP->size   = xByteCt * rows;
 
     unsigned int const assumedBpp = (pa == NULL) ? 1 : bpp;
 
     switch (assumedBpp) {
@@ -704,10 +706,14 @@ addEntryToIcon(MS_Ico *     const MSIcon
     entryP->planes        = 1;
     entryP->bitcount      = bpp;
     /* all the icons I looked at ignored this value */
     entryP->ih            = newInfoHeader(*entryP);
     entryP->colors        = paletteP->colors;
+    overflow2(4, entryCols);
+    overflow_add(xorBitmapP->size, andBitmapP->size);
+    overflow_add(xorBitmapP->size + andBitmapP->size, 40);
+    overflow_add(xorBitmapP->size + andBitmapP->size + 40, 4 * entryCols);
     entryP->size_in_bytes =
         xorBitmapP->size + andBitmapP->size + 40 + (4 * entryCols);
     if (verbose)
         pm_message("entry->size_in_bytes = %u + %u + %u = %u",
                    xorBitmapP->size, andBitmapP->size,
--- a/converter/ppm/ppmtoxpm.c
+++ b/converter/ppm/ppmtoxpm.c
@@ -306,10 +306,11 @@ genCmap(colorhist_vector const chv,
     CixelMap * cmap;  /* malloc'ed */
     unsigned int cmapIndex;
     unsigned int charsPerPixel;
     unsigned int xpmMaxval;
 
+    if (includeTransparent) overflow_add(ncolors, 1);
     MALLOCARRAY(cmap, cmapSize);
     if (cmapP == NULL)
         pm_error("Can't get memory for a %u-entry color map", cmapSize);
 
     xpmMaxval = xpmMaxvalFromMaxval(maxval);
--- a/editor/pamcut.c
+++ b/editor/pamcut.c
@@ -831,10 +831,12 @@ cutOneImage(FILE *             const ifP
                    inpam.width, inpam.height);
         drainRaster(&inpam);
     } else {
         outpam = inpam;    /* Initial value -- most fields should be same */
         outpam.file   = ofP;
+	overflow_add(rightcol, 1);
+	overflow_add(bottomrow, 1);
         outpam.width  = rightcol - leftcol + 1;
         outpam.height = bottomrow - toprow + 1;
 
         pnm_writepaminit(&outpam);
 
--- a/editor/pnmgamma.c
+++ b/editor/pnmgamma.c
@@ -594,10 +594,11 @@ createGammaTables(enum transferFunction
                   xelval **             const rtableP,
                   xelval **             const gtableP,
                   xelval **             const btableP) {
 
     /* Allocate space for the tables. */
+    overflow_add(maxval, 1);
     MALLOCARRAY(*rtableP, maxval+1);
     MALLOCARRAY(*gtableP, maxval+1);
     MALLOCARRAY(*btableP, maxval+1);
     if (*rtableP == NULL || *gtableP == NULL || *btableP == NULL)
         pm_error("Can't get memory to make gamma transfer tables");
--- a/editor/pnmhisteq.c
+++ b/editor/pnmhisteq.c
@@ -104,10 +104,11 @@ computeLuminosityHistogram(xel * const *
 -----------------------------------------------------------------------------*/
     xelval lmin, lmax;
     unsigned int pixelCount;
     unsigned int * lumahist;
 
+    overflow_add(maxval, 1);
     MALLOCARRAY(lumahist, maxval + 1);
     if (lumahist == NULL)
         pm_error("Out of storage allocating array for %u histogram elements",
                  maxval + 1);
 
--- a/editor/pnmpad.c
+++ b/editor/pnmpad.c
@@ -1125,10 +1125,12 @@ main(int argc, const char ** argv) {
     if (cmdline.verbose)
         pm_message("image WxH = %dx%d", cols, rows);
 
     computePadSizes(cmdline, cols, rows, &lpad, &rpad, &tpad, &bpad);
 
+    overflow_add(cols, lpad);
+    overflow_add(cols + lpad, rpad);
     newcols = cols + lpad + rpad;
 
     if (cmdline.reportonly)
         reportPadSizes(cols, rows, lpad, rpad, tpad, bpad);
     else {
--- a/editor/pnmremap.c
+++ b/editor/pnmremap.c
@@ -468,10 +468,11 @@ fserr_init(struct pam *   const pamP,
 -----------------------------------------------------------------------------*/
     unsigned int plane;
 
     unsigned int const fserrSz = pamP->width + 2;
 
+    overflow_add(pamP->width, 2);
     fserrP->width = pamP->width;
 
     MALLOCARRAY(fserrP->thiserr, pamP->depth);
     if (fserrP->thiserr == NULL)
         pm_error("Out of memory allocating Floyd-Steinberg structures "
@@ -506,10 +507,11 @@ static void
 floydInitRow(struct pam *   const pamP,
              struct Fserr * const fserrP) {
 
     unsigned int col;
 
+    overflow_add(pamP->width, 2);
     for (col = 0; col < pamP->width + 2; ++col) {
         unsigned int plane;
         for (plane = 0; plane < pamP->depth; ++plane)
             fserrP->nexterr[plane][col] = 0;
     }
--- a/editor/pnmscalefixed.c
+++ b/editor/pnmscalefixed.c
@@ -264,10 +264,12 @@ compute_output_dimensions(const struct c
        having to check their numbers to avoid catastrophe.
     */
 
     if (*newcolsP < 1) *newcolsP = 1;
     if (*newrowsP < 1) *newrowsP = 1;
+
+    overflow2(*newcolsP, *newrowsP);
 }
 
 
 
 static void
--- a/editor/specialty/pamoil.c
+++ b/editor/specialty/pamoil.c
@@ -183,10 +183,11 @@ main(int argc, const char ** argv) {
 
     ifP = pm_openr(cmdline.inputFileNm);
 
     tuples = pnm_readpam(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
 
+    overflow_add(inpam.maxval, 1);
     MALLOCARRAY(hist, inpam.maxval + 1);
     if (hist == NULL)
         pm_error("Unable to allocate memory for histogram.");
 
     outpam = inpam; outpam.file = stdout;
--- a/lib/libpam.c
+++ b/lib/libpam.c
@@ -256,11 +256,12 @@ allocPamRow(const struct pam * const pam
     */
 
     unsigned int const bytesPerTuple = allocationDepth(pamP) * sizeof(sample);
     tuple * tuplerow;
 
-    tuplerow = malloc(pamP->width * (sizeof(tuple *) + bytesPerTuple));
+    overflow_add(sizeof(tuple *), bytesPerTuple);
+    tuplerow = malloc2(pamP->width, (sizeof(tuple *) + bytesPerTuple));
 
     if (tuplerow != NULL) {
         /* Now we initialize the pointers to the individual tuples
            to make this a regulation C two dimensional array.
         */
--- a/lib/libpm.c
+++ b/lib/libpm.c
@@ -937,5 +937,54 @@ pm_parse_maxval(const char * const arg)
     }
     return maxval;
 }
 
 
+/*
+ *	Maths wrapping
+ */
+
+void __overflow2(int a, int b)
+{
+        if(a < 0 || b < 0)
+                pm_error("object too large");
+        if(b == 0)
+                return;
+        if(a > INT_MAX / b)
+                pm_error("object too large");
+}
+
+void overflow3(int a, int b, int c)
+{
+        overflow2(a,b);
+        overflow2(a*b, c);
+}
+
+void overflow_add(int a, int b)
+{
+        if( a > INT_MAX - b)
+                pm_error("object too large");
+}
+
+void *malloc2(int a, int b)
+{
+        overflow2(a, b);
+        if(a*b == 0)
+                pm_error("Zero byte allocation");
+        return malloc(a*b);
+}
+
+void *malloc3(int a, int b, int c)
+{
+        overflow3(a, b, c);
+        if(a*b*c == 0)
+                pm_error("Zero byte allocation");
+        return malloc(a*b*c);
+}
+
+void *realloc2(void * a, int b, int c)
+{
+        overflow2(b, c);
+        if(b*c == 0)
+                pm_error("Zero byte allocation");
+        return realloc(a, b*c);
+}
--- a/lib/pm.h
+++ b/lib/pm.h
@@ -463,7 +463,14 @@ pm_parse_maxval(const char * const arg);
 
 #ifdef __cplusplus
 }
 #endif
 
+void *malloc2(int, int);
+void *malloc3(int, int, int);
+#define overflow2(a,b) __overflow2(a,b)
+void __overflow2(int, int);
+void overflow3(int, int, int);
+void overflow_add(int, int);
+
 
 #endif
--- a/other/pnmcolormap.c
+++ b/other/pnmcolormap.c
@@ -1130,10 +1130,11 @@ colormapToSquare(struct pam * const pamP
         unsigned int const intsqrt = (int)sqrt((float)colormap.size);
         if (SQR(intsqrt) == colormap.size)
             pamP->width = intsqrt;
         else
             pamP->width = intsqrt + 1;
+            overflow_add(intsqrt, 1);
     }
     {
         unsigned int const intQuotient = colormap.size / pamP->width;
         if (pamP->width * intQuotient == colormap.size)
             pamP->height = intQuotient;
--- a/urt/rle.h
+++ b/urt/rle.h
@@ -150,10 +150,21 @@ rle_hdr             /* End of typedef. *
  *
  * Global variable with possibly useful default values.
  */
 extern rle_hdr rle_dflt_hdr;
 
+/*
+ * Provided by pm library
+ */
+
+extern void overflow_add(int, int);
+#define overflow2(a,b) __overflow2(a,b)
+extern void __overflow2(int, int);
+extern void overflow3(int, int, int);
+extern void *malloc2(int, int);
+extern void *malloc3(int, int, int);
+extern void *realloc2(void *, int, int);
 
 /* Declare RLE library routines. */
 
 /*****************************************************************
  * TAG( rle_get_error )
--- a/urt/rle_addhist.c
+++ b/urt/rle_addhist.c
@@ -55,18 +55,24 @@ newCommentLen(const char *  const histoi
             length += thisArgLen;
             length += 1;  /* For the space */
         }
     }
 
+    overflow_add(length, strlen(timedate));
     /* Add length of date and time in ASCII. */
     length += strlen(timedate);
 
+    overflow_add(strlen(padding), 4);
+    overflow_add(strlen(histoire), strlen(padding) + 4);
+    overflow_add(length, strlen(histoire) + strlen(padding) + 4);
     /* Add length of padding, "on ", and length of history name plus "="*/
     length += strlen(padding) + 3 + strlen(histoire) + 1;
 
-    if (old && *old)
+    if (old && *old) {
+        overflow_add(length, strlen(old));
         length += strlen(old);       /* add length if there. */
+    }
 
     ++length;     /* Add size of terminating NUL. */
 
     return length;
 }
--- a/converter/other/gemtopnm.c
+++ b/converter/other/gemtopnm.c
@@ -151,10 +151,11 @@ main(int argc, const char ** argv) {
     else
         type = PPM_TYPE;
 
     pnm_writepnminit(stdout, cols, rows, MAXVAL, type, 0);
 
+    overflow_add(cols, padright);
     {
         /* allocate input row data structure */
         unsigned int plane;
         for (plane = 0; plane < planes; ++plane) {
             MALLOCARRAY(bitrow[plane], cols + padright);
--- a/converter/other/pnmtops.c
+++ b/converter/other/pnmtops.c
@@ -296,21 +296,26 @@ parseCommandLine(int argc, const char **
     cmdlineP->showpage = !noshowpage;
 
     validateCompDimension(width,  psScaleFactor, "-width value");
     validateCompDimension(height, psScaleFactor, "-height value");
 
+    overflow2(width, psScaleFactor);
     cmdlineP->width  = width  * psScaleFactor;
+    overflow2(height, psScaleFactor);
     cmdlineP->height = height * psScaleFactor;
 
     if (imagewidthSpec) {
         validateCompDimension(imagewidth, psScaleFactor, "-imagewidth value");
+
+	overflow2(imagewidth, psScaleFactor);
         cmdlineP->imagewidth = imagewidth * psScaleFactor;
     }
     else
         cmdlineP->imagewidth = 0;
     if (imageheightSpec) {
         validateCompDimension(imagewidth, psScaleFactor, "-imageheight value");
+	overflow2(imageheight, psScaleFactor);
         cmdlineP->imageheight = imageheight * psScaleFactor;
     }
     else
         cmdlineP->imageheight = 0;
 
--- a/converter/pbm/pbmtoppa/pbm.c
+++ b/converter/pbm/pbmtoppa/pbm.c
@@ -155,10 +155,11 @@ pbm_readline(pbm_stat *      const pbmSt
                 } else
                     retval = 0;
                 break;
             case P4: {
                 int tmp, tmp2;
+                overflow_add(pbmStatP->width, 7);
                 tmp = (pbmStatP->width+7)/8;
                 tmp2 = fread(data,1,tmp,pbmStatP->fptr);
                 if (tmp2 == tmp) {
                     ++pbmStatP->current_line;
                     retval = 1;
--- a/converter/ppm/sldtoppm.c
+++ b/converter/ppm/sldtoppm.c
@@ -540,10 +540,12 @@ slider(slvecfn         slvec,
     computeSize(slfrof, wantAdjust, uscale, sxsize, sysize,
                 &ixdots, &iydots, &dsar);
 
     /* Allocate image buffer and clear it to black. */
 
+    overflow_add(ixdots, 1);
+    overflow_add(iydots, 1);
     pixcols = ixdots + 1;
     pixrows = iydots + 1;
     pixels = ppm_allocarray(pixcols, pixrows);
     PPM_ASSIGN(rgbcolor, 0, 0, 0);
     ppmd_filledrectangle(pixels, pixcols, pixrows, pixmaxval, 0, 0,
--- a/generator/pbmtext.c
+++ b/generator/pbmtext.c
@@ -79,18 +79,21 @@ textFmCmdLine(int argc, const char ** ar
                  "characters of text", MAXLINECHARS);
 
     text[0] = '\0';
 
     for (i = 1, totaltextsize = 0; i < argc; ++i) {
-        if (i > 1)
+        if (i > 1) {
             strcat(text, " ");
+	    overflow_add(totaltextsize, 1);
+        }
 
         if (strlen(argv[i]) > MAXLINECHARS) { /* avoid arithmetic overflow */
             pm_error("Command line argument %u is %u characters.  "
                      "Cannot process longer than %u",
                      i, (unsigned) strlen(argv[i]), (unsigned) MAXLINECHARS);
         }
+	overflow_add(totaltextsize, strlen(argv[i]));
         totaltextsize += strlen(argv[i]) + (i > 1 ? 1 : 0);
         if (totaltextsize > MAXLINECHARS)
            pm_error("Input text is %u characters.  "
                     "Cannot process longer than %u",
                     totaltextsize, (unsigned int) MAXLINECHARS);