diff --git a/gdk-pixbuf/io-xbm.c b/gdk-pixbuf/io-xbm.c
index 46653b9..8ae9d48 100644
--- a/gdk-pixbuf/io-xbm.c
+++ b/gdk-pixbuf/io-xbm.c
@@ -153,7 +153,7 @@ read_bitmap_file_data (FILE    *fstream,
 {
 	guchar *bits = NULL;		/* working variable */
 	char line[MAX_SIZE];		/* input line from file */
-	int size;			/* number of bytes of data */
+	gsize size;			/* number of bytes of data */
 	char name_and_type[MAX_SIZE];	/* an input line */
 	char *type;			/* for parsing */
 	int value;			/* from an input line */
@@ -220,7 +220,7 @@ read_bitmap_file_data (FILE    *fstream,
 		if (strcmp ("bits[]", type))
 			continue;
     
-		if (!ww || !hh)
+		if (!ww || !hh || ww > G_MAXINT16 || hh > G_MAXINT16)
 			RETURN (FALSE);
 
 		if ((ww % 16) && ((ww % 16) < 9) && version10p)
