From 9dd16f0a1ae599e9e53a49f382cf44719494c183 Mon Sep 17 00:00:00 2001
From: Gerard Patel <g.patel@wanadoo.fr>
Date: Sun, 15 Aug 1999 16:38:07 +0000
Subject: [PATCH] Added protection against possible memory corruption.

---
 objects/dib.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/objects/dib.c b/objects/dib.c
index 1d480d4bc0d..438cabe6afd 100644
--- a/objects/dib.c
+++ b/objects/dib.c
@@ -297,15 +297,18 @@ UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
     
     if (dc->w.bitsPerPixel <= 8) {
 	palEntry = palette->logpalette.palPalEntry + startpos;
-	if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
+	if (startpos + entries > (1 << dc->w.bitsPerPixel))
 	    entries = (1 << dc->w.bitsPerPixel) - startpos;
-	}
-	for (end = colors + entries; colors < end; palEntry++, colors++)
-	{
-	    palEntry->peRed   = colors->rgbRed;
-	    palEntry->peGreen = colors->rgbGreen;
-	    palEntry->peBlue  = colors->rgbBlue;
-	}
+
+        if (startpos + entries > palette->logpalette.palNumEntries)
+            entries = palette->logpalette.palNumEntries - startpos;
+
+        for (end = colors + entries; colors < end; palEntry++, colors++)
+        {
+            palEntry->peRed   = colors->rgbRed;
+            palEntry->peGreen = colors->rgbGreen;
+            palEntry->peBlue  = colors->rgbBlue;
+        }
     } else {
 	entries = 0;
     }
-- 
GitLab