348bc06
diff -Naur blender-2.68a-original/source/blender/avi/CMakeLists.txt blender-2.68a/source/blender/avi/CMakeLists.txt
348bc06
--- blender-2.68a-original/source/blender/avi/CMakeLists.txt	2012-10-07 02:27:31.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/avi/CMakeLists.txt	2022-04-04 07:18:55.810153370 -0400
348bc06
@@ -26,6 +26,7 @@
348bc06
 set(INC 
348bc06
 	.
348bc06
 	../blenlib
348bc06
+	../imbuf
348bc06
 	../../../intern/guardedalloc
348bc06
 )
348bc06
 
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi.c blender-2.68a/source/blender/avi/intern/avi.c
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi.c	2013-05-28 15:35:26.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi.c	2022-04-04 08:51:43.768676218 -0400
348bc06
@@ -285,13 +285,15 @@
348bc06
 
348bc06
 	fseek(movie.fp, movie.header->size - 14 * 4, SEEK_CUR);
348bc06
 
348bc06
-	if (movie.header->Streams < 1) {
348bc06
-		DEBUG_PRINT("streams less than 1\n");
348bc06
+	/* Limit number of streams to some reasonable amount to prevent
348bc06
+	 * buffer oveflow vulnerabilities. */
348bc06
+	if (movie.header->Streams < 1 || movie.header->Streams > 65536) {
348bc06
+		DEBUG_PRINT("Number of streams should be in range 1-65536\n");
348bc06
 		fclose(movie.fp);
348bc06
 		return 0;
348bc06
 	}
348bc06
 	
348bc06
-	movie.streams = (AviStreamRec *) MEM_callocN(sizeof(AviStreamRec) * movie.header->Streams, "moviestreams");
348bc06
+	movie.streams = (AviStreamRec *) MEM_calloc_arrayN(movie.header->Streams, sizeof(AviStreamRec), "moviestreams");
348bc06
 
348bc06
 	for (temp = 0; temp < movie.header->Streams; temp++) {
348bc06
 
348bc06
@@ -486,12 +488,14 @@
348bc06
 
348bc06
 	fseek(movie->fp, movie->header->size - 14 * 4, SEEK_CUR);
348bc06
 
348bc06
-	if (movie->header->Streams < 1) {
348bc06
-		DEBUG_PRINT("streams less than 1\n");
348bc06
+	/* Limit number of streams to some reasonable amount to prevent
348bc06
+	 * buffer oveflow vulnerabilities. */
348bc06
+	if (movie->header->Streams < 1 || movie->header->Streams > 65536) {
348bc06
+		DEBUG_PRINT("Number of streams should be in range 1-65536\n");
348bc06
 		return AVI_ERROR_FORMAT;
348bc06
 	}
348bc06
 	
348bc06
-	movie->streams = (AviStreamRec *) MEM_callocN(sizeof(AviStreamRec) * movie->header->Streams, "moviestreams");
348bc06
+	movie->streams = (AviStreamRec *) MEM_calloc_arrayN(movie->header->Streams, sizeof(AviStreamRec), "moviestreams");
348bc06
 
348bc06
 	for (temp = 0; temp < movie->header->Streams; temp++) {
348bc06
 
348bc06
@@ -689,7 +693,8 @@
348bc06
 
348bc06
 void *AVI_read_frame(AviMovie *movie, AviFormat format, int frame, int stream)
348bc06
 {
348bc06
-	int cur_frame = -1, temp, i = 0, rewind = 1;
348bc06
+	int cur_frame = -1, i = 0, rewind = 1;
348bc06
+	size_t size;
348bc06
 	void *buffer;
348bc06
 
348bc06
 	/* Retrieve the record number of the desired frame in the index 
348bc06
@@ -720,16 +725,16 @@
348bc06
 
348bc06
 	fseek(movie->fp, movie->read_offset + movie->entries[i - 1].Offset, SEEK_SET);
348bc06
 
348bc06
-	temp = GET_FCC(movie->fp);
348bc06
-	buffer = MEM_mallocN(temp, "readbuffer");
348bc06
+	size = GET_FCC(movie->fp);
348bc06
+	buffer = MEM_mallocN(size, "readbuffer");
348bc06
 
348bc06
-	if (fread(buffer, 1, temp, movie->fp) != temp) {
348bc06
+	if (fread(buffer, 1, size, movie->fp) != size) {
348bc06
 		MEM_freeN(buffer);
348bc06
 
348bc06
 		return NULL;
348bc06
 	}
348bc06
 	
348bc06
-	buffer = avi_format_convert(movie, stream, buffer, movie->streams[stream].format, format, &temp);
348bc06
+	buffer = avi_format_convert(movie, stream, buffer, movie->streams[stream].format, format, &size);
348bc06
 
348bc06
 	return buffer;
348bc06
 }
348bc06
@@ -801,6 +806,13 @@
348bc06
 	movie->header->Reserved[2] = 0;
348bc06
 	movie->header->Reserved[3] = 0;
348bc06
 
348bc06
+	/* Limit number of streams to some reasonable amount to prevent
348bc06
+	 * buffer oveflow vulnerabilities. */
348bc06
+	if (movie->header->Streams < 0 || movie->header->Streams > 65536) {
348bc06
+		DEBUG_PRINT("Number of streams should be in range 0-65536\n");
348bc06
+		return AVI_ERROR_FORMAT;
348bc06
+	}
348bc06
+
348bc06
 	movie->streams = (AviStreamRec *) MEM_mallocN(sizeof(AviStreamRec) * movie->header->Streams, "moviestreams");
348bc06
 
348bc06
 	va_start(ap, streams);
348bc06
@@ -962,7 +974,6 @@
348bc06
 	int64_t rec_off;
348bc06
 	AviFormat format;
348bc06
 	void *buffer;
348bc06
-	int size;
348bc06
 
348bc06
 	if (frame_num < 0)
348bc06
 		return AVI_ERROR_OPTION;
348bc06
@@ -993,6 +1004,7 @@
348bc06
 
348bc06
 	for (stream = 0; stream < movie->header->Streams; stream++) {
348bc06
 		unsigned int tbuf = 0;
348bc06
+		size_t size;
348bc06
 		
348bc06
 		format = va_arg(ap, AviFormat);
348bc06
 		buffer = va_arg(ap, void *);
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi_codecs.c blender-2.68a/source/blender/avi/intern/avi_codecs.c
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi_codecs.c	2012-10-07 02:27:31.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi_codecs.c	2022-04-04 07:23:09.248184710 -0400
348bc06
@@ -39,7 +39,7 @@
348bc06
 #include "avi_mjpeg.h"
348bc06
 #include "avi_rgb32.h"
348bc06
 
348bc06
-void *avi_format_convert(AviMovie *movie, int stream, void *buffer, AviFormat from, AviFormat to, int *size)
348bc06
+void *avi_format_convert(AviMovie *movie, int stream, void *buffer, AviFormat from, AviFormat to, size_t *size)
348bc06
 {
348bc06
 	if (from == to)
348bc06
 		return buffer;
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi_intern.h blender-2.68a/source/blender/avi/intern/avi_intern.h
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi_intern.h	2013-04-17 21:52:38.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi_intern.h	2022-04-04 07:23:24.235363969 -0400
348bc06
@@ -59,7 +59,7 @@
348bc06
 	putc(ch2[1], fp); \
348bc06
 } (void)0
348bc06
 
348bc06
-void *avi_format_convert(AviMovie *movie, int stream, void *buffer, AviFormat from, AviFormat to, int *size);
348bc06
+void *avi_format_convert(AviMovie *movie, int stream, void *buffer, AviFormat from, AviFormat to, size_t *size);
348bc06
 
348bc06
 int avi_get_data_id(AviFormat format, int stream);
348bc06
 int avi_get_format_type(AviFormat format);
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi_mjpeg.c blender-2.68a/source/blender/avi/intern/avi_mjpeg.c
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi_mjpeg.c	2012-12-23 08:57:09.000000000 -0500
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi_mjpeg.c	2022-04-04 14:04:29.614968727 -0400
348bc06
@@ -39,6 +39,8 @@
348bc06
 
348bc06
 #include "MEM_guardedalloc.h"
348bc06
 
348bc06
+#include "IMB_imbuf.h"
348bc06
+
348bc06
 #include "jpeglib.h"
348bc06
 #include "jerror.h"
348bc06
 
348bc06
@@ -46,10 +48,10 @@
348bc06
 
348bc06
 #define PADUP(num, amt) ((num + (amt - 1)) & ~(amt - 1))
348bc06
 
348bc06
-static void jpegmemdestmgr_build(j_compress_ptr cinfo, unsigned char *buffer, int bufsize);
348bc06
-static void jpegmemsrcmgr_build(j_decompress_ptr dinfo, unsigned char *buffer, int bufsize);
348bc06
+static void jpegmemdestmgr_build(j_compress_ptr cinfo, unsigned char *buffer, size_t bufsize);
348bc06
+static void jpegmemsrcmgr_build(j_decompress_ptr dinfo, unsigned char *buffer, size_t bufsize);
348bc06
 
348bc06
-static int numbytes;
348bc06
+static size_t numbytes;
348bc06
 
348bc06
 static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
348bc06
 {
348bc06
@@ -153,10 +155,9 @@
348bc06
 	               bits_ac_chrominance, val_ac_chrominance);
348bc06
 }
348bc06
 
348bc06
-static int Decode_JPEG(unsigned char *inBuffer, unsigned char *outBuffer, unsigned int width, unsigned int height, int bufsize)
348bc06
+static int Decode_JPEG(unsigned char *inBuffer, unsigned char *outBuffer, unsigned int width, unsigned int height, size_t bufsize)
348bc06
 {
348bc06
-	int rowstride;
348bc06
-	unsigned int y;
348bc06
+	size_t rowstride, y;
348bc06
 	struct jpeg_decompress_struct dinfo;
348bc06
 	struct jpeg_error_mgr jerr;
348bc06
 	
348bc06
@@ -206,10 +207,10 @@
348bc06
 	return 1;
348bc06
 }
348bc06
 
348bc06
-static void Compress_JPEG(int quality, unsigned char *outbuffer, const unsigned char *inBuffer, int width, int height, int bufsize)
348bc06
+static void Compress_JPEG(int quality, unsigned char *outbuffer, const unsigned char *inBuffer, int width, int height, size_t bufsize)
348bc06
 {
348bc06
-	int i, rowstride;
348bc06
-	unsigned int y;
348bc06
+	int i;
348bc06
+	size_t rowstride, y;
348bc06
 	struct jpeg_compress_struct cinfo;
348bc06
 	struct jpeg_error_mgr jerr;
348bc06
 	unsigned char marker[60];
348bc06
@@ -270,7 +271,7 @@
348bc06
 
348bc06
 static void interlace(unsigned char *to, unsigned char *from, int width, int height)
348bc06
 {
348bc06
-	int i, rowstride = width * 3;
348bc06
+	size_t i, rowstride = width * 3;
348bc06
 	
348bc06
 	for (i = 0; i < height; i++) {
348bc06
 		if (i & 1)
348bc06
@@ -282,7 +283,7 @@
348bc06
 
348bc06
 static void deinterlace(int odd, unsigned char *to, unsigned char *from, int width, int height)
348bc06
 {
348bc06
-	int i, rowstride = width * 3;
348bc06
+	size_t i, rowstride = width * 3;
348bc06
 	
348bc06
 	for (i = 0; i < height; i++) {
348bc06
 		if ((i & 1) == odd)
348bc06
@@ -346,22 +347,27 @@
348bc06
 	}
348bc06
 }
348bc06
 
348bc06
-void *avi_converter_from_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, int *size)
348bc06
+void *avi_converter_from_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, size_t *size)
348bc06
 {
348bc06
 	int deint;
348bc06
 	unsigned char *buf;
348bc06
 
348bc06
 	(void)stream; /* unused */
348bc06
 
348bc06
-	buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "avi.avi_converter_from_mjpeg 1");
348bc06
+	buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "avi.avi_converter_from_mjpeg 1");
348bc06
+	if (!buf) {
348bc06
+		return NULL;
348bc06
+	}
348bc06
 
348bc06
 	deint = check_and_decode_jpeg(buffer, buf, movie->header->Width, movie->header->Height, *size);
348bc06
 	
348bc06
 	MEM_freeN(buffer);
348bc06
 	
348bc06
 	if (deint) {
348bc06
-		buffer = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "avi.avi_converter_from_mjpeg 2");
348bc06
-		interlace(buffer, buf, movie->header->Width, movie->header->Height);
348bc06
+		buffer = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "avi.avi_converter_from_mjpeg 2");
348bc06
+		if (buffer) {
348bc06
+			interlace(buffer, buf, movie->header->Width, movie->header->Height);
348bc06
+		}
348bc06
 		MEM_freeN(buf);
348bc06
 	
348bc06
 		buf = buffer;
348bc06
@@ -370,43 +376,50 @@
348bc06
 	return buf;
348bc06
 }
348bc06
 
348bc06
-void *avi_converter_to_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, int *size)
348bc06
+void *avi_converter_to_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, size_t *size)
348bc06
 {
348bc06
 	unsigned char *buf;
348bc06
-	int bufsize = *size;
348bc06
+	size_t bufsize = *size;
348bc06
 	
348bc06
 	numbytes = 0;
348bc06
 	*size = 0;
348bc06
 
348bc06
-	buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "avi.avi_converter_to_mjpeg 1");
348bc06
+	buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "avi.avi_converter_to_mjpeg 1");
348bc06
+	if (!buf) {
348bc06
+		return NULL;
348bc06
+	}
348bc06
+
348bc06
 	if (!movie->interlace) {
348bc06
 		check_and_compress_jpeg(movie->streams[stream].sh.Quality / 100,
348bc06
 		                        buf, buffer,
348bc06
 		                        movie->header->Width,
348bc06
 		                        movie->header->Height,
348bc06
 		                        bufsize);
348bc06
+		*size += numbytes;
348bc06
 	}
