2020#include <stdlib.h>
2121#include <stdio.h>
2222#include <string.h>
23+ #include <stdint.h>
2324#ifdef _WIN32
2425 #include <io.h>
2526#else
@@ -139,7 +140,7 @@ int loadppm(int *fd, unsigned char **buf, int *w, int *h,
139140 if ((* w )< 1 || (* h )< 1 || scalefactor < 1 ) _throw ("Corrupt PPM header" );
140141
141142 dstpitch = (((* w )* ps [f ])+ (align - 1 ))& (~(align - 1 ));
142- if ((* buf = (unsigned char * )malloc (dstpitch * ( * h ) ))== NULL )
143+ if ((* buf = (unsigned char * )calloc (dstpitch , * h ))== NULL )
143144 _throw ("Memory allocation error" );
144145 if (ascii )
145146 {
@@ -159,7 +160,7 @@ int loadppm(int *fd, unsigned char **buf, int *w, int *h,
159160 {
160161 if (scalefactor != 255 )
161162 _throw ("Binary PPMs must have 8-bit components" );
162- if ((tempbuf = (unsigned char * )malloc (( * w ) * (* h )* 3 ))== NULL )
163+ if (* h > SIZE_MAX / 3 || (tempbuf = (unsigned char * )calloc ( * w , (* h )* 3 ))== NULL )
163164 _throw ("Memory allocation error" );
164165 if (fread (tempbuf , (* w )* (* h )* 3 , 1 , fs )!= 1 ) _throw ("Read error" );
165166 pixelconvert (tempbuf , BMP_RGB , (* w )* 3 , * buf , f , dstpitch , * w , * h , dstbottomup );
@@ -249,8 +250,8 @@ int loadbmp(char *filename, unsigned char **buf, int *w, int *h,
249250 dstpitch = (((* w )* ps [f ])+ (align - 1 ))& (~(align - 1 ));
250251
251252 if (srcpitch * (* h )+ bh .bfOffBits != bh .bfSize ) _throw ("Corrupt bitmap header" );
252- if ((tempbuf = (unsigned char * )malloc (srcpitch * ( * h ) ))== NULL
253- || (* buf = (unsigned char * )malloc (dstpitch * ( * h ) ))== NULL )
253+ if ((tempbuf = (unsigned char * )calloc (srcpitch , * h ))== NULL
254+ || (* buf = (unsigned char * )calloc (dstpitch , * h ))== NULL )
254255 _throw ("Memory allocation error" );
255256 if (lseek (fd , (long )bh .bfOffBits , SEEK_SET )!= (long )bh .bfOffBits )
256257 _throw (strerror (errno ));
0 commit comments