netpbm-security-code.patch 48 KB

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