From 59218b9086f5736cbfa66fb4afb2ae4b4f92c292 Mon Sep 17 00:00:00 2001
From: cristy <cristy@aa41f4f7-0bf4-0310-aa73-e5a19afd5a74>
Date: Fri, 28 Nov 2014 17:21:53 +0000
Subject: [PATCH] Avoid out of bound access in xwd file handling

git-svn-id: https://subversion.imagemagick.org/subversion/ImageMagick/branches/ImageMagick-6@17119 aa41f4f7-0bf4-0310-aa73-e5a19afd5a74
origin: http://trac.imagemagick.org/changeset/17119
---
 coders/xwd.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/coders/xwd.c b/coders/xwd.c
index f5831e8..097e979 100644
--- a/coders/xwd.c
+++ b/coders/xwd.c
@@ -459,11 +459,14 @@ static Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
             {
               pixel=XGetPixel(ximage,(int) x,(int) y);
               index=(IndexPacket) ((pixel >> red_shift) & red_mask);
-              q->red=ScaleShortToQuantum(colors[(long) index].red);
+	      if (index < header.ncolors)
+		 q->red=ScaleShortToQuantum(colors[(long) index].red);
               index=(IndexPacket) ((pixel >> green_shift) & green_mask);
-              q->green=ScaleShortToQuantum(colors[(long) index].green);
+	      if (index < header.ncolors)
+		q->green=ScaleShortToQuantum(colors[(long) index].green);
               index=(IndexPacket) ((pixel >> blue_shift) & blue_mask);
-              q->blue=ScaleShortToQuantum(colors[(long) index].blue);
+	      if (index < header.ncolors)
+		q->blue=ScaleShortToQuantum(colors[(long) index].blue);
               q++;
             }
             if (SyncAuthenticPixels(image,exception) == MagickFalse)
-- 
2.1.4

