83bc7c
--- libwmf-0.2.8.4/src/extra/gd/gd.c
83bc7c
+++ libwmf-0.2.8.4/src/extra/gd/gd.c
83bc7c
@@ -106,6 +106,18 @@
83bc7c
   gdImagePtr im;
83bc7c
   unsigned long cpa_size;
83bc7c
 
83bc7c
+  if (overflow2(sx, sy)) {
83bc7c
+    return NULL;
83bc7c
+  }
83bc7c
+
83bc7c
+  if (overflow2(sizeof (int *), sy)) {
83bc7c
+    return NULL;
83bc7c
+  }
83bc7c
+
83bc7c
+  if (overflow2(sizeof(int), sx)) {
83bc7c
+    return NULL;
83bc7c
+  }
83bc7c
+
83bc7c
   im = (gdImage *) gdMalloc (sizeof (gdImage));
83bc7c
   if (im == 0) return 0;
83bc7c
   memset (im, 0, sizeof (gdImage));
83bc7c
--- libwmf-0.2.8.4/src/extra/gd/gdhelpers.c	2010-12-06 11:47:31.000000000 +0000
83bc7c
+++ libwmf-0.2.8.4/src/extra/gd/gdhelpers.c	2010-12-06 11:48:04.000000000 +0000
83bc7c
@@ -2,6 +2,7 @@
83bc7c
 #include "gdhelpers.h"
83bc7c
 #include <stdlib.h>
83bc7c
 #include <string.h>
83bc7c
+#include <limits.h>
83bc7c
 
83bc7c
 /* TBB: gd_strtok_r is not portable; provide an implementation */
83bc7c
 
83bc7c
@@ -94,3 +95,18 @@
83bc7c
 {
83bc7c
   free (ptr);
83bc7c
 }
83bc7c
+
83bc7c
+int overflow2(int a, int b)
83bc7c
+{
83bc7c
+	if(a < 0 || b < 0) {
83bc7c
+		fprintf(stderr, "gd warning: one parameter to a memory allocation multiplication is negative, failing operation gracefully\n");
83bc7c
+		return 1;
83bc7c
+	}
83bc7c
+	if(b == 0)
83bc7c
+		return 0;
83bc7c
+	if(a > INT_MAX / b) {
83bc7c
+		fprintf(stderr, "gd warning: product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully\n");
83bc7c
+		return 1;
83bc7c
+	}
83bc7c
+	return 0;
83bc7c
+}
83bc7c
--- libwmf-0.2.8.4/src/extra/gd/gdhelpers.h	2010-12-06 11:47:17.000000000 +0000
83bc7c
+++ libwmf-0.2.8.4/src/extra/gd/gdhelpers.h	2010-12-06 11:48:36.000000000 +0000
83bc7c
@@ -15,6 +15,8 @@
83bc7c
 void *gdMalloc(size_t size);
83bc7c
 void *gdRealloc(void *ptr, size_t size);
83bc7c
 
83bc7c
+int overflow2(int a, int b);
83bc7c
+
83bc7c
 #pragma GCC visibility pop
83bc7c
 
83bc7c
 #endif /* GDHELPERS_H */