348bc06
 	else {
348bc06
 		deinterlace(movie->odd_fields, buf, buffer, movie->header->Width, movie->header->Height);
348bc06
 		MEM_freeN(buffer);
348bc06
 	
348bc06
 		buffer = buf;
348bc06
-		buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "avi.avi_converter_to_mjpeg 2");
348bc06
-	
348bc06
-		check_and_compress_jpeg(movie->streams[stream].sh.Quality / 100,
348bc06
-		                        buf, buffer,
348bc06
-		                        movie->header->Width,
348bc06
-		                        movie->header->Height / 2,
348bc06
-		                        bufsize / 2);
348bc06
-		*size += numbytes;
348bc06
-		numbytes = 0;
348bc06
-		check_and_compress_jpeg(movie->streams[stream].sh.Quality / 100,
348bc06
-		                        buf + *size, buffer + (movie->header->Height / 2) * movie->header->Width * 3,
348bc06
-		                        movie->header->Width,
348bc06
-		                        movie->header->Height / 2,
348bc06
-		                        bufsize / 2);
348bc06
+		buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "avi.avi_converter_to_mjpeg 1");
348bc06
+
348bc06
+		if (buf) {
348bc06
+			Compress_JPEG(movie->streams[stream].sh.Quality / 100,
348bc06
+				      buf, buffer,
348bc06
+				      movie->header->Width,
348bc06
+				      movie->header->Height / 2,
348bc06
+				      bufsize / 2);
348bc06
+			*size += numbytes;
348bc06
+			numbytes = 0;
348bc06
+			Compress_JPEG(movie->streams[stream].sh.Quality / 100,
348bc06
+				      buf + *size, buffer + (size_t)(movie->header->Height / 2) * (size_t)movie->header->Width * 3,
348bc06
+				      movie->header->Width,
348bc06
+				      movie->header->Height / 2,
348bc06
+				      bufsize / 2);
348bc06
+			*size += numbytes;
348bc06
+		}
348bc06
 	}
348bc06
-	*size += numbytes;
348bc06
 
348bc06
 	MEM_freeN(buffer);
348bc06
 	return buf;
348bc06
@@ -433,7 +446,7 @@
348bc06
 	MEM_freeN(cinfo->dest);
348bc06
 }
348bc06
 
348bc06
-static void jpegmemdestmgr_build(j_compress_ptr cinfo, unsigned char *buffer, int bufsize)
348bc06
+static void jpegmemdestmgr_build(j_compress_ptr cinfo, unsigned char *buffer, size_t bufsize)
348bc06
 {
348bc06
 	cinfo->dest = MEM_mallocN(sizeof(*(cinfo->dest)), "avi.jpegmemdestmgr_build");
348bc06
 	
348bc06
@@ -486,7 +499,7 @@
348bc06
 	MEM_freeN(dinfo->src);
348bc06
 }
348bc06
 
348bc06
-static void jpegmemsrcmgr_build(j_decompress_ptr dinfo, unsigned char *buffer, int bufsize)
348bc06
+static void jpegmemsrcmgr_build(j_decompress_ptr dinfo, unsigned char *buffer, size_t bufsize)
348bc06
 {
348bc06
 	dinfo->src = MEM_mallocN(sizeof(*(dinfo->src)), "avi.jpegmemsrcmgr_build");
348bc06
 	
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi_mjpeg.h blender-2.68a/source/blender/avi/intern/avi_mjpeg.h
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi_mjpeg.h	2013-04-17 21:52:38.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi_mjpeg.h	2022-04-04 07:29:10.605507637 -0400
348bc06
@@ -32,7 +32,7 @@
348bc06
 #ifndef __AVI_MJPEG_H__
348bc06
 #define __AVI_MJPEG_H__
348bc06
 
348bc06
-void *avi_converter_from_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, int *size);
348bc06
-void *avi_converter_to_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, int *size);
348bc06
+void *avi_converter_from_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, size_t *size);
348bc06
+void *avi_converter_to_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, size_t *size);
348bc06
 
348bc06
 #endif /* __AVI_MJPEG_H__ */
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi_rgb32.c blender-2.68a/source/blender/avi/intern/avi_rgb32.c
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi_rgb32.c	2012-10-07 02:27:31.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi_rgb32.c	2022-04-04 08:59:10.860988656 -0400
348bc06
@@ -37,23 +37,29 @@
348bc06
 
348bc06
 #include "MEM_guardedalloc.h"
348bc06
 
348bc06
+#include "IMB_imbuf.h"
348bc06
+
348bc06
 #include "AVI_avi.h"
348bc06
 #include "avi_rgb32.h"
348bc06
 
348bc06
-void *avi_converter_from_rgb32(AviMovie *movie, int stream, unsigned char *buffer, int *size)
348bc06
+void *avi_converter_from_rgb32(AviMovie *movie, int stream, unsigned char *buffer, size_t *size)
348bc06
 {
348bc06
-	int y, x, rowstridea, rowstrideb;
348bc06
+	size_t y, rowstridea, rowstrideb;
348bc06
 	unsigned char *buf;
348bc06
 
348bc06
 	(void)stream; /* unused */
348bc06
 
348bc06
-	buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "fromrgb32buf");
348bc06
-	*size = movie->header->Height * movie->header->Width * 3;
348bc06
+	*size = (size_t)movie->header->Height * (size_t)movie->header->Width * 3;
348bc06
+	buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromrgb32buf");
348bc06
+	if (!buf) {
348bc06
+		return NULL;
348bc06
+	}
348bc06
 
348bc06
 	rowstridea = movie->header->Width * 3;
348bc06
 	rowstrideb = movie->header->Width * 4;
348bc06
 
