netpbm-security-code.patch 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. diff --git a/analyzer/pgmtexture.c b/analyzer/pgmtexture.c
  2. index 0731733..635c58b 100644
  3. --- a/analyzer/pgmtexture.c
  4. +++ b/analyzer/pgmtexture.c
  5. @@ -98,6 +98,8 @@ vector(unsigned int const nl,
  6. assert(nh >= nl);
  7. + overflow_add(nh - nl, 1);
  8. +
  9. MALLOCARRAY(v, (unsigned) (nh - nl + 1));
  10. if (v == NULL)
  11. @@ -129,6 +131,7 @@ matrix (unsigned int const nrl,
  12. assert(nrh >= nrl);
  13. /* allocate pointers to rows */
  14. + overflow_add(nrh - nrl, 1);
  15. MALLOCARRAY(m, (unsigned) (nrh - nrl + 1));
  16. if (m == NULL)
  17. pm_error("Unable to allocate memory for a matrix.");
  18. @@ -137,6 +140,7 @@ matrix (unsigned int const nrl,
  19. assert (nch >= ncl);
  20. + overflow_add(nch - ncl, 1);
  21. /* allocate rows and set pointers to them */
  22. for (i = nrl; i <= nrh; ++i) {
  23. MALLOCARRAY(m[i], (unsigned) (nch - ncl + 1));
  24. diff --git a/converter/other/gemtopnm.c b/converter/other/gemtopnm.c
  25. index aac7479..5f1a51a 100644
  26. --- a/converter/other/gemtopnm.c
  27. +++ b/converter/other/gemtopnm.c
  28. @@ -106,6 +106,7 @@ main(argc, argv)
  29. pnm_writepnminit( stdout, cols, rows, MAXVAL, type, 0 );
  30. + overflow_add(cols, padright);
  31. {
  32. /* allocate input row data structure */
  33. int plane;
  34. diff --git a/converter/other/jpegtopnm.c b/converter/other/jpegtopnm.c
  35. index ab3b18e..c324b86 100644
  36. --- a/converter/other/jpegtopnm.c
  37. +++ b/converter/other/jpegtopnm.c
  38. @@ -861,6 +861,8 @@ convertImage(FILE * const ofP,
  39. /* Calculate output image dimensions so we can allocate space */
  40. jpeg_calc_output_dimensions(cinfoP);
  41. + overflow2(cinfoP->output_width, cinfoP->output_components);
  42. +
  43. /* Start decompressor */
  44. jpeg_start_decompress(cinfoP);
  45. diff --git a/converter/other/pbmtopgm.c b/converter/other/pbmtopgm.c
  46. index 69b20fb..382a487 100644
  47. --- a/converter/other/pbmtopgm.c
  48. +++ b/converter/other/pbmtopgm.c
  49. @@ -47,6 +47,7 @@ main(int argc, char *argv[]) {
  50. "than the image height (%u rows)", height, rows);
  51. outrow = pgm_allocrow(cols) ;
  52. + overflow2(width, height);
  53. maxval = MIN(PGM_OVERALLMAXVAL, width*height);
  54. pgm_writepgminit(stdout, cols, rows, maxval, 0) ;
  55. diff --git a/converter/other/pnmtoddif.c b/converter/other/pnmtoddif.c
  56. index ae8c852..9ee037b 100644
  57. --- a/converter/other/pnmtoddif.c
  58. +++ b/converter/other/pnmtoddif.c
  59. @@ -632,6 +632,7 @@ main(int argc, char *argv[]) {
  60. switch (PNM_FORMAT_TYPE(format)) {
  61. case PBM_TYPE:
  62. ip.bits_per_pixel = 1;
  63. + overflow_add(cols, 7);
  64. ip.bytes_per_line = (cols + 7) / 8;
  65. ip.spectral = 2;
  66. ip.components = 1;
  67. @@ -647,6 +648,7 @@ main(int argc, char *argv[]) {
  68. ip.polarity = 2;
  69. break;
  70. case PPM_TYPE:
  71. + overflow2(cols, 3);
  72. ip.bytes_per_line = 3 * cols;
  73. ip.bits_per_pixel = 24;
  74. ip.spectral = 5;
  75. diff --git a/converter/other/pnmtojpeg.c b/converter/other/pnmtojpeg.c
  76. index ce231c9..1279040 100644
  77. --- a/converter/other/pnmtojpeg.c
  78. +++ b/converter/other/pnmtojpeg.c
  79. @@ -605,7 +605,11 @@ read_scan_script(j_compress_ptr const cinfo,
  80. want JPOOL_PERMANENT.
  81. */
  82. const unsigned int scan_info_size = nscans * sizeof(jpeg_scan_info);
  83. - jpeg_scan_info * const scan_info =
  84. + const jpeg_scan_info * scan_info;
  85. +
  86. + overflow2(nscans, sizeof(jpeg_scan_info));
  87. +
  88. + scan_info =
  89. (jpeg_scan_info *)
  90. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  91. scan_info_size);
  92. @@ -937,6 +941,8 @@ compute_rescaling_array(JSAMPLE ** const rescale_p, const pixval maxval,
  93. const long half_maxval = maxval / 2;
  94. long val;
  95. + overflow_add(maxval, 1);
  96. + overflow2(maxval+1, sizeof(JSAMPLE));
  97. *rescale_p = (JSAMPLE *)
  98. (cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_IMAGE,
  99. (size_t) (((long) maxval + 1L) *
  100. @@ -1015,6 +1021,7 @@ convert_scanlines(struct jpeg_compress_struct * const cinfo_p,
  101. */
  102. /* Allocate the libpnm output and compressor input buffers */
  103. + overflow2(cinfo_p->image_width, cinfo_p->input_components);
  104. buffer = (*cinfo_p->mem->alloc_sarray)
  105. ((j_common_ptr) cinfo_p, JPOOL_IMAGE,
  106. (unsigned int) cinfo_p->image_width * cinfo_p->input_components,
  107. diff --git a/converter/other/pnmtops.c b/converter/other/pnmtops.c
  108. index 6cd6be9..8a7d25a 100644
  109. --- a/converter/other/pnmtops.c
  110. +++ b/converter/other/pnmtops.c
  111. @@ -292,17 +292,21 @@ parseCommandLine(int argc, const char ** argv,
  112. validateCompDimension(width, 72, "-width value");
  113. validateCompDimension(height, 72, "-height value");
  114. + overflow2(width, 72);
  115. cmdlineP->width = width * 72;
  116. + overflow2(height, 72);
  117. cmdlineP->height = height * 72;
  118. if (imagewidthSpec) {
  119. validateCompDimension(imagewidth, 72, "-imagewidth value");
  120. + overflow2(imagewidth, 72);
  121. cmdlineP->imagewidth = imagewidth * 72;
  122. }
  123. else
  124. cmdlineP->imagewidth = 0;
  125. if (imageheightSpec) {
  126. - validateCompDimension(imagewidth, 72, "-imageheight value");
  127. + validateCompDimension(imageheight, 72, "-imageheight value");
  128. + overflow2(imageheight, 72);
  129. cmdlineP->imageheight = imageheight * 72;
  130. }
  131. else
  132. diff --git a/converter/other/rletopnm.c b/converter/other/rletopnm.c
  133. index 9995914..058278c 100644
  134. --- a/converter/other/rletopnm.c
  135. +++ b/converter/other/rletopnm.c
  136. @@ -19,6 +19,8 @@
  137. * If you modify this software, you should include a notice giving the
  138. * name of the person performing the modification, the date of modification,
  139. * and the reason for such modification.
  140. + *
  141. + * 2002-12-19: Fix maths wrapping bugs. Alan Cox <alan@redhat.com>
  142. */
  143. /*
  144. * rletopnm - A conversion program to convert from Utah's "rle" image format
  145. diff --git a/converter/other/sirtopnm.c b/converter/other/sirtopnm.c
  146. index fafcc91..9fe49d0 100644
  147. --- a/converter/other/sirtopnm.c
  148. +++ b/converter/other/sirtopnm.c
  149. @@ -69,6 +69,7 @@ char* argv[];
  150. }
  151. break;
  152. case PPM_TYPE:
  153. + overflow3(cols, rows, 3);
  154. picsize = cols * rows * 3;
  155. planesize = cols * rows;
  156. if ( !( sirarray = (unsigned char*) malloc( picsize ) ) )
  157. diff --git a/converter/other/tifftopnm.c b/converter/other/tifftopnm.c
  158. index 0d6494f..19e9e38 100644
  159. --- a/converter/other/tifftopnm.c
  160. +++ b/converter/other/tifftopnm.c
  161. @@ -1309,7 +1309,9 @@ convertRasterByRows(pnmOut * const pnmOutP,
  162. if (scanbuf == NULL)
  163. pm_error("can't allocate memory for scanline buffer");
  164. - MALLOCARRAY(samplebuf, cols * spp);
  165. + /* samplebuf is unsigned int * !!! */
  166. + samplebuf = (unsigned int *) malloc3(cols , sizeof(unsigned int) , spp);
  167. +
  168. if (samplebuf == NULL)
  169. pm_error("can't allocate memory for row buffer");
  170. diff --git a/converter/other/xwdtopnm.c b/converter/other/xwdtopnm.c
  171. index 45d66b4..c914481 100644
  172. --- a/converter/other/xwdtopnm.c
  173. +++ b/converter/other/xwdtopnm.c
  174. @@ -209,6 +209,10 @@ processX10Header(X10WDFileHeader * const h10P,
  175. *colorsP = pnm_allocrow(2);
  176. PNM_ASSIGN1((*colorsP)[0], 0);
  177. PNM_ASSIGN1((*colorsP)[1], *maxvalP);
  178. + overflow_add(h10P->pixmap_width, 15);
  179. + if(h10P->pixmap_width < 0)
  180. + pm_error("assert: negative width");
  181. + overflow2((((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width), 8);
  182. *padrightP =
  183. (((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width) * 8;
  184. *bits_per_itemP = 16;
  185. @@ -634,6 +638,7 @@ processX11Header(X11WDFileHeader * const h11P,
  186. *colsP = h11FixedP->pixmap_width;
  187. *rowsP = h11FixedP->pixmap_height;
  188. + overflow2(h11FixedP->bytes_per_line, 8);
  189. *padrightP =
  190. h11FixedP->bytes_per_line * 8 -
  191. h11FixedP->pixmap_width * h11FixedP->bits_per_pixel;
  192. diff --git a/converter/pbm/mdatopbm.c b/converter/pbm/mdatopbm.c
  193. index d8e0657..12c7468 100644
  194. --- a/converter/pbm/mdatopbm.c
  195. +++ b/converter/pbm/mdatopbm.c
  196. @@ -245,10 +245,13 @@ main(int argc, char **argv) {
  197. pm_readlittleshort(infile, &yy); nInCols = yy;
  198. }
  199. + overflow2(nOutCols, 8);
  200. nOutCols = 8 * nInCols;
  201. nOutRows = nInRows;
  202. - if (bScale)
  203. + if (bScale) {
  204. + overflow2(nOutRows, 2);
  205. nOutRows *= 2;
  206. + }
  207. data = pbm_allocarray(nOutCols, nOutRows);
  208. diff --git a/converter/pbm/mgrtopbm.c b/converter/pbm/mgrtopbm.c
  209. index 9f7004a..60e8477 100644
  210. --- a/converter/pbm/mgrtopbm.c
  211. +++ b/converter/pbm/mgrtopbm.c
  212. @@ -65,6 +65,8 @@ readMgrHeader(FILE * const ifP,
  213. if (head.h_high < ' ' || head.l_high < ' ')
  214. pm_error("Invalid width field in MGR header");
  215. + overflow_add(*colsP, pad);
  216. +
  217. *colsP = (((int)head.h_wide - ' ') << 6) + ((int)head.l_wide - ' ');
  218. *rowsP = (((int)head.h_high - ' ') << 6) + ((int) head.l_high - ' ');
  219. *padrightP = ( ( *colsP + pad - 1 ) / pad ) * pad - *colsP;
  220. diff --git a/converter/pbm/pbmtogem.c b/converter/pbm/pbmtogem.c
  221. index 9eab041..13b0257 100644
  222. --- a/converter/pbm/pbmtogem.c
  223. +++ b/converter/pbm/pbmtogem.c
  224. @@ -79,6 +79,7 @@ putinit (int const rows, int const cols)
  225. bitsperitem = 0;
  226. bitshift = 7;
  227. outcol = 0;
  228. + overflow_add(cols, 7);
  229. outmax = (cols + 7) / 8;
  230. outrow = (unsigned char *) pm_allocrow (outmax, sizeof (unsigned char));
  231. lastrow = (unsigned char *) pm_allocrow (outmax, sizeof (unsigned char));
  232. diff --git a/converter/pbm/pbmtogo.c b/converter/pbm/pbmtogo.c
  233. index 23b2ee9..d2ee91f 100644
  234. --- a/converter/pbm/pbmtogo.c
  235. +++ b/converter/pbm/pbmtogo.c
  236. @@ -158,6 +158,7 @@ main(int argc,
  237. bitrow = pbm_allocrow(cols);
  238. /* Round cols up to the nearest multiple of 8. */
  239. + overflow_add(cols, 7);
  240. rucols = ( cols + 7 ) / 8;
  241. bytesperrow = rucols; /* GraphOn uses bytes */
  242. rucols = rucols * 8;
  243. diff --git a/converter/pbm/pbmtolj.c b/converter/pbm/pbmtolj.c
  244. index 0cceb4f..fdab6df 100644
  245. --- a/converter/pbm/pbmtolj.c
  246. +++ b/converter/pbm/pbmtolj.c
  247. @@ -120,7 +120,11 @@ parseCommandLine(int argc, char ** argv,
  248. static void
  249. allocateBuffers(unsigned int const cols) {
  250. + overflow_add(cols, 8);
  251. rowBufferSize = (cols + 7) / 8;
  252. + overflow_add(rowBufferSize, 128);
  253. + overflow_add(rowBufferSize, rowBufferSize+128);
  254. + overflow_add(rowBufferSize+10, rowBufferSize/8);
  255. packBufferSize = rowBufferSize + (rowBufferSize + 127) / 128 + 1;
  256. deltaBufferSize = rowBufferSize + rowBufferSize / 8 + 10;
  257. diff --git a/converter/pbm/pbmtomda.c b/converter/pbm/pbmtomda.c
  258. index 3ad5149..9efe5cf 100644
  259. --- a/converter/pbm/pbmtomda.c
  260. +++ b/converter/pbm/pbmtomda.c
  261. @@ -179,6 +179,7 @@ int main(int argc, char **argv)
  262. nOutRowsUnrounded = bScale ? nInRows/2 : nInRows;
  263. + overflow_add(nOutRowsUnrounded, 3);
  264. nOutRows = ((nOutRowsUnrounded + 3) / 4) * 4;
  265. /* MDA wants rows a multiple of 4 */
  266. nOutCols = nInCols / 8;
  267. diff --git a/converter/pbm/pbmtoppa/pbm.c b/converter/pbm/pbmtoppa/pbm.c
  268. index 2f8a42b..1c8d236 100644
  269. --- a/converter/pbm/pbmtoppa/pbm.c
  270. +++ b/converter/pbm/pbmtoppa/pbm.c
  271. @@ -106,6 +106,7 @@ int pbm_readline(pbm_stat* pbm,unsigned char* data)
  272. return 0;
  273. case P4:
  274. + overflow_add(pbm->width, 7);
  275. tmp=(pbm->width+7)/8;
  276. tmp2=fread(data,1,tmp,pbm->fptr);
  277. if(tmp2 == tmp)
  278. @@ -130,6 +131,7 @@ void pbm_unreadline (pbm_stat *pbm, void *data)
  279. return;
  280. pbm->unread = 1;
  281. + overflow_add(pbm->width, 7);
  282. pbm->revdata = malloc ((pbm->width+7)/8);
  283. memcpy (pbm->revdata, data, (pbm->width+7)/8);
  284. pbm->current_line--;
  285. diff --git a/converter/pbm/pbmtoppa/pbmtoppa.c b/converter/pbm/pbmtoppa/pbmtoppa.c
  286. index f43c08a..98e0284 100644
  287. --- a/converter/pbm/pbmtoppa/pbmtoppa.c
  288. +++ b/converter/pbm/pbmtoppa/pbmtoppa.c
  289. @@ -452,6 +452,7 @@ main(int argc, char *argv[]) {
  290. pm_error("main(): unrecognized parameter '%s'", argv[argn]);
  291. }
  292. + overflow_add(Width, 7);
  293. Pwidth=(Width+7)/8;
  294. printer.fptr=out;
  295. diff --git a/converter/pbm/pbmtoxbm.c b/converter/pbm/pbmtoxbm.c
  296. index 14c6b85..362b70e 100644
  297. --- a/converter/pbm/pbmtoxbm.c
  298. +++ b/converter/pbm/pbmtoxbm.c
  299. @@ -351,6 +351,8 @@ convertRaster(FILE * const ifP,
  300. unsigned char * bitrow;
  301. unsigned int row;
  302. +
  303. + overflow_add(cols, padright);
  304. putinit(xbmVersion);
  305. diff --git a/converter/pbm/thinkjettopbm.l b/converter/pbm/thinkjettopbm.l
  306. index 5de4f2b..7f31de5 100644
  307. --- a/converter/pbm/thinkjettopbm.l
  308. +++ b/converter/pbm/thinkjettopbm.l
  309. @@ -114,7 +114,9 @@ DIG [0-9]
  310. <RASTERMODE>\033\*b{DIG}+W {
  311. int l;
  312. if (rowCount >= rowCapacity) {
  313. - rowCapacity += 100;
  314. + overflow_add(rowCapacity, 100);
  315. + rowCapacity += 100;
  316. + overflow2(rowCapacity, sizeof *rows);
  317. rows = realloc (rows, rowCapacity * sizeof *rows);
  318. if (rows == NULL)
  319. pm_error ("Out of memory.");
  320. @@ -226,6 +228,8 @@ yywrap (void)
  321. /*
  322. * Quite simple since ThinkJet bit arrangement matches PBM
  323. */
  324. +
  325. + overflow2(maxRowLength, 8);
  326. pbm_writepbminit(stdout, maxRowLength*8, rowCount, 0);
  327. packed_bitrow = malloc(maxRowLength);
  328. diff --git a/converter/pbm/ybmtopbm.c b/converter/pbm/ybmtopbm.c
  329. index 2a42908..cf1ff03 100644
  330. --- a/converter/pbm/ybmtopbm.c
  331. +++ b/converter/pbm/ybmtopbm.c
  332. @@ -43,6 +43,7 @@ getinit(FILE * const ifP,
  333. pm_error("EOF / read error");
  334. *depthP = 1;
  335. + overflow_add(*colsP, 15);
  336. }
  337. diff --git a/converter/pgm/lispmtopgm.c b/converter/pgm/lispmtopgm.c
  338. index 40dd3fb..b5469f7 100644
  339. --- a/converter/pgm/lispmtopgm.c
  340. +++ b/converter/pgm/lispmtopgm.c
  341. @@ -58,6 +58,7 @@ main( argc, argv )
  342. pm_error( "depth (%d bits) is too large", depth);
  343. pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 );
  344. + overflow_add(cols, 7);
  345. grayrow = pgm_allocrow( ( cols + 7 ) / 8 * 8 );
  346. for ( row = 0; row < rows; ++row )
  347. @@ -102,6 +103,8 @@ getinit( file, colsP, rowsP, depthP, padrightP )
  348. if ( *depthP == 0 )
  349. *depthP = 1; /* very old file */
  350. +
  351. + overflow_add((int)colsP, 31);
  352. *padrightP = ( ( *colsP + 31 ) / 32 ) * 32 - *colsP;
  353. diff --git a/converter/pgm/psidtopgm.c b/converter/pgm/psidtopgm.c
  354. index 07417d1..25bb311 100644
  355. --- a/converter/pgm/psidtopgm.c
  356. +++ b/converter/pgm/psidtopgm.c
  357. @@ -78,6 +78,7 @@ main(int argc,
  358. pm_error("bits/sample (%d) is too large.", bitspersample);
  359. pgm_writepgminit(stdout, cols, rows, maxval, 0);
  360. + overflow_add(cols, 7);
  361. grayrow = pgm_allocrow((cols + 7) / 8 * 8);
  362. for (row = 0; row < rows; ++row) {
  363. unsigned int col;
  364. diff --git a/converter/ppm/Makefile b/converter/ppm/Makefile
  365. index 09f05cd..f68170f 100644
  366. --- a/converter/ppm/Makefile
  367. +++ b/converter/ppm/Makefile
  368. @@ -11,7 +11,7 @@ SUBDIRS = hpcdtoppm
  369. PORTBINARIES = 411toppm eyuvtoppm gouldtoppm ilbmtoppm imgtoppm \
  370. leaftoppm mtvtoppm neotoppm \
  371. - pcxtoppm pc1toppm pi1toppm picttoppm pjtoppm \
  372. + pcxtoppm pc1toppm pi1toppm pjtoppm \
  373. ppmtoacad ppmtoapplevol ppmtoarbtxt ppmtoascii \
  374. ppmtobmp ppmtoeyuv ppmtogif ppmtoicr ppmtoilbm \
  375. ppmtoleaf ppmtolj ppmtomitsu ppmtoneo \
  376. diff --git a/converter/ppm/ilbmtoppm.c b/converter/ppm/ilbmtoppm.c
  377. index 662be0b..2a86efc 100644
  378. --- a/converter/ppm/ilbmtoppm.c
  379. +++ b/converter/ppm/ilbmtoppm.c
  380. @@ -606,6 +606,7 @@ decode_row(FILE * const ifP,
  381. rawtype *chp;
  382. cols = bmhdP->w;
  383. + overflow_add(cols, 15);
  384. bytes = RowBytes(cols);
  385. for( plane = 0; plane < nPlanes; plane++ ) {
  386. int mask;
  387. @@ -693,6 +694,23 @@ decode_mask(FILE * const ifP,
  388. Multipalette handling
  389. ****************************************************************************/
  390. +static void *
  391. +xmalloc2(x, y)
  392. + int x;
  393. + int y;
  394. +{
  395. + void *mem;
  396. +
  397. + overflow2(x,y);
  398. + if( x * y == 0 )
  399. + return NULL;
  400. +
  401. + mem = malloc2(x,y);
  402. + if( mem == NULL )
  403. + pm_error("out of memory allocating %d bytes", x * y);
  404. + return mem;
  405. +}
  406. +
  407. static void
  408. multi_adjust(cmap, row, palchange)
  409. @@ -1355,6 +1373,9 @@ dcol_to_ppm(FILE * const ifP,
  410. if( redmaxval != maxval || greenmaxval != maxval || bluemaxval != maxval )
  411. pm_message("scaling colors to %d bits", pm_maxvaltobits(maxval));
  412. + overflow_add(redmaxval, 1);
  413. + overflow_add(greenmaxval, 1);
  414. + overflow_add(bluemaxval, 1);
  415. MALLOCARRAY_NOFAIL(redtable, redmaxval +1);
  416. MALLOCARRAY_NOFAIL(greentable, greenmaxval +1);
  417. MALLOCARRAY_NOFAIL(bluetable, bluemaxval +1);
  418. @@ -1784,7 +1805,9 @@ PCHG_ConvertSmall(PCHG, cmap, mask, datasize)
  419. ChangeCount32 = *data++;
  420. datasize -= 2;
  421. + overflow_add(ChangeCount16, ChangeCount32);
  422. changes = ChangeCount16 + ChangeCount32;
  423. + overflow_add(changes, 1);
  424. for( i = 0; i < changes; i++ ) {
  425. if( totalchanges >= PCHG->TotalChanges ) goto fail;
  426. if( datasize < 2 ) goto fail;
  427. @@ -2049,6 +2072,9 @@ read_pchg(FILE * const ifP,
  428. cmap->mp_change[i] = NULL;
  429. if( PCHG.StartLine < 0 ) {
  430. int nch;
  431. + if(PCHG.MaxReg < PCHG.MinReg)
  432. + pm_error("assert: MinReg > MaxReg");
  433. + overflow_add(PCHG.MaxReg-PCHG.MinReg, 2);
  434. nch = PCHG.MaxReg - PCHG.MinReg +1;
  435. MALLOCARRAY_NOFAIL(cmap->mp_init, nch + 1);
  436. for( i = 0; i < nch; i++ )
  437. @@ -2125,6 +2151,7 @@ process_body( FILE * const ifP,
  438. if (typeid == ID_ILBM) {
  439. int isdeep;
  440. + overflow_add(bmhdP->w, 15);
  441. MALLOCARRAY_NOFAIL(ilbmrow, RowBytes(bmhdP->w));
  442. *viewportmodesP |= fakeviewport; /* -isham/-isehb */
  443. diff --git a/converter/ppm/imgtoppm.c b/converter/ppm/imgtoppm.c
  444. index 7078b88..eb8509e 100644
  445. --- a/converter/ppm/imgtoppm.c
  446. +++ b/converter/ppm/imgtoppm.c
  447. @@ -84,6 +84,7 @@ main(int argc, char ** argv) {
  448. len = atoi((char*) buf );
  449. if ( fread( buf, len, 1, ifp ) != 1 )
  450. pm_error( "bad colormap buf" );
  451. + overflow2(cmaplen, 3);
  452. if ( cmaplen * 3 != len )
  453. {
  454. pm_message(
  455. @@ -105,6 +106,7 @@ main(int argc, char ** argv) {
  456. pm_error( "bad pixel data header" );
  457. buf[8] = '\0';
  458. len = atoi((char*) buf );
  459. + overflow2(cols, rows);
  460. if ( len != cols * rows )
  461. pm_message(
  462. "pixel data length (%d) does not match image size (%d)",
  463. diff --git a/converter/ppm/pcxtoppm.c b/converter/ppm/pcxtoppm.c
  464. index e252ba2..270ae3b 100644
  465. --- a/converter/ppm/pcxtoppm.c
  466. +++ b/converter/ppm/pcxtoppm.c
  467. @@ -409,6 +409,7 @@ pcx_planes_to_pixels(pixels, bitplanes, bytesperline, planes, bitsperpixel)
  468. /*
  469. * clear the pixel buffer
  470. */
  471. + overflow2(bytesperline, 8);
  472. npixels = (bytesperline * 8) / bitsperpixel;
  473. p = pixels;
  474. while (--npixels >= 0)
  475. @@ -470,6 +471,7 @@ pcx_16col_to_ppm(FILE * const ifP,
  476. }
  477. /* BytesPerLine should be >= BitsPerPixel * cols / 8 */
  478. + overflow2(BytesPerLine, 8);
  479. rawcols = BytesPerLine * 8 / BitsPerPixel;
  480. if (headerCols > rawcols) {
  481. pm_message("warning - BytesPerLine = %d, "
  482. diff --git a/converter/ppm/picttoppm.c b/converter/ppm/picttoppm.c
  483. index 828d527..8cdb7b3 100644
  484. --- a/converter/ppm/picttoppm.c
  485. +++ b/converter/ppm/picttoppm.c
  486. @@ -1,3 +1,4 @@
  487. +#error "Unfixable. Don't ship me"
  488. /*
  489. * picttoppm.c -- convert a MacIntosh PICT file to PPM format.
  490. *
  491. diff --git a/converter/ppm/pjtoppm.c b/converter/ppm/pjtoppm.c
  492. index 7b694fb..62ce77e 100644
  493. --- a/converter/ppm/pjtoppm.c
  494. +++ b/converter/ppm/pjtoppm.c
  495. @@ -127,20 +127,22 @@ main(argc, argv)
  496. case 'V': /* send plane */
  497. case 'W': /* send last plane */
  498. if (rows == -1 || r >= rows || image == NULL) {
  499. - if (rows == -1 || r >= rows)
  500. + if (rows == -1 || r >= rows) {
  501. + overflow_add(rows, 100);
  502. rows += 100;
  503. + }
  504. +
  505. if (image == NULL) {
  506. - MALLOCARRAY(image, rows * planes);
  507. - MALLOCARRAY(imlen, rows * planes);
  508. + image = (unsigned char **)
  509. + malloc3(rows , planes , sizeof(unsigned char *));
  510. + imlen = (int *) malloc3(rows , planes, sizeof(int));
  511. }
  512. else {
  513. - image = (unsigned char **)
  514. - realloc(image,
  515. - rows * planes *
  516. - sizeof(unsigned char *));
  517. - imlen = (int *)
  518. - realloc(imlen, rows * planes * sizeof(int));
  519. - }
  520. + overflow2(rows,planes);
  521. + image = (unsigned char **)
  522. + realloc2(image, rows * planes,
  523. + sizeof(unsigned char *));
  524. + imlen = (int *) realloc2(imlen, rows * planes, sizeof(int)); }
  525. }
  526. if (image == NULL || imlen == NULL)
  527. pm_error("out of memory");
  528. @@ -212,8 +214,10 @@ main(argc, argv)
  529. for (i = 0, c = 0; c < imlen[p + r * planes]; c += 2)
  530. for (cmd = image[p + r * planes][c],
  531. val = image[p + r * planes][c+1];
  532. - cmd >= 0 && i < newcols; cmd--, i++)
  533. + cmd >= 0 && i < newcols; cmd--, i++) {
  534. buf[i] = val;
  535. + overflow_add(i, 1);
  536. + }
  537. cols = cols > i ? cols : i;
  538. free(image[p + r * planes]);
  539. /*
  540. @@ -224,6 +228,7 @@ main(argc, argv)
  541. image[p + r * planes] = (unsigned char *) realloc(buf, i);
  542. }
  543. }
  544. + overflow2(cols, 8);
  545. cols *= 8;
  546. }
  547. diff --git a/converter/ppm/ppmtoeyuv.c b/converter/ppm/ppmtoeyuv.c
  548. index f5ce115..6f072be 100644
  549. --- a/converter/ppm/ppmtoeyuv.c
  550. +++ b/converter/ppm/ppmtoeyuv.c
  551. @@ -114,6 +114,7 @@ create_multiplication_tables(const pixval maxval) {
  552. int index;
  553. + overflow_add(maxval, 1);
  554. MALLOCARRAY_NOFAIL(mult299 , maxval+1);
  555. MALLOCARRAY_NOFAIL(mult587 , maxval+1);
  556. MALLOCARRAY_NOFAIL(mult114 , maxval+1);
  557. diff --git a/converter/ppm/ppmtolj.c b/converter/ppm/ppmtolj.c
  558. index 7ed814e..b4e7db1 100644
  559. --- a/converter/ppm/ppmtolj.c
  560. +++ b/converter/ppm/ppmtolj.c
  561. @@ -182,6 +182,7 @@ int main(int argc, char *argv[]) {
  562. ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
  563. pixelrow = ppm_allocrow( cols );
  564. + overflow2(cols, 6);
  565. obuf = (unsigned char *) pm_allocrow(cols * 3, sizeof(unsigned char));
  566. cbuf = (unsigned char *) pm_allocrow(cols * 6, sizeof(unsigned char));
  567. if (mode == C_TRANS_MODE_DELTA)
  568. diff --git a/converter/ppm/ppmtomitsu.c b/converter/ppm/ppmtomitsu.c
  569. index e59f09b..1d2be20 100644
  570. --- a/converter/ppm/ppmtomitsu.c
  571. +++ b/converter/ppm/ppmtomitsu.c
  572. @@ -685,6 +685,8 @@ main(int argc, char * argv[]) {
  573. medias = MSize_User;
  574. if (dpi300) {
  575. + overflow2(medias.maxcols, 2);
  576. + overflow2(medias.maxrows, 2);
  577. medias.maxcols *= 2;
  578. medias.maxrows *= 2;
  579. }
  580. diff --git a/converter/ppm/ppmtopcx.c b/converter/ppm/ppmtopcx.c
  581. index fa68edc..97dfb2b 100644
  582. --- a/converter/ppm/ppmtopcx.c
  583. +++ b/converter/ppm/ppmtopcx.c
  584. @@ -425,6 +425,8 @@ ppmTo16ColorPcx(pixel ** const pixels,
  585. else Planes = 1;
  586. }
  587. }
  588. + overflow2(BitsPerPixel, cols);
  589. + overflow_add(BitsPerPixel * cols, 7);
  590. BytesPerLine = ((cols * BitsPerPixel) + 7) / 8;
  591. MALLOCARRAY_NOFAIL(indexRow, cols);
  592. MALLOCARRAY_NOFAIL(planesrow, BytesPerLine);
  593. diff --git a/converter/ppm/ppmtopict.c b/converter/ppm/ppmtopict.c
  594. index 034e705..4541387 100644
  595. --- a/converter/ppm/ppmtopict.c
  596. +++ b/converter/ppm/ppmtopict.c
  597. @@ -450,6 +450,8 @@ main(int argc, const char ** argv) {
  598. putShort(stdout, 0); /* mode */
  599. /* Finally, write out the data. */
  600. + overflow_add(cols/MAX_COUNT, 1);
  601. + overflow_add(cols, cols/MAX_COUNT+1);
  602. outBuf = malloc((unsigned)(cols+cols/MAX_COUNT+1));
  603. for (row = 0, oc = 0; row < rows; ++row) {
  604. unsigned int rowSize;
  605. diff --git a/converter/ppm/ppmtopj.c b/converter/ppm/ppmtopj.c
  606. index d116773..fc84cac 100644
  607. --- a/converter/ppm/ppmtopj.c
  608. +++ b/converter/ppm/ppmtopj.c
  609. @@ -179,6 +179,7 @@ char *argv[];
  610. pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
  611. pm_close( ifp );
  612. + overflow2(cols,2);
  613. obuf = (unsigned char *) pm_allocrow(cols, sizeof(unsigned char));
  614. cbuf = (unsigned char *) pm_allocrow(cols * 2, sizeof(unsigned char));
  615. diff --git a/converter/ppm/ppmtowinicon.c b/converter/ppm/ppmtowinicon.c
  616. index c673798..af2b445 100644
  617. --- a/converter/ppm/ppmtowinicon.c
  618. +++ b/converter/ppm/ppmtowinicon.c
  619. @@ -12,6 +12,7 @@
  620. #include <math.h>
  621. #include <string.h>
  622. +#include <stdlib.h>
  623. #include "pm_c_util.h"
  624. #include "winico.h"
  625. @@ -214,6 +215,7 @@ createAndBitmap (gray ** const ba, int const cols, int const rows,
  626. MALLOCARRAY_NOFAIL(rowData, rows);
  627. icBitmap->xBytes = xBytes;
  628. icBitmap->data = rowData;
  629. + overflow2(xBytes, rows);
  630. icBitmap->size = xBytes * rows;
  631. for (y=0;y<rows;y++) {
  632. u1 * row;
  633. @@ -342,6 +344,7 @@ create4Bitmap (pixel ** const pa, int const cols, int const rows,
  634. MALLOCARRAY_NOFAIL(rowData, rows);
  635. icBitmap->xBytes = xBytes;
  636. icBitmap->data = rowData;
  637. + overflow2(xBytes, rows);
  638. icBitmap->size = xBytes * rows;
  639. for (y=0;y<rows;y++) {
  640. @@ -402,6 +405,7 @@ create8Bitmap (pixel ** const pa, int const cols, int const rows,
  641. MALLOCARRAY_NOFAIL(rowData, rows);
  642. icBitmap->xBytes = xBytes;
  643. icBitmap->data = rowData;
  644. + overflow2(xBytes, rows);
  645. icBitmap->size = xBytes * rows;
  646. for (y=0;y<rows;y++) {
  647. @@ -709,7 +713,11 @@ addEntryToIcon(MS_Ico const MSIconData,
  648. entry->bitcount = bpp;
  649. entry->ih = createInfoHeader(entry, xorBitmap, andBitmap);
  650. entry->colors = palette->colors;
  651. - entry->size_in_bytes =
  652. + overflow2(4, entry->color_count);
  653. + overflow_add(xorBitmap->size, andBitmap->size);
  654. + overflow_add(xorBitmap->size + andBitmap->size, 40);
  655. + overflow_add(xorBitmap->size + andBitmap->size + 40, 4 * entry->color_count);
  656. + entry->size_in_bytes =
  657. xorBitmap->size + andBitmap->size + 40 + (4 * entry->color_count);
  658. if (verbose)
  659. pm_message("entry->size_in_bytes = %d + %d + %d = %d",
  660. diff --git a/converter/ppm/ppmtoxpm.c b/converter/ppm/ppmtoxpm.c
  661. index 38d9997..904c98d 100644
  662. --- a/converter/ppm/ppmtoxpm.c
  663. +++ b/converter/ppm/ppmtoxpm.c
  664. @@ -197,6 +197,7 @@ genNumstr(unsigned int const input, int const digits) {
  665. unsigned int i;
  666. /* Allocate memory for printed number. Abort if error. */
  667. + overflow_add(digits, 1);
  668. if (!(str = (char *) malloc(digits + 1)))
  669. pm_error("out of memory");
  670. @@ -314,6 +315,7 @@ genCmap(colorhist_vector const chv,
  671. unsigned int charsPerPixel;
  672. unsigned int xpmMaxval;
  673. + if (includeTransparent) overflow_add(ncolors, 1);
  674. MALLOCARRAY(cmap, cmapSize);
  675. if (cmapP == NULL)
  676. pm_error("Out of memory allocating %u bytes for a color map.",
  677. diff --git a/converter/ppm/qrttoppm.c b/converter/ppm/qrttoppm.c
  678. index 935463e..653084c 100644
  679. --- a/converter/ppm/qrttoppm.c
  680. +++ b/converter/ppm/qrttoppm.c
  681. @@ -46,7 +46,7 @@ main( argc, argv )
  682. ppm_writeppminit( stdout, cols, rows, maxval, 0 );
  683. pixelrow = ppm_allocrow( cols );
  684. - buf = (unsigned char *) malloc( 3 * cols );
  685. + buf = (unsigned char *) malloc2( 3 , cols );
  686. if ( buf == (unsigned char *) 0 )
  687. pm_error( "out of memory" );
  688. diff --git a/converter/ppm/sldtoppm.c b/converter/ppm/sldtoppm.c
  689. index 6ba4cb4..fc6a498 100644
  690. --- a/converter/ppm/sldtoppm.c
  691. +++ b/converter/ppm/sldtoppm.c
  692. @@ -464,6 +464,8 @@ slider(slvecfn slvec,
  693. /* Allocate image buffer and clear it to black. */
  694. + overflow_add(ixdots, 1);
  695. + overflow_add(iydots, 1);
  696. pixels = ppm_allocarray(pixcols = ixdots + 1, pixrows = iydots + 1);
  697. PPM_ASSIGN(rgbcolor, 0, 0, 0);
  698. ppmd_filledrectangle(pixels, pixcols, pixrows, pixmaxval, 0, 0,
  699. diff --git a/converter/ppm/ximtoppm.c b/converter/ppm/ximtoppm.c
  700. index ce5e639..a39b689 100644
  701. --- a/converter/ppm/ximtoppm.c
  702. +++ b/converter/ppm/ximtoppm.c
  703. @@ -117,6 +117,7 @@ ReadXimHeader(FILE * const in_fp,
  704. header->bits_channel = atoi(a_head.bits_per_channel);
  705. header->alpha_flag = atoi(a_head.alpha_channel);
  706. if (strlen(a_head.author)) {
  707. + overflow_add(strlen(a_head.author),1);
  708. if (!(header->author = calloc((unsigned int)strlen(a_head.author)+1,
  709. 1))) {
  710. pm_message("ReadXimHeader: can't calloc author string" );
  711. @@ -126,6 +127,7 @@ ReadXimHeader(FILE * const in_fp,
  712. strncpy(header->author, a_head.author, strlen(a_head.author));
  713. }
  714. if (strlen(a_head.date)) {
  715. + overflow_add(strlen(a_head.date),1);
  716. if (!(header->date =calloc((unsigned int)strlen(a_head.date)+1,1))){
  717. pm_message("ReadXimHeader: can't calloc date string" );
  718. return(0);
  719. @@ -134,6 +136,7 @@ ReadXimHeader(FILE * const in_fp,
  720. strncpy(header->date, a_head.date, strlen(a_head.date));
  721. }
  722. if (strlen(a_head.program)) {
  723. + overflow_add(strlen(a_head.program),1);
  724. if (!(header->program = calloc(
  725. (unsigned int)strlen(a_head.program) + 1, 1))) {
  726. pm_message("ReadXimHeader: can't calloc program string" );
  727. @@ -160,6 +163,7 @@ ReadXimHeader(FILE * const in_fp,
  728. if (header->nchannels == 3 && header->bits_channel == 8)
  729. header->ncolors = 0;
  730. else if (header->nchannels == 1 && header->bits_channel == 8) {
  731. + overflow2(header->ncolors, sizeof(Color));
  732. header->colors = (Color *)calloc((unsigned int)header->ncolors,
  733. sizeof(Color));
  734. if (header->colors == NULL) {
  735. diff --git a/editor/pamcut.c b/editor/pamcut.c
  736. index 7c41af3..72df687 100644
  737. --- a/editor/pamcut.c
  738. +++ b/editor/pamcut.c
  739. @@ -655,6 +655,8 @@ cutOneImage(FILE * const ifP,
  740. outpam = inpam; /* Initial value -- most fields should be same */
  741. outpam.file = ofP;
  742. + overflow_add(rightcol, 1);
  743. + overflow_add(bottomrow, 1);
  744. outpam.width = rightcol - leftcol + 1;
  745. outpam.height = bottomrow - toprow + 1;
  746. diff --git a/editor/pbmreduce.c b/editor/pbmreduce.c
  747. index f49c8d9..580e5e0 100644
  748. --- a/editor/pbmreduce.c
  749. +++ b/editor/pbmreduce.c
  750. @@ -94,6 +94,7 @@ main( argc, argv )
  751. if (halftone == QT_FS) {
  752. unsigned int col;
  753. /* Initialize Floyd-Steinberg. */
  754. + overflow_add(newcols, 2);
  755. MALLOCARRAY(thiserr, newcols + 2);
  756. MALLOCARRAY(nexterr, newcols + 2);
  757. if (thiserr == NULL || nexterr == NULL)
  758. diff --git a/editor/pnmgamma.c b/editor/pnmgamma.c
  759. index b357b0d..ec612d3 100644
  760. --- a/editor/pnmgamma.c
  761. +++ b/editor/pnmgamma.c
  762. @@ -596,6 +596,7 @@ createGammaTables(enum transferFunction const transferFunction,
  763. xelval ** const btableP) {
  764. /* Allocate space for the tables. */
  765. + overflow_add(maxval, 1);
  766. MALLOCARRAY(*rtableP, maxval+1);
  767. MALLOCARRAY(*gtableP, maxval+1);
  768. MALLOCARRAY(*btableP, maxval+1);
  769. diff --git a/editor/pnmhisteq.c b/editor/pnmhisteq.c
  770. index 8af4201..0c8d6e5 100644
  771. --- a/editor/pnmhisteq.c
  772. +++ b/editor/pnmhisteq.c
  773. @@ -107,6 +107,7 @@ computeLuminosityHistogram(xel * const * const xels,
  774. unsigned int pixelCount;
  775. unsigned int * lumahist;
  776. + overflow_add(maxval, 1);
  777. MALLOCARRAY(lumahist, maxval + 1);
  778. if (lumahist == NULL)
  779. pm_error("Out of storage allocating array for %u histogram elements",
  780. diff --git a/editor/pnmindex.csh b/editor/pnmindex.csh
  781. index c6f1e84..c513a84 100755
  782. --- a/editor/pnmindex.csh
  783. +++ b/editor/pnmindex.csh
  784. @@ -1,5 +1,7 @@
  785. #!/bin/csh -f
  786. #
  787. +echo "Unsafe code, needs debugging, do not ship"
  788. +exit 1
  789. # pnmindex - build a visual index of a bunch of anymaps
  790. #
  791. # Copyright (C) 1991 by Jef Poskanzer.
  792. diff --git a/editor/pnmpad.c b/editor/pnmpad.c
  793. index 1904b68..0797cf1 100644
  794. --- a/editor/pnmpad.c
  795. +++ b/editor/pnmpad.c
  796. @@ -527,6 +527,8 @@ main(int argc, const char ** argv) {
  797. computePadSizes(cmdline, cols, rows, &lpad, &rpad, &tpad, &bpad);
  798. + overflow_add(cols, lpad);
  799. + overflow_add(cols + lpad, rpad);
  800. newcols = cols + lpad + rpad;
  801. if (PNM_FORMAT_TYPE(format) == PBM_TYPE)
  802. diff --git a/editor/pnmremap.c b/editor/pnmremap.c
  803. index b2448cb..b924120 100644
  804. --- a/editor/pnmremap.c
  805. +++ b/editor/pnmremap.c
  806. @@ -428,6 +428,7 @@ initFserr(struct pam * const pamP,
  807. unsigned int const fserrSize = pamP->width + 2;
  808. + overflow_add(pamP->width, 2);
  809. fserrP->width = pamP->width;
  810. MALLOCARRAY(fserrP->thiserr, pamP->depth);
  811. @@ -465,6 +466,7 @@ floydInitRow(struct pam * const pamP, struct fserr * const fserrP) {
  812. int col;
  813. + overflow_add(pamP->width, 2);
  814. for (col = 0; col < pamP->width + 2; ++col) {
  815. unsigned int plane;
  816. for (plane = 0; plane < pamP->depth; ++plane)
  817. diff --git a/editor/pnmscalefixed.c b/editor/pnmscalefixed.c
  818. index 884ca31..747cd8f 100644
  819. --- a/editor/pnmscalefixed.c
  820. +++ b/editor/pnmscalefixed.c
  821. @@ -214,6 +214,7 @@ compute_output_dimensions(const struct cmdline_info cmdline,
  822. const int rows, const int cols,
  823. int * newrowsP, int * newcolsP) {
  824. + overflow2(rows, cols);
  825. if (cmdline.pixels) {
  826. if (rows * cols <= cmdline.pixels) {
  827. *newrowsP = rows;
  828. @@ -265,6 +266,8 @@ compute_output_dimensions(const struct cmdline_info cmdline,
  829. if (*newcolsP < 1) *newcolsP = 1;
  830. if (*newrowsP < 1) *newrowsP = 1;
  831. +
  832. + overflow2(*newcolsP, *newrowsP);
  833. }
  834. @@ -446,6 +449,9 @@ main(int argc, char **argv ) {
  835. unfilled. We can address that by stretching, whereas the other
  836. case would require throwing away some of the input.
  837. */
  838. +
  839. + overflow2(newcols, SCALE);
  840. + overflow2(newrows, SCALE);
  841. sxscale = SCALE * newcols / cols;
  842. syscale = SCALE * newrows / rows;
  843. diff --git a/editor/ppmdither.c b/editor/ppmdither.c
  844. index ec1b977..e701e09 100644
  845. --- a/editor/ppmdither.c
  846. +++ b/editor/ppmdither.c
  847. @@ -356,6 +356,11 @@ dithMatrix(unsigned int const dithPower) {
  848. (dithDim * sizeof(*dithMat)) + /* pointers */
  849. (dithDim * dithDim * sizeof(**dithMat)); /* data */
  850. +
  851. + overflow2(dithDim, sizeof(*dithMat));
  852. + overflow3(dithDim, dithDim, sizeof(**dithMat));
  853. + overflow_add(dithDim * sizeof(*dithMat), dithDim * dithDim * sizeof(**dithMat));
  854. +
  855. dithMat = malloc(dithMatSize);
  856. if (dithMat == NULL)
  857. diff --git a/editor/specialty/pamoil.c b/editor/specialty/pamoil.c
  858. index 6cb8d3a..6f4bde9 100644
  859. --- a/editor/specialty/pamoil.c
  860. +++ b/editor/specialty/pamoil.c
  861. @@ -112,6 +112,7 @@ main(int argc, char *argv[] ) {
  862. tuples = pnm_readpam(ifp, &inpam, PAM_STRUCT_SIZE(tuple_type));
  863. pm_close(ifp);
  864. + overflow_add(inpam.maxval, 1);
  865. MALLOCARRAY(hist, inpam.maxval + 1);
  866. if (hist == NULL)
  867. pm_error("Unable to allocate memory for histogram.");
  868. diff --git a/generator/pbmtext.c b/generator/pbmtext.c
  869. index 9f4366d..445d718 100644
  870. --- a/generator/pbmtext.c
  871. +++ b/generator/pbmtext.c
  872. @@ -96,12 +96,14 @@ parseCommandLine(int argc, const char ** argv,
  873. for (i = 1; i < argc; ++i) {
  874. if (i > 1) {
  875. + overflow_add(totaltextsize, 1);
  876. totaltextsize += 1;
  877. text = realloc(text, totaltextsize);
  878. if (text == NULL)
  879. pm_error("out of memory allocating space for input text");
  880. strcat(text, " ");
  881. - }
  882. + }
  883. + overflow_add(totaltextsize, strlen(argv[i]));
  884. totaltextsize += strlen(argv[i]);
  885. text = realloc(text, totaltextsize);
  886. if (text == NULL)
  887. @@ -712,6 +714,7 @@ getText(const char cmdline_text[],
  888. pm_error("A line of input text is longer than %u characters."
  889. "Cannot process.", (unsigned)sizeof(buf)-1);
  890. if (lineCount >= maxlines) {
  891. + overflow2(maxlines, 2);
  892. maxlines *= 2;
  893. REALLOCARRAY(text_array, maxlines);
  894. if (text_array == NULL)
  895. @@ -832,6 +835,7 @@ main(int argc, const char *argv[]) {
  896. hmargin = fontP->maxwidth;
  897. } else {
  898. vmargin = fontP->maxheight;
  899. + overflow2(2, fontP->maxwidth);
  900. hmargin = 2 * fontP->maxwidth;
  901. }
  902. }
  903. diff --git a/lib/libpam.c b/lib/libpam.c
  904. index cc6368e..4e10572 100644
  905. --- a/lib/libpam.c
  906. +++ b/lib/libpam.c
  907. @@ -224,8 +224,9 @@ allocPamRow(const struct pam * const pamP) {
  908. unsigned int const bytesPerTuple = allocationDepth(pamP) * sizeof(sample);
  909. tuple * tuplerow;
  910. - tuplerow = malloc(pamP->width * (sizeof(tuple *) + bytesPerTuple));
  911. -
  912. + overflow_add(sizeof(tuple *), bytesPerTuple);
  913. + tuplerow = malloc2(pamP->width, (sizeof(tuple *) + bytesPerTuple));
  914. +
  915. if (tuplerow != NULL) {
  916. /* Now we initialize the pointers to the individual tuples
  917. to make this a regulation C two dimensional array.
  918. diff --git a/lib/libpammap.c b/lib/libpammap.c
  919. index 55e1d3f..04b1ba3 100644
  920. --- a/lib/libpammap.c
  921. +++ b/lib/libpammap.c
  922. @@ -108,7 +108,9 @@ allocTupleIntListItem(struct pam * const pamP) {
  923. */
  924. struct tupleint_list_item * retval;
  925. - unsigned int const size =
  926. + overflow2(pamP->depth, sizeof(sample));
  927. + overflow_add(sizeof(*retval)-sizeof(retval->tupleint.tuple), pamP->depth*sizeof(sample));
  928. + unsigned int const size =
  929. sizeof(*retval) - sizeof(retval->tupleint.tuple)
  930. + pamP->depth * sizeof(sample);
  931. diff --git a/lib/libpm.c b/lib/libpm.c
  932. index 4374bbe..5ab7f83 100644
  933. --- a/lib/libpm.c
  934. +++ b/lib/libpm.c
  935. @@ -841,5 +841,53 @@ pm_parse_height(const char * const arg) {
  936. return height;
  937. }
  938. +/*
  939. + * Maths wrapping
  940. + */
  941. +void __overflow2(int a, int b)
  942. +{
  943. + if(a < 0 || b < 0)
  944. + pm_error("object too large");
  945. + if(b == 0)
  946. + return;
  947. + if(a > INT_MAX / b)
  948. + pm_error("object too large");
  949. +}
  950. +
  951. +void overflow3(int a, int b, int c)
  952. +{
  953. + overflow2(a,b);
  954. + overflow2(a*b, c);
  955. +}
  956. +
  957. +void overflow_add(int a, int b)
  958. +{
  959. + if( a > INT_MAX - b)
  960. + pm_error("object too large");
  961. +}
  962. +
  963. +void *malloc2(int a, int b)
  964. +{
  965. + overflow2(a, b);
  966. + if(a*b == 0)
  967. + pm_error("Zero byte allocation");
  968. + return malloc(a*b);
  969. +}
  970. +
  971. +void *malloc3(int a, int b, int c)
  972. +{
  973. + overflow3(a, b, c);
  974. + if(a*b*c == 0)
  975. + pm_error("Zero byte allocation");
  976. + return malloc(a*b*c);
  977. +}
  978. +
  979. +void *realloc2(void * a, int b, int c)
  980. +{
  981. + overflow2(b, c);
  982. + if(b*c == 0)
  983. + pm_error("Zero byte allocation");
  984. + return realloc(a, b*c);
  985. +}
  986. diff --git a/lib/pm.h b/lib/pm.h
  987. index e9f1405..da54391 100644
  988. --- a/lib/pm.h
  989. +++ b/lib/pm.h
  990. @@ -435,5 +435,12 @@ pm_parse_height(const char * const arg);
  991. }
  992. #endif
  993. +void *malloc2(int, int);
  994. +void *malloc3(int, int, int);
  995. +#define overflow2(a,b) __overflow2(a,b)
  996. +void __overflow2(int, int);
  997. +void overflow3(int, int, int);
  998. +void overflow_add(int, int);
  999. +
  1000. #endif
  1001. diff --git a/other/pnmcolormap.c b/other/pnmcolormap.c
  1002. index 57db432..7195295 100644
  1003. --- a/other/pnmcolormap.c
  1004. +++ b/other/pnmcolormap.c
  1005. @@ -840,6 +840,7 @@ colormapToSquare(struct pam * const pamP,
  1006. pamP->width = intsqrt;
  1007. else
  1008. pamP->width = intsqrt + 1;
  1009. + overflow_add(intsqrt, 1);
  1010. }
  1011. {
  1012. unsigned int const intQuotient = colormap.size / pamP->width;
  1013. diff --git a/urt/Runput.c b/urt/Runput.c
  1014. index 3bc562a..645a376 100644
  1015. --- a/urt/Runput.c
  1016. +++ b/urt/Runput.c
  1017. @@ -202,10 +202,11 @@ RunSetup(rle_hdr * the_hdr)
  1018. if ( the_hdr->background != 0 )
  1019. {
  1020. register int i;
  1021. - register rle_pixel *background =
  1022. - (rle_pixel *)malloc( (unsigned)(the_hdr->ncolors + 1) );
  1023. - register int *bg_color;
  1024. - /*
  1025. + register rle_pixel *background;
  1026. + register int *bg_color;
  1027. +
  1028. + overflow_add(the_hdr->ncolors,1);
  1029. + background = (rle_pixel *)malloc( (unsigned)(the_hdr->ncolors + 1) ); /*
  1030. * If even number of bg color bytes, put out one more to get to
  1031. * 16 bit boundary.
  1032. */
  1033. @@ -224,7 +225,7 @@ RunSetup(rle_hdr * the_hdr)
  1034. /* Big-endian machines are harder */
  1035. register int i, nmap = (1 << the_hdr->cmaplen) *
  1036. the_hdr->ncmap;
  1037. - register char *h_cmap = (char *)malloc( nmap * 2 );
  1038. + register char *h_cmap = (char *)malloc2( nmap, 2 );
  1039. if ( h_cmap == NULL )
  1040. {
  1041. fprintf( stderr,
  1042. diff --git a/urt/rle.h b/urt/rle.h
  1043. index 8d72cb9..ac65b94 100644
  1044. --- a/urt/rle.h
  1045. +++ b/urt/rle.h
  1046. @@ -161,6 +161,17 @@ rle_hdr /* End of typedef. */
  1047. */
  1048. extern rle_hdr rle_dflt_hdr;
  1049. +/*
  1050. + * Provided by pm library
  1051. + */
  1052. +
  1053. +extern void overflow_add(int, int);
  1054. +#define overflow2(a,b) __overflow2(a,b)
  1055. +extern void __overflow2(int, int);
  1056. +extern void overflow3(int, int, int);
  1057. +extern void *malloc2(int, int);
  1058. +extern void *malloc3(int, int, int);
  1059. +extern void *realloc2(void *, int, int);
  1060. /* Declare RLE library routines. */
  1061. diff --git a/urt/rle_addhist.c b/urt/rle_addhist.c
  1062. index b165175..e09ed94 100644
  1063. --- a/urt/rle_addhist.c
  1064. +++ b/urt/rle_addhist.c
  1065. @@ -70,13 +70,18 @@ rle_addhist(char * argv[],
  1066. return;
  1067. length = 0;
  1068. - for (i = 0; argv[i]; ++i)
  1069. + for (i = 0; argv[i]; ++i) {
  1070. + overflow_add(length, strlen(argv[i]));
  1071. + overflow_add(length+1, strlen(argv[i]));
  1072. length += strlen(argv[i]) +1; /* length of each arg plus space. */
  1073. + }
  1074. time(&temp);
  1075. timedate = ctime(&temp);
  1076. length += strlen(timedate); /* length of date and time in ASCII. */
  1077. -
  1078. + overflow_add(strlen(padding), 4);
  1079. + overflow_add(strlen(histoire), strlen(padding) + 4);
  1080. + overflow_add(length, strlen(histoire) + strlen(padding) + 4);
  1081. length += strlen(padding) + 3 + strlen(histoire) + 1;
  1082. /* length of padding, "on " and length of history name plus "="*/
  1083. if (in_hdr) /* if we are interested in the old comments... */
  1084. @@ -84,8 +89,10 @@ rle_addhist(char * argv[],
  1085. else
  1086. old = NULL;
  1087. - if (old && *old)
  1088. + if (old && *old) {
  1089. + overflow_add(length, strlen(old));
  1090. length += strlen(old); /* add length if there. */
  1091. + }
  1092. ++length; /*Cater for the null. */
  1093. diff --git a/urt/rle_getrow.c b/urt/rle_getrow.c
  1094. index bd7d1c8..bd05698 100644
  1095. --- a/urt/rle_getrow.c
  1096. +++ b/urt/rle_getrow.c
  1097. @@ -168,6 +168,7 @@ rle_get_setup(rle_hdr * const the_hdr) {
  1098. char * cp;
  1099. VAXSHORT(comlen, infile); /* get comment length */
  1100. + overflow_add(comlen, 1);
  1101. evenlen = (comlen + 1) & ~1; /* make it even */
  1102. if (evenlen) {
  1103. MALLOCARRAY(comment_buf, evenlen);
  1104. diff --git a/urt/rle_hdr.c b/urt/rle_hdr.c
  1105. index 1611324..7c9c010 100644
  1106. --- a/urt/rle_hdr.c
  1107. +++ b/urt/rle_hdr.c
  1108. @@ -80,7 +80,10 @@ int img_num;
  1109. /* Fill in with copies of the strings. */
  1110. if ( the_hdr->cmd != pgmname )
  1111. {
  1112. - char *tmp = (char *)malloc( strlen( pgmname ) + 1 );
  1113. + char *tmp;
  1114. +
  1115. + overflow_add(strlen(pgmname), 1);
  1116. + tmp = malloc( strlen(pgmname) + 1 );
  1117. RLE_CHECK_ALLOC( pgmname, tmp, 0 );
  1118. strcpy( tmp, pgmname );
  1119. the_hdr->cmd = tmp;
  1120. @@ -88,8 +91,10 @@ int img_num;
  1121. if ( the_hdr->file_name != fname )
  1122. {
  1123. - char *tmp = (char *)malloc( strlen( fname ) + 1 );
  1124. - RLE_CHECK_ALLOC( pgmname, tmp, 0 );
  1125. + char *tmp;
  1126. + overflow_add(strlen(fname), 1);
  1127. + tmp = malloc( strlen( fname ) + 1 );
  1128. + RLE_CHECK_ALLOC( pgmname, tmp, 0 );
  1129. strcpy( tmp, fname );
  1130. the_hdr->file_name = tmp;
  1131. }
  1132. @@ -153,6 +158,7 @@ rle_hdr *from_hdr, *to_hdr;
  1133. if ( to_hdr->bg_color )
  1134. {
  1135. int size = to_hdr->ncolors * sizeof(int);
  1136. + overflow2(to_hdr->ncolors, sizeof(int));
  1137. to_hdr->bg_color = (int *)malloc( size );
  1138. RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->bg_color, "background color" );
  1139. memcpy( to_hdr->bg_color, from_hdr->bg_color, size );
  1140. @@ -161,7 +167,7 @@ rle_hdr *from_hdr, *to_hdr;
  1141. if ( to_hdr->cmap )
  1142. {
  1143. int size = to_hdr->ncmap * (1 << to_hdr->cmaplen) * sizeof(rle_map);
  1144. - to_hdr->cmap = (rle_map *)malloc( size );
  1145. + to_hdr->cmap = (rle_map *)malloc3( to_hdr->ncmap, 1<<to_hdr->cmaplen, sizeof(rle_map));
  1146. RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->cmap, "color map" );
  1147. memcpy( to_hdr->cmap, from_hdr->cmap, size );
  1148. }
  1149. @@ -173,12 +179,17 @@ rle_hdr *from_hdr, *to_hdr;
  1150. {
  1151. int size = 0;
  1152. CONST_DECL char **cp;
  1153. - for ( cp=to_hdr->comments; *cp; cp++ )
  1154. + for ( cp=to_hdr->comments; *cp; cp++ )
  1155. + {
  1156. + overflow_add(size, 1);
  1157. size++; /* Count the comments. */
  1158. + }
  1159. /* Check if there are really any comments. */
  1160. if ( size )
  1161. {
  1162. + overflow_add(size, 1);
  1163. size++; /* Copy the NULL pointer, too. */
  1164. + overflow2(size, sizeof(char *));
  1165. size *= sizeof(char *);
  1166. to_hdr->comments = (CONST_DECL char **)malloc( size );
  1167. RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->comments, "comments" );
  1168. diff --git a/urt/rle_open_f.c b/urt/rle_open_f.c
  1169. index ae8548b..c2ef37d 100644
  1170. --- a/urt/rle_open_f.c
  1171. +++ b/urt/rle_open_f.c
  1172. @@ -163,65 +163,7 @@ dealWithSubprocess(const char * const file_name,
  1173. FILE ** const fpP,
  1174. bool * const noSubprocessP,
  1175. const char ** const errorP) {
  1176. -
  1177. -#ifdef NO_OPEN_PIPES
  1178. *noSubprocessP = TRUE;
  1179. -#else
  1180. - const char *cp;
  1181. -
  1182. - reapChildren(catchingChildrenP, pids);
  1183. -
  1184. - /* Real file, not stdin or stdout. If name ends in ".Z",
  1185. - * pipe from/to un/compress (depending on r/w mode).
  1186. - *
  1187. - * If it starts with "|", popen that command.
  1188. - */
  1189. -
  1190. - cp = file_name + strlen(file_name) - 2;
  1191. - /* Pipe case. */
  1192. - if (file_name[0] == '|') {
  1193. - pid_t thepid; /* PID from my_popen */
  1194. -
  1195. - *noSubprocessP = FALSE;
  1196. -
  1197. - *fpP = my_popen(file_name + 1, mode, &thepid);
  1198. - if (*fpP == NULL)
  1199. - *errorP = "%s: can't invoke <<%s>> for %s: ";
  1200. - else {
  1201. - /* One more child to catch, eventually. */
  1202. - if (*catchingChildrenP < MAX_CHILDREN)
  1203. - pids[(*catchingChildrenP)++] = thepid;
  1204. - }
  1205. - } else if (cp > file_name && *cp == '.' && *(cp + 1) == 'Z' ) {
  1206. - /* Compress case. */
  1207. - pid_t thepid; /* PID from my_popen. */
  1208. - const char * command;
  1209. -
  1210. - *noSubprocessP = FALSE;
  1211. -
  1212. - if (*mode == 'w')
  1213. - pm_asprintf(&command, "compress > %s", file_name);
  1214. - else if (*mode == 'a')
  1215. - pm_asprintf(&command, "compress >> %s", file_name);
  1216. - else
  1217. - pm_asprintf(&command, "compress -d < %s", file_name);
  1218. -
  1219. - *fpP = my_popen(command, mode, &thepid);
  1220. -
  1221. - if (*fpP == NULL)
  1222. - *errorP = "%s: can't invoke 'compress' program, "
  1223. - "trying to open %s for %s";
  1224. - else {
  1225. - /* One more child to catch, eventually. */
  1226. - if (*catchingChildrenP < MAX_CHILDREN)
  1227. - pids[(*catchingChildrenP)++] = thepid;
  1228. - }
  1229. - pm_strfree(command);
  1230. - } else {
  1231. - *noSubprocessP = TRUE;
  1232. - *errorP = NULL;
  1233. - }
  1234. -#endif
  1235. }
  1236. diff --git a/urt/rle_putcom.c b/urt/rle_putcom.c
  1237. index ab2eb20..f6a6ff7 100644
  1238. --- a/urt/rle_putcom.c
  1239. +++ b/urt/rle_putcom.c
  1240. @@ -98,12 +98,14 @@ rle_putcom(const char * const value,
  1241. const char * v;
  1242. const char ** old_comments;
  1243. int i;
  1244. - for (i = 2, cp = the_hdr->comments; *cp != NULL; ++i, ++cp)
  1245. + for (i = 2, cp = the_hdr->comments; *cp != NULL; ++i, ++cp) {
  1246. + overflow_add(i, 1);
  1247. if (match(value, *cp) != NULL) {
  1248. v = *cp;
  1249. *cp = value;
  1250. return v;
  1251. }
  1252. + }
  1253. /* Not found */
  1254. /* Can't realloc because somebody else might be pointing to this
  1255. * comments block. Of course, if this were true, then the
  1256. diff --git a/urt/scanargs.c b/urt/scanargs.c
  1257. index f3af334..5e114bb 100644
  1258. --- a/urt/scanargs.c
  1259. +++ b/urt/scanargs.c
  1260. @@ -62,9 +62,8 @@ typedef int *ptr;
  1261. /*
  1262. * Storage allocation macros
  1263. */
  1264. -#define NEW( type, cnt ) (type *) malloc( (cnt) * sizeof( type ) )
  1265. -#define RENEW( type, ptr, cnt ) (type *) realloc( ptr, (cnt) * sizeof( type ) )
  1266. -
  1267. +#define NEW( type, cnt ) (type *) malloc2( (cnt) , sizeof( type ) )
  1268. +#define RENEW( type, ptr, cnt ) (type *) realloc2( ptr, (cnt), sizeof( type ) )
  1269. static CONST_DECL char * prformat( CONST_DECL char *, int );
  1270. static int isnum( CONST_DECL char *, int, int );
  1271. static int _do_scanargs( int argc, char **argv, CONST_DECL char *format,