4b8d9b4
diff --git a/tools/ddjvu.cpp b/tools/ddjvu.cpp
4b8d9b4
index 7109952..b41f7d2 100644
4b8d9b4
--- a/tools/ddjvu.cpp
4b8d9b4
+++ b/tools/ddjvu.cpp
4b8d9b4
@@ -70,6 +70,7 @@
4b8d9b4
 #include <locale.h>
4b8d9b4
 #include <fcntl.h>
4b8d9b4
 #include <errno.h>
4b8d9b4
+#include <stdint.h>
4b8d9b4
 
4b8d9b4
 #ifdef UNIX
4b8d9b4
 # include <sys/time.h>
4b8d9b4
@@ -394,7 +395,9 @@ render(ddjvu_page_t *page, int pageno)
4b8d9b4
     rowsize = rrect.w;
4b8d9b4
   else
4b8d9b4
     rowsize = rrect.w * 3; 
4b8d9b4
-  if (! (image = (char*)malloc(rowsize * rrect.h)))
4b8d9b4
+  if ((size_t)rowsize > SIZE_MAX / rrect.h)
4b8d9b4
+    die(i18n("Integer overflow when allocating image buffer for page %d"), pageno);
4b8d9b4
+  if (! (image = (char*)malloc((size_t)rowsize * rrect.h)))
4b8d9b4
     die(i18n("Cannot allocate image buffer for page %d"), pageno);
4b8d9b4
 
4b8d9b4
   /* Render */