348bc06
-	for (y = 0; y < movie->header->Height; y++) {
348bc06
+	for ( y = 0; y < movie->header->Height; y++) {
348bc06
+		size_t x;
348bc06
 		for (x = 0; x < movie->header->Width; x++) {
348bc06
 			buf[y * rowstridea + x * 3 + 0] = buffer[y * rowstrideb + x * 4 + 3];
348bc06
 			buf[y * rowstridea + x * 3 + 1] = buffer[y * rowstrideb + x * 4 + 2];
348bc06
@@ -66,21 +72,24 @@
348bc06
 	return buf;
348bc06
 }
348bc06
 
348bc06
-void *avi_converter_to_rgb32(AviMovie *movie, int stream, unsigned char *buffer, int *size)
348bc06
+void *avi_converter_to_rgb32(AviMovie *movie, int stream, unsigned char *buffer, size_t *size)
348bc06
 {
348bc06
-	int i;
348bc06
+	size_t i;
348bc06
 	unsigned char *buf;
348bc06
 	unsigned char *to, *from;
348bc06
 
348bc06
 	(void)stream; /* unused */
348bc06
 
348bc06
-	buf = MEM_mallocN(movie->header->Height * movie->header->Width * 4, "torgb32buf");
348bc06
-	*size = movie->header->Height * movie->header->Width * 4;
348bc06
+	*size = (size_t)movie->header->Height * (size_t)movie->header->Width * 4;
348bc06
+	buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "torgb32buf");
348bc06
+	if (!buf) {
348bc06
+		return NULL;
348bc06
+	}
348bc06
 
348bc06
 	memset(buf, 255, *size);
348bc06
 
348bc06
 	to = buf; from = buffer;
348bc06
-	i = movie->header->Height * movie->header->Width;
348bc06
+	i = (size_t)movie->header->Height * (size_t)movie->header->Width;
348bc06
 	
348bc06
 	while (i--) {
348bc06
 		memcpy(to, from, 3);
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi_rgb32.h blender-2.68a/source/blender/avi/intern/avi_rgb32.h
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi_rgb32.h	2012-10-07 02:27:31.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi_rgb32.h	2022-04-04 07:41:09.931118849 -0400
348bc06
@@ -32,7 +32,7 @@
348bc06
 #ifndef __AVI_RGB32_H__
348bc06
 #define __AVI_RGB32_H__
348bc06
 
348bc06
-void *avi_converter_from_rgb32(AviMovie *movie, int stream, unsigned char *buffer, int *size);
348bc06
-void *avi_converter_to_rgb32(AviMovie *movie, int stream, unsigned char *buffer, int *size);
348bc06
+void *avi_converter_from_rgb32(AviMovie *movie, int stream, unsigned char *buffer, size_t *size);
348bc06
+void *avi_converter_to_rgb32(AviMovie *movie, int stream, unsigned char *buffer, size_t *size);
348bc06
 
348bc06
 #endif /* __AVI_RGB32_H__ */
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi_rgb.c blender-2.68a/source/blender/avi/intern/avi_rgb.c
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi_rgb.c	2012-11-17 21:41:55.000000000 -0500
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi_rgb.c	2022-04-04 13:54:10.675622194 -0400
348bc06
@@ -40,11 +40,12 @@
348bc06
 #include "AVI_avi.h"
348bc06
 #include "avi_rgb.h"
348bc06
 
348bc06
+#include "IMB_imbuf.h"
348bc06
+
348bc06
 /* implementation */
348bc06
 
348bc06
-void *avi_converter_from_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, int *size)
348bc06
+void *avi_converter_from_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, size_t *size)
348bc06
 {
348bc06
-	int x, y, i, rowstride;
348bc06
 	unsigned char *buf;
348bc06
 	AviBitmapInfoHeader *bi;
348bc06
 	short bits = 32;
348bc06
@@ -60,33 +61,36 @@
348bc06
 #ifdef __BIG_ENDIAN__
348bc06
 		unsigned char  *pxla;
348bc06
 #endif
348bc06
-		
348bc06
-		buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
348bc06
+		buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char),  "fromavirgbbuf");
348bc06
+
348bc06
+		if (buf) {
348bc06
+			size_t y = movie->header->Height;
348bc06
+			to = buf;
348bc06
 
348bc06
-		y = movie->header->Height;
348bc06
-		to = buf;
348bc06
+			while (y--) {
348bc06
+				size_t x;
348bc06
+
348bc06
+				pxl = (unsigned short *) (buffer + y * movie->header->Width * 2);
348bc06
 				
348bc06
-		while (y--) {
348bc06
-			pxl = (unsigned short *) (buffer + y * movie->header->Width * 2);
348bc06
-			
348bc06
 #ifdef __BIG_ENDIAN__
348bc06
-			pxla = (unsigned char *)pxl;
348bc06
+				pxla = (unsigned char *)pxl;
348bc06
 #endif
348bc06
 
348bc06
-			x = movie->header->Width;
348bc06
-			while (x--) {
348bc06
+				x = movie->header->Width;
348bc06
+				while (x--) {
348bc06
 #ifdef __BIG_ENDIAN__
348bc06
-				i = pxla[0];
348bc06
-				pxla[0] = pxla[1];
348bc06
-				pxla[1] = i;
348bc06
-	
348bc06
-				pxla += 2;
348bc06
+					int i = pxla[0];
348bc06
+					pxla[0] = pxla[1];
348bc06
+					pxla[1] = i;
348bc06
+
348bc06
+					pxla += 2;
348bc06
 #endif
348bc06
-			
348bc06
-				*(to++) = ((*pxl >> 10) & 0x1f) * 8;
348bc06
-				*(to++) = ((*pxl >> 5) & 0x1f) * 8;
348bc06
-				*(to++) = (*pxl & 0x1f) * 8;
348bc06
-				pxl++;
348bc06
+
348bc06
+					*(to++) = ((*pxl >> 10) & 0x1f) * 8;
348bc06
+					*(to++) = ((*pxl >> 5) & 0x1f) * 8;
348bc06
+					*(to++) = (*pxl & 0x1f) * 8;
348bc06
+					pxl++;
348bc06
+				}
348bc06
 			}
348bc06
 		}
348bc06
 
348bc06
@@ -95,30 +99,33 @@
348bc06
 		return buf;
348bc06
 	}
348bc06
 	else {
348bc06
-		buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
348bc06
-	
348bc06
-		rowstride = movie->header->Width * 3;
348bc06
-		if ((bits != 16) && (movie->header->Width % 2)) rowstride++;
348bc06
-	
348bc06
-		for (y = 0; y < movie->header->Height; y++) {
348bc06
-			memcpy(&buf[y * movie->header->Width * 3], &buffer[((movie->header->Height - 1) - y) * rowstride], movie->header->Width * 3);
348bc06
-		}
348bc06
+		buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char),  "fromavirgbbuf");
348bc06
 	
348bc06
-		for (y = 0; y < movie->header->Height * movie->header->Width * 3; y += 3) {
348bc06
-			i = buf[y];
348bc06
-			buf[y] = buf[y + 2];
348bc06
-			buf[y + 2] = i;
348bc06
+		if (buf) {
348bc06
+			size_t y;
348bc06
+			size_t rowstride = movie->header->Width * 3;
348bc06
+			if ((bits != 16) && (movie->header->Width % 2)) rowstride++;
348bc06
+
348bc06
+			for (y = 0; y < movie->header->Height; y++) {
348bc06
+				memcpy(&buf[y * movie->header->Width * 3], &buffer[((movie->header->Height - 1) - y) * rowstride], movie->header->Width * 3);
348bc06
+			}
348bc06
+
348bc06
+			for (y = 0; y < (size_t)movie->header->Height * (size_t)movie->header->Width * 3; y += 3) {
348bc06
+				int i = buf[y];
348bc06
+				buf[y] = buf[y + 2];
348bc06
+				buf[y + 2] = i;
348bc06
+			}
348bc06
 		}
348bc06
-	
348bc06
+
348bc06
 		MEM_freeN(buffer);
348bc06
-	
348bc06
+
348bc06
 		return buf;
348bc06
 	}
348bc06
 }
348bc06
 
348bc06
-void *avi_converter_to_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, int *size)
348bc06
+void *avi_converter_to_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, size_t *size)
348bc06
 {
348bc06
-	int y, x, i, rowstride;
348bc06
+	size_t y, rowstride;
348bc06
 	unsigned char *buf;
348bc06
 
348bc06
 	(void)stream; /* unused */
348bc06
@@ -136,8 +143,9 @@
348bc06
 	}
348bc06
 
348bc06
 	for (y = 0; y < movie->header->Height; y++) {
348bc06
+		size_t x;
348bc06
 		for (x = 0; x < movie->header->Width * 3; x += 3) {
348bc06
-			i = buf[y * rowstride + x];
348bc06
+			int i = buf[y * rowstride + x];
348bc06
 			buf[y * rowstride + x] = buf[y * rowstride + x + 2];
348bc06
 			buf[y * rowstride + x + 2] = i;
348bc06
 		}
348bc06
diff -Naur blender-2.68a-original/source/blender/avi/intern/avi_rgb.h blender-2.68a/source/blender/avi/intern/avi_rgb.h
348bc06
--- blender-2.68a-original/source/blender/avi/intern/avi_rgb.h	2012-10-07 02:27:31.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/avi/intern/avi_rgb.h	2022-04-04 07:36:15.196590519 -0400
348bc06
@@ -32,7 +32,7 @@
348bc06
 #ifndef __AVI_RGB_H__
348bc06
 #define __AVI_RGB_H__
348bc06
 
348bc06
-void *avi_converter_from_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, int *size);
348bc06
-void *avi_converter_to_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, int *size);
348bc06
+void *avi_converter_from_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, size_t *size);
348bc06
+void *avi_converter_to_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, size_t *size);
348bc06
 
348bc06
 #endif /* __AVI_RGB_H__ */
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/IMB_imbuf.h blender-2.68a/source/blender/imbuf/IMB_imbuf.h
348bc06
--- blender-2.68a-original/source/blender/imbuf/IMB_imbuf.h	2013-05-12 05:14:13.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/imbuf/IMB_imbuf.h	2022-04-04 07:41:29.491353010 -0400
348bc06
@@ -503,6 +503,12 @@
348bc06
 int IMB_metadata_change_field(struct ImBuf *img, const char *key, const char *field);
348bc06
 
348bc06
 /* exported for image tools in blender, to quickly allocate 32 bits rect */
348bc06
+void *imb_alloc_pixels(unsigned int x,
348bc06
+                       unsigned int y,
348bc06
+                       unsigned int channels,
348bc06
+                       size_t typesize,
348bc06
+                       const char *name);
348bc06
+
348bc06
 short imb_addrectImBuf(struct ImBuf *ibuf);
348bc06
 void imb_freerectImBuf(struct ImBuf *ibuf);
348bc06
 
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/allocimbuf.c blender-2.68a/source/blender/imbuf/intern/allocimbuf.c
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/allocimbuf.c	2012-12-19 10:02:51.000000000 -0500
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/allocimbuf.c	2022-04-04 08:59:57.627543513 -0400
348bc06
@@ -193,14 +193,11 @@
348bc06
 
348bc06
 short addzbufImBuf(ImBuf *ibuf)
348bc06
 {
348bc06
-	int size;
348bc06
-	
348bc06
 	if (ibuf == NULL) return FALSE;
348bc06
 	
348bc06
 	IMB_freezbufImBuf(ibuf);
348bc06
 	
348bc06
-	size = ibuf->x * ibuf->y * sizeof(unsigned int);
348bc06
-	if ((ibuf->zbuf = MEM_mapallocN(size, "addzbufImBuf"))) {
348bc06
+	if ((ibuf->zbuf = imb_alloc_pixels(ibuf->x, ibuf->y, 1, sizeof(unsigned int), "addzbufImBuf"))) {
348bc06
 		ibuf->mall |= IB_zbuf;
348bc06
 		ibuf->flags |= IB_zbuf;
348bc06
 		return TRUE;
348bc06
@@ -211,14 +208,11 @@
348bc06
 
348bc06
 short addzbuffloatImBuf(ImBuf *ibuf)
348bc06
 {
348bc06
-	int size;
348bc06
-	
348bc06
 	if (ibuf == NULL) return FALSE;
348bc06
 	
348bc06
 	IMB_freezbuffloatImBuf(ibuf);
348bc06
 	
348bc06
-	size = ibuf->x * ibuf->y * sizeof(float);
348bc06
-	if ((ibuf->zbuf_float = MEM_mapallocN(size, "addzbuffloatImBuf"))) {
348bc06
+	if ((ibuf->zbuf_float = imb_alloc_pixels(ibuf->x, ibuf->y, 1, sizeof(float), "addzbuffloatImBuf"))) {
348bc06
 		ibuf->mall |= IB_zbuffloat;
348bc06
 		ibuf->flags |= IB_zbuffloat;
348bc06
 		return TRUE;
348bc06
@@ -287,20 +281,34 @@
348bc06
 	return TRUE;
348bc06
 }
348bc06
 
348bc06
+void *imb_alloc_pixels(unsigned int x,
348bc06
+                       unsigned int y,
348bc06
+                       unsigned int channels,
348bc06
+                       size_t typesize,
348bc06
+                       const char *name)
348bc06
+{
348bc06
+	size_t size;
348bc06
+
348bc06
+	/* Protect against buffer overflow vulnerabilities from files specifying
348bc06
+	 * a width and height that overflow and alloc too little memory. */
348bc06
+	if (!((uint64_t)x * (uint64_t)y < (SIZE_MAX / (channels * typesize)))) {
348bc06
+		return NULL;
348bc06
+	}
348bc06
+
348bc06
+	size = (size_t)x * (size_t)y * (size_t)channels * typesize;
348bc06
+	return MEM_mapallocN(size, name);
348bc06
+}
348bc06
+
348bc06
 short imb_addrectfloatImBuf(ImBuf *ibuf)
348bc06
 {
348bc06
-	int size;
348bc06
-	
348bc06
 	if (ibuf == NULL) return FALSE;
348bc06
 	
348bc06
 	if (ibuf->rect_float)
348bc06
 		imb_freerectfloatImBuf(ibuf);  /* frees mipmap too, hrm */
348bc06
 	
348bc06
-	size = ibuf->x * ibuf->y;
348bc06
-	size = size * 4 * sizeof(float);
348bc06
 	ibuf->channels = 4;
348bc06
 	
348bc06
-	if ((ibuf->rect_float = MEM_mapallocN(size, "imb_addrectfloatImBuf"))) {
348bc06
+	if ((ibuf->rect_float = imb_alloc_pixels(ibuf->x, ibuf->y, 4, sizeof(float), "imb_addrectfloatImBuf"))) {
348bc06
 		ibuf->mall |= IB_rectfloat;
348bc06
 		ibuf->flags |= IB_rectfloat;
348bc06
 		return TRUE;
348bc06
@@ -312,8 +320,6 @@
348bc06
 /* question; why also add zbuf? */
348bc06
 short imb_addrectImBuf(ImBuf *ibuf)
348bc06
 {
348bc06
-	int size;
348bc06
-
348bc06
 	if (ibuf == NULL) return FALSE;
348bc06
 	
348bc06
 	/* don't call imb_freerectImBuf, it frees mipmaps, this call is used only too give float buffers display */
348bc06
@@ -321,10 +327,7 @@
348bc06
 		MEM_freeN(ibuf->rect);
348bc06
 	ibuf->rect = NULL;
348bc06
 	
348bc06
-	size = ibuf->x * ibuf->y;
348bc06
-	size = size * sizeof(unsigned int);
348bc06
-
348bc06
-	if ((ibuf->rect = MEM_mapallocN(size, "imb_addrectImBuf"))) {
348bc06
+	if ((ibuf->rect = imb_alloc_pixels(ibuf->x, ibuf->y, 4, sizeof(unsigned char), "imb_addrectImBuf"))) {
348bc06
 		ibuf->mall |= IB_rect;
348bc06
 		ibuf->flags |= IB_rect;
348bc06
 		if (ibuf->planes > 32) return (addzbufImBuf(ibuf));
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/bmp.c blender-2.68a/source/blender/imbuf/intern/bmp.c
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/bmp.c	2013-03-17 06:26:23.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/bmp.c	2022-04-04 09:04:24.666711767 -0400
348bc06
@@ -125,7 +125,7 @@
348bc06
 {
348bc06
 	struct ImBuf *ibuf = NULL;
348bc06
 	BMPINFOHEADER bmi;
348bc06
-	int x, y, depth, skip, i;
348bc06
+	int x, y, depth, skip;
348bc06
 	unsigned char *bmp, *rect;
348bc06
 	unsigned short col;
348bc06
 	double xppm, yppm;
348bc06
@@ -163,10 +163,15 @@
348bc06
 	}
348bc06
 	else {
348bc06
 		ibuf = IMB_allocImBuf(x, y, depth, IB_rect);
348bc06
+		if (!ibuf) {
348bc06
+			return NULL;
348bc06
+		}
348bc06
+
348bc06
 		bmp = mem + skip;
348bc06
 		rect = (unsigned char *) ibuf->rect;
348bc06
 
348bc06
 		if (depth == 16) {
348bc06
+			size_t i;
348bc06
 			for (i = x * y; i > 0; i--) {
348bc06
 				col = bmp[0] + (bmp[1] << 8);
348bc06
 				rect[0] = ((col >> 10) & 0x1f) << 3;
348bc06
@@ -179,8 +184,9 @@
348bc06
 
348bc06
 		}
348bc06
 		else if (depth == 24) {
348bc06
+			size_t i;
348bc06
 			for (i = y; i > 0; i--) {
348bc06
-				int j;
348bc06
+				size_t j;
348bc06
 				for (j = x; j > 0; j--) {
348bc06
 					rect[0] = bmp[2];
348bc06
 					rect[1] = bmp[1];
348bc06
@@ -194,6 +200,7 @@
348bc06
 			}
348bc06
 		}
348bc06
 		else if (depth == 32) {
348bc06
+			size_t i;
348bc06
 			for (i = x * y; i > 0; i--) {
348bc06
 				rect[0] = bmp[2];
348bc06
 				rect[1] = bmp[1];
348bc06
@@ -232,7 +239,7 @@
348bc06
 int imb_savebmp(struct ImBuf *ibuf, const char *name, int flags)
348bc06
 {
348bc06
 	BMPINFOHEADER infoheader;
348bc06
-	int bytesize, extrabytes, x, y, t, ptr;
348bc06
+	size_t bytesize, extrabytes, y, ptr;
348bc06
 	uchar *data;
348bc06
 	FILE *ofile;
348bc06
 	
348bc06
@@ -265,6 +272,7 @@
348bc06
 
348bc06
 	/* Need to write out padded image data in bgr format */
348bc06
 	for (y = 0; y < ibuf->y; y++) {
348bc06
+		size_t x, t;
348bc06
 		for (x = 0; x < ibuf->x; x++) {
348bc06
 			ptr = (x + y * ibuf->x) * 4;
348bc06
 			if (putc(data[ptr + 2], ofile) == EOF) return 0;
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/cineon/dpxlib.c blender-2.68a/source/blender/imbuf/intern/cineon/dpxlib.c
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/cineon/dpxlib.c	2013-03-22 01:34:10.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/cineon/dpxlib.c	2022-04-04 13:32:48.236366316 -0400
348bc06
@@ -137,6 +137,7 @@
348bc06
 	LogImageFile *dpx = (LogImageFile *)MEM_mallocN(sizeof(LogImageFile), __func__);
348bc06
 	char *filename = (char *)byteStuff;
348bc06
 	int i;
348bc06
+	size_t max_elements;
348bc06
 
348bc06
 	if (dpx == NULL) {
348bc06
 		if (verbose) printf("DPX: Failed to malloc dpx file structure.\n");
348bc06
@@ -192,7 +193,8 @@
348bc06
 
348bc06
 	dpx->srcFormat = format_DPX;
348bc06
 	dpx->numElements = swap_ushort(header.imageHeader.elements_per_image, dpx->isMSB);
348bc06
-	if (dpx->numElements == 0) {
348bc06
+	max_elements = sizeof(header.imageHeader.element)/sizeof(header.imageHeader.element[0]);
348bc06
+	if (dpx->numElements == 0 || dpx->numElements >= max_elements) {
348bc06
 		if (verbose) printf("DPX: Wrong number of elements: %d\n", dpx->numElements);
348bc06
 		logImageClose(dpx);
348bc06
 		return NULL;
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/cineon/logImageCore.c blender-2.68a/source/blender/imbuf/intern/cineon/logImageCore.c
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/cineon/logImageCore.c	2013-07-17 10:14:50.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/cineon/logImageCore.c	2022-04-04 13:42:55.865601375 -0400
348bc06
@@ -38,6 +38,8 @@
348bc06
 #include "BLI_fileops.h"
348bc06
 #include "BLI_utildefines.h"
348bc06
 
348bc06
+#include "IMB_imbuf.h"
348bc06
+
348bc06
 #include "MEM_guardedalloc.h"
348bc06
 
348bc06
 /*
348bc06
@@ -162,7 +164,7 @@
348bc06
  * Helper
348bc06
  */
348bc06
 
348bc06
-unsigned int getRowLength(int width, LogImageElement logElement)
348bc06
+size_t getRowLength(size_t width, LogImageElement logElement)
348bc06
 {
348bc06
 	/* return the row length in bytes according to width and packing method */
348bc06
 	switch (logElement.bitsPerSample) {
348bc06
@@ -202,7 +204,7 @@
348bc06
 	float *elementData;
348bc06
 	int returnValue;
348bc06
 
348bc06
-	elementData = (float *)MEM_mallocN(logImage->width * logImage->height * logImage->depth * sizeof(float), __func__);
348bc06
+	elementData = (float *)imb_alloc_pixels(logImage->width, logImage->height, logImage->depth, sizeof(float), __func__);
348bc06
 	if (elementData == NULL)
348bc06
 		return 1;
348bc06
 
348bc06
@@ -239,9 +241,9 @@
348bc06
 
348bc06
 static int logImageSetData8(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
-	unsigned int rowLength = getRowLength(logImage->width, logElement);
348bc06
+	size_t rowLength = getRowLength(logImage->width, logElement);
348bc06
 	unsigned char *row;
348bc06
-	int x, y;
348bc06
+	size_t y;
348bc06
 
348bc06
 	row = (unsigned char *)MEM_mallocN(rowLength, __func__);
348bc06
 	if (row == NULL) {
348bc06
@@ -251,6 +253,7 @@
348bc06
 	memset(row, 0, rowLength);
348bc06
 
348bc06
 	for (y = 0; y < logImage->height; y++) {
348bc06
+		size_t x;
348bc06
 		for (x = 0; x < logImage->width * logImage->depth; x++)
348bc06
 			row[x] = (unsigned char)float_uint(data[y * logImage->width * logImage->depth + x], 255);
348bc06
 
348bc06
@@ -266,10 +269,10 @@
348bc06
 
348bc06
 static int logImageSetData10(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
-	unsigned int rowLength = getRowLength(logImage->width, logElement);
348bc06
+	size_t rowLength = getRowLength(logImage->width, logElement);
348bc06
 	unsigned int pixel, index;
348bc06
 	unsigned int *row;
348bc06
-	int x, y, offset;
348bc06
+	size_t y;
348bc06
 
348bc06
 	row = (unsigned int *)MEM_mallocN(rowLength, __func__);
348bc06
 	if (row == NULL) {
348bc06
@@ -278,7 +281,8 @@
348bc06
 	}
348bc06
 
348bc06
 	for (y = 0; y < logImage->height; y++) {
348bc06
-		offset = 22;
348bc06
+		size_t x;
348bc06
+		int offset = 22;
348bc06
 		index = 0;
348bc06
 		pixel = 0;
348bc06
 
348bc06
@@ -308,9 +312,9 @@
348bc06
 
348bc06
 static int logImageSetData12(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
-	unsigned int rowLength = getRowLength(logImage->width, logElement);
348bc06
+	size_t rowLength = getRowLength(logImage->width, logElement);
348bc06
 	unsigned short *row;
348bc06
-	int x, y;
348bc06
+	size_t y;
348bc06
 
348bc06
 	row = (unsigned short *)MEM_mallocN(rowLength, __func__);
348bc06
 	if (row == NULL) {
348bc06
@@ -319,6 +323,7 @@
348bc06
 	}
348bc06
 
348bc06
 	for (y = 0; y < logImage->height; y++) {
348bc06
+		size_t x;
348bc06
 		for (x = 0; x < logImage->width * logImage->depth; x++)
348bc06
 			row[x] = swap_ushort(((unsigned short)float_uint(data[y * logImage->width * logImage->depth + x], 4095)) << 4, logImage->isMSB);
348bc06
 
348bc06
@@ -334,9 +339,9 @@
348bc06
 
348bc06
 static int logImageSetData16(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
-	unsigned int rowLength = getRowLength(logImage->width, logElement);
348bc06
+	size_t rowLength = getRowLength(logImage->width, logElement);
348bc06
 	unsigned short *row;
348bc06
-	int x, y;
348bc06
+	size_t y;
348bc06
 
348bc06
 	row = (unsigned short *)MEM_mallocN(rowLength, __func__);
348bc06
 	if (row == NULL) {
348bc06
@@ -345,6 +350,7 @@
348bc06
 	}
348bc06
 
348bc06
 	for (y = 0; y < logImage->height; y++) {
348bc06
+		size_t x;
348bc06
 		for (x = 0; x < logImage->width * logImage->depth; x++)
348bc06
 			row[x] = swap_ushort((unsigned short)float_uint(data[y * logImage->width * logImage->depth + x], 65535), logImage->isMSB);
348bc06
 
348bc06
@@ -382,7 +388,7 @@
348bc06
 		/* descriptor_Depth and descriptor_Composite are not supported */
348bc06
 		if (logImage->element[i].descriptor != descriptor_Depth && logImage->element[i].descriptor != descriptor_Composite) {
348bc06
 			/* Allocate memory */
348bc06
-			elementData[i] = (float *)MEM_mallocN(logImage->width * logImage->height * logImage->element[i].depth * sizeof(float), __func__);
348bc06
+			elementData[i] = imb_alloc_pixels(logImage->width, logImage->height, logImage->element[i].depth, sizeof(float), __func__);
348bc06
 			if (elementData[i] == NULL) {
348bc06
 				if (verbose) printf("DPX/Cineon: Cannot allocate memory for elementData[%d]\n.", i);
348bc06
 				for (j = 0; j < i; j++)
348bc06
@@ -530,7 +536,7 @@
348bc06
 			}
348bc06
 		}
348bc06
 
348bc06
-		mergedData = (float *)MEM_mallocN(logImage->width * logImage->height * mergedElement.depth * sizeof(float), __func__);
348bc06
+		mergedData = (float *)imb_alloc_pixels(logImage->width, logImage->height, mergedElement.depth, sizeof(float), __func__);
348bc06
 		if (mergedData == NULL) {
348bc06
 			if (verbose) printf("DPX/Cineon: Cannot allocate mergedData.\n");
348bc06
 			for (i = 0; i < logImage->numElements; i++)
348bc06
@@ -590,7 +596,7 @@
348bc06
 static int logImageElementGetData1(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
 	unsigned int pixel;
348bc06
-	int x, y, offset;
348bc06
+	size_t y;
348bc06
 
348bc06
 	/* seek at the right place */
348bc06
 	if (logimage_fseek(logImage, logElement.dataOffset, SEEK_SET) != 0) {
348bc06
@@ -600,7 +606,9 @@
348bc06
 
348bc06
 	/* read 1 bit data padded to 32 bits */
348bc06
 	for (y = 0; y < logImage->height; y++) {
348bc06
+		size_t x;
348bc06
 		for (x = 0; x < logImage->width * logElement.depth; x += 32) {
348bc06
+			int offset;
348bc06
 			if (logimage_read_uint(&pixel, logImage) != 0) {
348bc06
 				if (verbose) printf("DPX/Cineon: EOF reached\n");
348bc06
 				return 1;
348bc06
@@ -615,15 +623,16 @@
348bc06
 
348bc06
 static int logImageElementGetData8(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
-	unsigned int rowLength = getRowLength(logImage->width, logElement);
348bc06
+	size_t rowLength = getRowLength(logImage->width, logElement);
348bc06
 	unsigned char pixel;
348bc06
-	int x, y;
348bc06
+	size_t y;
348bc06
 
348bc06
 	/* extract required pixels */
348bc06
 	for (y = 0; y < logImage->height; y++) {
348bc06
+		size_t x;
348bc06
 		/* 8 bits are 32-bits padded so we need to seek at each row */
348bc06
 		if (logimage_fseek(logImage, logElement.dataOffset + y * rowLength, SEEK_SET) != 0) {
348bc06
-			if (verbose) printf("DPX/Cineon: Couldn't seek at %d\n", logElement.dataOffset + y * rowLength);
348bc06
+			if (verbose) printf("DPX/Cineon: Couldn't seek at %d\n", (int)(logElement.dataOffset + y * rowLength));
348bc06
 			return 1;
348bc06
 		}
348bc06
 
348bc06
@@ -641,7 +650,6 @@
348bc06
 static int logImageElementGetData10(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
 	unsigned int pixel;
348bc06
-	int x, y, offset;
348bc06
 
348bc06
 	/* seek to data */
348bc06
 	if (logimage_fseek(logImage, logElement.dataOffset, SEEK_SET) != 0) {
348bc06
@@ -650,8 +658,10 @@
348bc06
 	}
348bc06
 
348bc06
 	if (logImage->depth == 1 && logImage->srcFormat == format_DPX) {
348bc06
+		size_t y;
348bc06
 		for (y = 0; y < logImage->height; y++) {
348bc06
-			offset = 32;
348bc06
+			int offset = 32;
348bc06
+			size_t x;
348bc06
 			for (x = 0; x < logImage->width * logElement.depth; x++) {
348bc06
 				/* we need to read the next long */
348bc06
 				if (offset >= 30) {
348bc06
@@ -672,8 +682,10 @@
348bc06
 		}
348bc06
 	}
348bc06
 	else {
348bc06
+		size_t y;
348bc06
 		for (y = 0; y < logImage->height; y++) {
348bc06
-			offset = -1;
348bc06
+			int offset = -1;
348bc06
+			size_t x;
348bc06
 			for (x = 0; x < logImage->width * logElement.depth; x++) {
348bc06
 				/* we need to read the next long */
348bc06
 				if (offset < 0) {
348bc06
@@ -699,15 +711,18 @@
348bc06
 
348bc06
 static int logImageElementGetData10Packed(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
-	unsigned int rowLength = getRowLength(logImage->width, logElement);
348bc06
+	size_t rowLength = getRowLength(logImage->width, logElement);
348bc06
 	unsigned int pixel, oldPixel;
348bc06
-	int offset, offset2, x, y;
348bc06
+	size_t y;
348bc06
 
348bc06
 	/* converting bytes to pixels */
348bc06
 	for (y = 0; y < logImage->height; y++) {
348bc06
+		int offset, offset2;
348bc06
+		size_t x;
348bc06
+
348bc06
 		/* seek to data */
348bc06
 		if (logimage_fseek(logImage, y * rowLength + logElement.dataOffset, SEEK_SET) != 0) {
348bc06
-			if (verbose) printf("DPX/Cineon: Couldn't seek at %u\n", y * rowLength + logElement.dataOffset);
348bc06
+			if (verbose) printf("DPX/Cineon: Couldn't seek at %u\n", (int)(y * rowLength + logElement.dataOffset));
348bc06
 			return 1;
348bc06
 		}
348bc06
 
348bc06
@@ -778,15 +793,18 @@
348bc06
 
348bc06
 static int logImageElementGetData12Packed(LogImageFile *logImage, LogImageElement logElement, float *data)
348bc06
 {
348bc06
-	unsigned int rowLength = getRowLength(logImage->width, logElement);
348bc06
+	size_t rowLength = getRowLength(logImage->width, logElement);
348bc06
 	unsigned int pixel, oldPixel;
348bc06
-	int offset, offset2, x, y;
348bc06
+	size_t y;
348bc06
 
348bc06
 	/* converting bytes to pixels */
348bc06
 	for (y = 0; y < logImage->height; y++) {
348bc06
+		int offset, offset2;
348bc06
+		size_t x;
348bc06
+
348bc06
 		/* seek to data */
348bc06
 		if (logimage_fseek(logImage, y * rowLength + logElement.dataOffset, SEEK_SET) != 0) {
348bc06
-			if (verbose) printf("DPX/Cineon: Couldn't seek at %u\n", y * rowLength + logElement.dataOffset);
348bc06
+			if (verbose) printf("DPX/Cineon: Couldn't seek at %u\n", (int)(y * rowLength + logElement.dataOffset));
348bc06
 			return 1;
348bc06
 		}
348bc06
 
348bc06
@@ -1115,7 +1133,7 @@
348bc06
 		case transfer_UserDefined:
348bc06
 		case transfer_Linear:
348bc06
 		case transfer_Logarithmic: {
348bc06
-			memcpy(dst, src, 4 * logImage->width * logImage->height * sizeof(float));
348bc06
+			memcpy(dst, src, 4 * (size_t)logImage->width * (size_t)logImage->height * sizeof(float));
348bc06
 			return 0;
348bc06
 		}
348bc06
 
348bc06
@@ -1430,11 +1448,11 @@
348bc06
 
348bc06
 	if (srcIsLinearRGB != 0) {
348bc06
 		/* we need to convert src to sRGB */
348bc06
-		srgbSrc = (float *)MEM_mallocN(4 * logImage->width * logImage->height * sizeof(float), __func__);
348bc06
+		srgbSrc = (float *)imb_alloc_pixels(logImage->width, logImage->height, 4, sizeof(float), __func__);
348bc06
 		if (srgbSrc == NULL)
348bc06
 			return 1;
348bc06
 
348bc06
-		memcpy(srgbSrc, src, 4 * logImage->width * logImage->height * sizeof(float));
348bc06
+		memcpy(srgbSrc, src, 4 * (size_t)logImage->width * (size_t)logImage->height * sizeof(float));
348bc06
 		srgbSrc_ptr = srgbSrc;
348bc06
 
348bc06
 		/* convert data from Linear RGB to sRGB via lut */
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/cineon/logImageCore.h blender-2.68a/source/blender/imbuf/intern/cineon/logImageCore.h
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/cineon/logImageCore.h	2013-05-28 15:35:26.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/cineon/logImageCore.h	2022-04-04 08:08:50.270987726 -0400
348bc06
@@ -198,7 +198,7 @@
348bc06
 void logImageClose(LogImageFile *logImage);
348bc06
 
348bc06
 /* Data handling */
348bc06
-unsigned int getRowLength(int width, LogImageElement logElement);
348bc06
+size_t getRowLength(size_t width, LogImageElement logElement);
348bc06
 int logImageSetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB);
348bc06
 int logImageGetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB);
348bc06
 
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/iris.c blender-2.68a/source/blender/imbuf/intern/iris.c
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/iris.c	2013-03-04 22:17:46.000000000 -0500
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/iris.c	2022-04-04 09:12:38.122566058 -0400
348bc06
@@ -260,7 +260,6 @@
348bc06
 	unsigned int *starttab, *lengthtab;
348bc06
 	FILE *inf = NULL;
348bc06
 	IMAGE image;
348bc06
-	int x, y, z, tablen;
348bc06
 	int xsize, ysize, zsize;
348bc06
 	int bpp, rle, cur, badorder;
348bc06
 	ImBuf *ibuf;
348bc06
@@ -301,8 +300,8 @@
348bc06
 	}
348bc06
 	
348bc06
 	if (rle) {
348bc06
-		
348bc06
-		tablen = ysize * zsize * sizeof(int);
348bc06
+		size_t y;
348bc06
+		size_t tablen = (size_t)ysize * (size_t)zsize * sizeof(int);
348bc06
 		starttab = (unsigned int *)MEM_mallocN(tablen, "iris starttab");
348bc06
 		lengthtab = (unsigned int *)MEM_mallocN(tablen, "iris endtab");
348bc06
 		file_offset = 512;
348bc06
@@ -314,6 +313,7 @@
348bc06
 		cur = 0;
348bc06
 		badorder = 0;
348bc06
 		for (y = 0; y < ysize; y++) {
348bc06
+			size_t z;
348bc06
 			for (z = 0; z < zsize; z++) {
348bc06
 				if (starttab[y + z * ysize] < cur) {
348bc06
 					badorder = 1;
348bc06
@@ -328,11 +328,15 @@
348bc06
 		if (bpp == 1) {
348bc06
 			
348bc06
 			ibuf = IMB_allocImBuf(xsize, ysize, 8 * zsize, IB_rect);
348bc06
+			if (!ibuf) {
348bc06
+				goto fail_rle;
348bc06
+			}
348bc06
 			if (ibuf->planes > 32) ibuf->planes = 32;
348bc06
 			base = ibuf->rect;
348bc06
 			zbase = (unsigned int *)ibuf->zbuf;
348bc06
 			
348bc06
 			if (badorder) {
348bc06
+				size_t z;
348bc06
 				for (z = 0; z < zsize; z++) {
348bc06
 					lptr = base;
348bc06
 					for (y = 0; y < ysize; y++) {
348bc06
@@ -350,6 +354,7 @@
348bc06
 				lptr = base;
348bc06
 				zptr = zbase;
348bc06
 				for (y = 0; y < ysize; y++) {
348bc06
+					size_t z;
348bc06
 				
348bc06
 					for (z = 0; z < zsize; z++) {
348bc06
 						
348bc06
@@ -371,10 +376,14 @@
348bc06
 		else {  /* bpp == 2 */
348bc06
 			
348bc06
 			ibuf = IMB_allocImBuf(xsize, ysize, 32, (flags & IB_rect) | IB_rectfloat);
348bc06
-			
348bc06
+			if (!ibuf) {
348bc06
+				goto fail_rle;
348bc06
+			}
348bc06
+
348bc06
 			fbase = ibuf->rect_float;
348bc06
 			
348bc06
 			if (badorder) {
348bc06
+				size_t z;
348bc06
 				for (z = 0; z < zsize; z++) {
348bc06
 					fptr = fbase;
348bc06
 					for (y = 0; y < ysize; y++) {
348bc06
@@ -392,6 +401,7 @@
348bc06
 				fptr = fbase;
348bc06
 
348bc06
 				for (y = 0; y < ysize; y++) {
348bc06
+					size_t z;
348bc06
 				
348bc06
 					for (z = 0; z < zsize; z++) {
348bc06
 						
348bc06
@@ -408,14 +418,22 @@
348bc06
 			}
348bc06
 		}
348bc06
 		
348bc06
+fail_rle:
348bc06
 		MEM_freeN(starttab);
348bc06
 		MEM_freeN(lengthtab);
348bc06
 
348bc06
+		if (!ibuf) {
348bc06
+			return NULL;
348bc06
+		}
348bc06
 	}
348bc06
 	else {
348bc06
 		if (bpp == 1) {
348bc06
+			size_t z;
348bc06
 			
348bc06
 			ibuf = IMB_allocImBuf(xsize, ysize, 8 * zsize, IB_rect);
348bc06
+			if (!ibuf) {
348bc06
+				goto fail_uncompressed;
348bc06
+			}
348bc06
 			if (ibuf->planes > 32) ibuf->planes = 32;
348bc06
 
348bc06
 			base = ibuf->rect;
348bc06
@@ -425,6 +443,7 @@
348bc06
 			rledat = file_data + file_offset;
348bc06
 			
348bc06
 			for (z = 0; z < zsize; z++) {
348bc06
+				size_t y;
348bc06
 				
348bc06
 				if (z < 4) lptr = base;
348bc06
 				else if (z < 8) lptr = zbase;
348bc06
@@ -440,8 +459,12 @@
348bc06
 			
348bc06
 		}
348bc06
 		else {  /* bpp == 2 */
348bc06
+			size_t z;
348bc06
 			
348bc06
 			ibuf = IMB_allocImBuf(xsize, ysize, 32, (flags & IB_rect) | IB_rectfloat);
348bc06
+			if (!ibuf) {
348bc06
+				goto fail_uncompressed;
348bc06
+			}
348bc06
 
348bc06
 			fbase = ibuf->rect_float;
348bc06
 
348bc06
@@ -449,6 +472,7 @@
348bc06
 			rledat = file_data + file_offset;
348bc06
 			
348bc06
 			for (z = 0; z < zsize; z++) {
348bc06
+				size_t y;
348bc06
 				
348bc06
 				fptr = fbase;
348bc06
 				
348bc06
@@ -462,6 +486,10 @@
348bc06
 			}
348bc06
 			
348bc06
 		}
348bc06
+fail_uncompressed:
348bc06
+		if (!ibuf) {
348bc06
+			return NULL;
348bc06
+		}
348bc06
 	}
348bc06
 	
348bc06
 	
348bc06
@@ -469,8 +497,9 @@
348bc06
 		uchar *rect;
348bc06
 		
348bc06
 		if (image.zsize == 1) {
348bc06
+			size_t x;
348bc06
 			rect = (uchar *) ibuf->rect;
348bc06
-			for (x = ibuf->x * ibuf->y; x > 0; x--) {
348bc06
+			for (x = (size_t)ibuf->x * (size_t)ibuf->y; x > 0; x--) {
348bc06
 				rect[0] = 255;
348bc06
 				rect[1] = rect[2] = rect[3];
348bc06
 				rect += 4;
348bc06
@@ -478,8 +507,9 @@
348bc06
 		}
348bc06
 		else if (image.zsize == 2) {
348bc06
 			/* grayscale with alpha */
348bc06
+			size_t x;
348bc06
 			rect = (uchar *) ibuf->rect;
348bc06
-			for (x = ibuf->x * ibuf->y; x > 0; x--) {
348bc06
+			for (x = (size_t)ibuf->x * (size_t)ibuf->y; x > 0; x--) {
348bc06
 				rect[0] = rect[2];
348bc06
 				rect[1] = rect[2] = rect[3];
348bc06
 				rect += 4;
348bc06
@@ -487,8 +517,9 @@
348bc06
 		}
348bc06
 		else if (image.zsize == 3) {
348bc06
 			/* add alpha */
348bc06
+			size_t x;
348bc06
 			rect = (uchar *) ibuf->rect;
348bc06
-			for (x = ibuf->x * ibuf->y; x > 0; x--) {
348bc06
+			for (x = (size_t)ibuf->x * (size_t)ibuf->y; x > 0; x--) {
348bc06
 				rect[0] = 255;
348bc06
 				rect += 4;
348bc06
 			}
348bc06
@@ -498,8 +529,9 @@
348bc06
 	else {  /* bpp == 2 */
348bc06
 		
348bc06
 		if (image.zsize == 1) {
348bc06
+			size_t x;
348bc06
 			fbase = ibuf->rect_float;
348bc06
-			for (x = ibuf->x * ibuf->y; x > 0; x--) {
348bc06
+			for (x = (size_t)ibuf->x * (size_t)ibuf->y; x > 0; x--) {
348bc06
 				fbase[0] = 1;
348bc06
 				fbase[1] = fbase[2] = fbase[3];
348bc06
 				fbase += 4;
348bc06
@@ -507,8 +539,9 @@
348bc06
 		}
348bc06
 		else if (image.zsize == 2) {
348bc06
 			/* grayscale with alpha */
348bc06
+			size_t x;
348bc06
 			fbase = ibuf->rect_float;
348bc06
-			for (x = ibuf->x * ibuf->y; x > 0; x--) {
348bc06
+			for (x = (size_t)ibuf->x * (size_t)ibuf->y; x > 0; x--) {
348bc06
 				fbase[0] = fbase[2];
348bc06
 				fbase[1] = fbase[2] = fbase[3];
348bc06
 				fbase += 4;
348bc06
@@ -516,8 +549,9 @@
348bc06
 		}
348bc06
 		else if (image.zsize == 3) {
348bc06
 			/* add alpha */
348bc06
+			size_t x;
348bc06
 			fbase = ibuf->rect_float;
348bc06
-			for (x = ibuf->x * ibuf->y; x > 0; x--) {
348bc06
+			for (x = (size_t)ibuf->x * (size_t)ibuf->y; x > 0; x--) {
348bc06
 				fbase[0] = 1;
348bc06
 				fbase += 4;
348bc06
 			}
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/png.c blender-2.68a/source/blender/imbuf/intern/png.c
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/png.c	2013-05-29 07:49:39.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/png.c	2022-04-04 09:56:49.601984192 -0400
348bc06
@@ -499,7 +499,7 @@
348bc06
 	unsigned char *from, *to;
348bc06
 	unsigned short *from16;
348bc06
 	float *to_float;
348bc06
-	int i, bytesperpixel;
348bc06
+	unsigned int channels;
348bc06
 
348bc06
 	if (imb_is_a_png(mem) == 0) return(NULL);
348bc06
 
348bc06
@@ -542,7 +542,7 @@
348bc06
 	png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, 
348bc06
 	             &color_type, NULL, NULL, NULL);
348bc06
 
348bc06
-	bytesperpixel = png_get_channels(png_ptr, info_ptr);
348bc06
+	channels = png_get_channels(png_ptr, info_ptr);
348bc06
 
348bc06
 	switch (color_type) {
348bc06
 		case PNG_COLOR_TYPE_RGB:
348bc06
@@ -551,10 +551,10 @@
348bc06
 		case PNG_COLOR_TYPE_PALETTE:
348bc06
 			png_set_palette_to_rgb(png_ptr);
348bc06
 			if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
348bc06
-				bytesperpixel = 4;
348bc06
+				channels = 4;
348bc06
 			}
348bc06
 			else {
348bc06
-				bytesperpixel = 3;
348bc06
+				channels = 3;
348bc06
 			}
348bc06
 			break;
348bc06
 		case PNG_COLOR_TYPE_GRAY:
348bc06
@@ -569,7 +569,7 @@
348bc06
 			longjmp(png_jmpbuf(png_ptr), 1);
348bc06
 	}
348bc06
 	
348bc06
-	ibuf = IMB_allocImBuf(width, height, 8 * bytesperpixel, 0);
348bc06
+	ibuf = IMB_allocImBuf(width, height, 8 * channels, 0);
348bc06
 
348bc06
 	if (ibuf) {
348bc06
 		ibuf->ftype = PNG;
348bc06
@@ -593,17 +593,19 @@
348bc06
 
348bc06
 	if (ibuf && ((flags & IB_test) == 0)) {
348bc06
 		if (bit_depth == 16) {
348bc06
+			size_t i;
348bc06
+
348bc06
 			imb_addrectfloatImBuf(ibuf);
348bc06
 			png_set_swap(png_ptr);
348bc06
 
348bc06
-			pixels16 = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(png_uint_16), "pixels");
348bc06
-			if (pixels16 == NULL) {
348bc06
+			pixels16 = imb_alloc_pixels(ibuf->x, ibuf->y, channels, sizeof(png_uint_16), "pixels");
348bc06
+			if (pixels16 == NULL || ibuf->rect_float == NULL) {
348bc06
 				printf("Cannot allocate pixels array\n");
348bc06
 				longjmp(png_jmpbuf(png_ptr), 1);
348bc06
 			}
348bc06
 
348bc06
 			/* allocate memory for an array of row-pointers */
348bc06
-			row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_uint_16p), "row_pointers");
348bc06
+			row_pointers = (png_bytepp) MEM_mallocN((size_t)ibuf->y * sizeof(png_uint_16p), "row_pointers");
348bc06
 			if (row_pointers == NULL) {
348bc06
 				printf("Cannot allocate row-pointers array\n");
348bc06
 				longjmp(png_jmpbuf(png_ptr), 1);
348bc06
@@ -612,7 +614,7 @@
348bc06
 			/* set the individual row-pointers to point at the correct offsets */
348bc06
 			for (i = 0; i < ibuf->y; i++) {
348bc06
 				row_pointers[ibuf->y - 1 - i] = (png_bytep)
348bc06
-				                                ((png_uint_16 *)pixels16 + (i * ibuf->x) * bytesperpixel);
348bc06
+				                                ((png_uint_16 *)pixels16 + (i * ibuf->x) * channels);
348bc06
 			}
348bc06
 
348bc06
 			png_read_image(png_ptr, row_pointers);
348bc06
@@ -622,9 +624,9 @@
348bc06
 			to_float = ibuf->rect_float;
348bc06
 			from16 = pixels16;
348bc06
 
348bc06
-			switch (bytesperpixel) {
348bc06
+			switch (channels) {
348bc06
 				case 4:
348bc06
-					for (i = ibuf->x * ibuf->y; i > 0; i--) {
348bc06
+					for (i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
348bc06
 						to_float[0] = from16[0] / 65535.0;
348bc06
 						to_float[1] = from16[1] / 65535.0;
348bc06
 						to_float[2] = from16[2] / 65535.0;
348bc06
@@ -633,7 +635,7 @@
348bc06
 					}
348bc06
 					break;
348bc06
 				case 3:
348bc06
-					for (i = ibuf->x * ibuf->y; i > 0; i--) {
348bc06
+					for (i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
348bc06
 						to_float[0] = from16[0] / 65535.0;
348bc06
 						to_float[1] = from16[1] / 65535.0;
348bc06
 						to_float[2] = from16[2] / 65535.0;
348bc06
@@ -642,14 +644,14 @@
348bc06
 					}
348bc06
 					break;
348bc06
 				case 2:
348bc06
-					for (i = ibuf->x * ibuf->y; i > 0; i--) {
348bc06
+					for (i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
348bc06
 						to_float[0] = to_float[1] = to_float[2] = from16[0] / 65535.0;
348bc06
 						to_float[3] = from16[1] / 65535.0;
348bc06
 						to_float += 4; from16 += 2;
348bc06
 					}
348bc06
 					break;
348bc06
 				case 1:
348bc06
-					for (i = ibuf->x * ibuf->y; i > 0; i--) {
348bc06
+					for (i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
348bc06
 						to_float[0] = to_float[1] = to_float[2] = from16[0] / 65535.0;
348bc06
 						to_float[3] = 1.0;
348bc06
 						to_float += 4; from16++;
348bc06
@@ -658,25 +660,28 @@
348bc06
 			}
348bc06
 		}
348bc06
 		else {
348bc06
+			size_t i;
348bc06
+			int ri;
348bc06
+
348bc06
 			imb_addrectImBuf(ibuf);
348bc06
 
348bc06
-			pixels = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(unsigned char), "pixels");
348bc06
-			if (pixels == NULL) {
348bc06
+			pixels = imb_alloc_pixels(ibuf->x, ibuf->y, channels, sizeof(unsigned char), "pixels");
348bc06
+			if (pixels == NULL || ibuf->rect == NULL) {
348bc06
 				printf("Cannot allocate pixels array\n");
348bc06
 				longjmp(png_jmpbuf(png_ptr), 1);
348bc06
 			}
348bc06
 
348bc06
 			/* allocate memory for an array of row-pointers */
348bc06
-			row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_bytep), "row_pointers");
348bc06
+			row_pointers = (png_bytepp) MEM_mallocN((size_t)ibuf->y * sizeof(png_bytep), "row_pointers");
348bc06
 			if (row_pointers == NULL) {
348bc06
 				printf("Cannot allocate row-pointers array\n");
348bc06
 				longjmp(png_jmpbuf(png_ptr), 1);
348bc06
 			}
348bc06
 
348bc06
 			/* set the individual row-pointers to point at the correct offsets */
348bc06
-			for (i = 0; i < ibuf->y; i++) {
348bc06
-				row_pointers[ibuf->y - 1 - i] = (png_bytep)
348bc06
-				                                ((unsigned char *)pixels + (i * ibuf->x) * bytesperpixel * sizeof(unsigned char));
348bc06
+			for (ri = 0; ri < ibuf->y; ri++) {
348bc06
+				row_pointers[ibuf->y - 1 - ri] = (png_bytep)
348bc06
+				                                ((unsigned char *)pixels + (((size_t)ri) * ibuf->x) * channels * sizeof(unsigned char));
348bc06
 			}
348bc06
 
348bc06
 			png_read_image(png_ptr, row_pointers);
348bc06
@@ -686,9 +691,9 @@
348bc06
 			to = (unsigned char *) ibuf->rect;
348bc06
 			from = pixels;
348bc06
 
348bc06
-			switch (bytesperpixel) {
348bc06
+			switch (channels) {
348bc06
 				case 4:
348bc06
-					for (i = ibuf->x * ibuf->y; i > 0; i--) {
348bc06
+					for (i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
348bc06
 						to[0] = from[0];
348bc06
 						to[1] = from[1];
348bc06
 						to[2] = from[2];
348bc06
@@ -697,7 +702,7 @@
348bc06
 					}
348bc06
 					break;
348bc06
 				case 3:
348bc06
-					for (i = ibuf->x * ibuf->y; i > 0; i--) {
348bc06
+					for (i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
348bc06
 						to[0] = from[0];
348bc06
 						to[1] = from[1];
348bc06
 						to[2] = from[2];
348bc06
@@ -706,14 +711,14 @@
348bc06
 					}
348bc06
 					break;
348bc06
 				case 2:
348bc06
-					for (i = ibuf->x * ibuf->y; i > 0; i--) {
348bc06
+					for (i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
348bc06
 						to[0] = to[1] = to[2] = from[0];
348bc06
 						to[3] = from[1];
348bc06
 						to += 4; from += 2;
348bc06
 					}
348bc06
 					break;
348bc06
 				case 1:
348bc06
-					for (i = ibuf->x * ibuf->y; i > 0; i--) {
348bc06
+					for (i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
348bc06
 						to[0] = to[1] = to[2] = from[0];
348bc06
 						to[3] = 0xff;
348bc06
 						to += 4; from++;
348bc06
@@ -725,6 +730,7 @@
348bc06
 		if (flags & IB_metadata) {
348bc06
 			png_text *text_chunks;
348bc06
 			int count = png_get_text(png_ptr, info_ptr, &text_chunks, NULL);
348bc06
+			int i;
348bc06
 			for (i = 0; i < count; i++) {
348bc06
 				IMB_metadata_add_field(ibuf, text_chunks[i].key, text_chunks[i].text);
348bc06
 				ibuf->flags |= IB_metadata;
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/radiance_hdr.c blender-2.68a/source/blender/imbuf/intern/radiance_hdr.c
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/radiance_hdr.c	2013-03-08 22:46:30.000000000 -0500
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/radiance_hdr.c	2022-04-04 13:11:02.035807999 -0400
348bc06
@@ -75,7 +75,7 @@
348bc06
 /* read routines */
348bc06
 static unsigned char *oldreadcolrs(RGBE *scan, unsigned char *mem, int xmax)
348bc06
 {
348bc06
-	int i, rshift = 0, len = xmax;
348bc06
+	size_t i, rshift = 0, len = xmax;
348bc06
 	while (len > 0) {
348bc06
 		scan[0][RED] = *mem++;
348bc06
 		scan[0][GRN] = *mem++;
348bc06
@@ -100,22 +100,23 @@
348bc06
 
348bc06
 static unsigned char *freadcolrs(RGBE *scan, unsigned char *mem, int xmax)
348bc06
 {
348bc06
-	int i, j, code, val;
348bc06
-
348bc06
+	int val;
348bc06
+	size_t i;
348bc06
 	if ((xmax < MINELEN) | (xmax > MAXELEN)) return oldreadcolrs(scan, mem, xmax);
348bc06
 
348bc06
-	i = *mem++;
348bc06
-	if (i != 2) return oldreadcolrs(scan, mem - 1, xmax);
348bc06
+	val = *mem++;
348bc06
+	if (val != 2) return oldreadcolrs(scan, mem - 1, xmax);
348bc06
 
348bc06
 	scan[0][GRN] = *mem++;
348bc06
 	scan[0][BLU] = *mem++;
348bc06
 
348bc06
-	i = *mem++;
348bc06
-	if (((scan[0][BLU] << 8) | i) != xmax) return NULL;
348bc06
+	val = *mem++;
348bc06
+	if (((scan[0][BLU] << 8) | val) != xmax) return NULL;
348bc06
 
348bc06
-	for (i = 0; i < 4; i++)
348bc06
+	for (i = 0; i < 4; i++) {
348bc06
+		size_t j;
348bc06
 		for (j = 0; j < xmax; ) {
348bc06
-			code = *mem++;
348bc06
+			int code = *mem++;
348bc06
 			if (code > 128) {
348bc06
 				code &= 127;
348bc06
 				val = *mem++;
348bc06
@@ -126,6 +127,7 @@
348bc06
 				while (code--)
348bc06
 					scan[j++][i] = *mem++;
348bc06
 		}
348bc06
+	}
348bc06
 	return mem;
348bc06
 }
348bc06
 
348bc06
@@ -182,11 +184,11 @@
348bc06
 	float *rect_float;
348bc06
 	int found = 0;
348bc06
 	int width = 0, height = 0;
348bc06
-	int x, y;
348bc06
 	unsigned char *ptr;
348bc06
 	char oriY[80], oriX[80];
348bc06
 
348bc06
 	if (imb_is_a_hdr((void *)mem)) {
348bc06
+		size_t x;
348bc06
 		colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT);
348bc06
 
348bc06
 		/* find empty line, next line is resolution info */
348bc06
@@ -197,6 +199,8 @@
348bc06
 			}
348bc06
 		}
348bc06
 		if (found && (x < (size + 2))) {
348bc06
+			size_t y;
348bc06
+
348bc06
 			if (sscanf((char *)&mem[x + 1], "%79s %d %79s %d", (char *)&oriY, &height,
348bc06
 			           (char *)&oriX, &width) != 4)
348bc06
 			{
348bc06
@@ -223,6 +227,7 @@
348bc06
 			rect_float = ibuf->rect_float;
348bc06
 			
348bc06
 			for (y = 0; y < height; y++) {
348bc06
+				size_t x;
348bc06
 				ptr = freadcolrs(sline, ptr, width);
348bc06
 				if (ptr == NULL) {
348bc06
 					printf("HDR decode error\n");
348bc06
@@ -257,7 +262,8 @@
348bc06
 /* ImBuf write */
348bc06
 static int fwritecolrs(FILE *file, int width, int channels, unsigned char *ibufscan, float *fpscan)
348bc06
 {
348bc06
-	int x, i, j, beg, c2, cnt = 0;
348bc06
+	size_t i, j;
348bc06
+	int beg, c2, cnt = 0;
348bc06
 	fCOLOR fcol;
348bc06
 	RGBE rgbe, *rgbe_scan;
348bc06
 
348bc06
@@ -284,7 +290,7 @@
348bc06
 	}
348bc06
 
348bc06
 	if ((width < MINELEN) | (width > MAXELEN)) {    /* OOBs, write out flat */
348bc06
-		x = fwrite((char *)rgbe_scan, sizeof(RGBE), width, file) - width;
348bc06
+		int x = fwrite((char *)rgbe_scan, sizeof(RGBE), width, file) - width;
348bc06
 		MEM_freeN(rgbe_scan);
348bc06
 		return x;
348bc06
 	}
348bc06
@@ -345,9 +351,10 @@
348bc06
 
348bc06
 int imb_savehdr(struct ImBuf *ibuf, const char *name, int flags)
348bc06
 {
348bc06
+	size_t y;
348bc06
 	FILE *file = BLI_fopen(name, "wb");
348bc06
 	float *fp = NULL;
348bc06
-	int y, width = ibuf->x, height = ibuf->y;
348bc06
+	size_t width = ibuf->x, height = ibuf->y;
348bc06
 	unsigned char *cp = NULL;
348bc06
 	
348bc06
 	(void)flags; /* unused */
348bc06
diff -Naur blender-2.68a-original/source/blender/imbuf/intern/tiff.c blender-2.68a/source/blender/imbuf/intern/tiff.c
348bc06
--- blender-2.68a-original/source/blender/imbuf/intern/tiff.c	2013-07-11 00:38:47.000000000 -0400
348bc06
+++ blender-2.68a/source/blender/imbuf/intern/tiff.c	2022-04-04 08:30:29.953497643 -0400
348bc06
@@ -378,7 +378,7 @@
348bc06
  */
348bc06
 static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image)
348bc06
 {
348bc06
-	ImBuf *tmpibuf;
348bc06
+	ImBuf *tmpibuf = NULL;
348bc06
 	int success = 0;
348bc06
 	short bitspersample, spp, config;
348bc06
 	size_t scanline;
348bc06
@@ -414,16 +414,25 @@
348bc06
 	if (bitspersample == 32) {
348bc06
 		ib_flag = IB_rectfloat;
348bc06
 		fbuf = (float *)_TIFFmalloc(scanline);
348bc06
+		if (!fbuf) {
348bc06
+			goto cleanup;
348bc06
+		}
348bc06
 	}
348bc06
 	else if (bitspersample == 16) {
348bc06
 		ib_flag = IB_rectfloat;
348bc06
 		sbuf = (unsigned short *)_TIFFmalloc(scanline);
348bc06
+		if (!sbuf) {
348bc06
+			goto cleanup;
348bc06
+		}
348bc06
 	}
348bc06
 	else {
348bc06
 		ib_flag = IB_rect;
348bc06
 	}
348bc06
 	
348bc06
 	tmpibuf = IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->planes, ib_flag);
348bc06
+	if (!tmpibuf) {
348bc06
+		goto cleanup;
348bc06
+	}
348bc06
 	
348bc06
 	/* simple RGBA image */
348bc06
 	if (!(bitspersample == 32 || bitspersample == 16)) {
348bc06
@@ -432,7 +441,7 @@
348bc06
 	/* contiguous channels: RGBRGBRGB */
348bc06
 	else if (config == PLANARCONFIG_CONTIG) {
348bc06
 		for (row = 0; row < ibuf->y; row++) {
348bc06
-			int ib_offset = ibuf->x * ibuf->y * 4 - ibuf->x * 4 * (row + 1);
348bc06
+			size_t ib_offset = (size_t)ibuf->x * 4 * ((size_t)ibuf->y - ((size_t)row + 1));
348bc06
 		
348bc06
 			if (bitspersample == 32) {
348bc06
 				success |= TIFFReadScanline(image, fbuf, row, 0);
348bc06
@@ -452,7 +461,7 @@
348bc06
 		 * but only fill in from the TIFF scanline where necessary. */
348bc06
 		for (chan = 0; chan < 4; chan++) {
348bc06
 			for (row = 0; row < ibuf->y; row++) {
348bc06
-				int ib_offset = ibuf->x * ibuf->y * 4 - ibuf->x * 4 * (row + 1);
348bc06
+				size_t ib_offset = (size_t)ibuf->x * 4 * ((size_t)ibuf->y - ((size_t)row + 1));
348bc06
 				
348bc06
 				if (bitspersample == 32) {
348bc06
 					if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */
348bc06
@@ -477,11 +486,6 @@
348bc06
 			}
348bc06
 		}
348bc06
 	}
348bc06
-	
348bc06
-	if (bitspersample == 32)
348bc06
-		_TIFFfree(fbuf);
348bc06
-	else if (bitspersample == 16)
348bc06
-		_TIFFfree(sbuf);
348bc06
 
348bc06
 	if (success) {
348bc06
 		/* Code seems to be not needed for 16 bits tif, on PPC G5 OSX (ton) */
348bc06
@@ -500,6 +504,12 @@
348bc06
 		tmpibuf->mall &= ~ib_flag;
348bc06
 	}
348bc06
 
348bc06
+cleanup:
348bc06
+	if (bitspersample == 32)
348bc06
+		_TIFFfree(fbuf);
348bc06
+	else if (bitspersample == 16)
348bc06
+		_TIFFfree(sbuf);
348bc06
+
348bc06
 	IMB_freeImBuf(tmpibuf);
348bc06
 	
348bc06
 	return success;