From 7f6ad209681d518fdddddb8dc7ca34316cfae4db Mon Sep 17 00:00:00 2001
From: Adrian Robert <Adrian.B.Robert@gmail.com>
Date: Thu, 23 Jul 2009 13:21:44 +0000
Subject: [PATCH] * nsterm.m (ns_lookup_indexed_color): Check for bad index. 
 (ns_index_color): Init unused slot to 0.  (ns_dumpglyphs_box_or_relief):
 Replace useless xassert with an if(). Bug 3714, possibly 3082.

---
 src/ChangeLog | 5 +++++
 src/nsterm.m  | 6 ++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 3d4ef29a877..fa664e4bc00 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,6 +1,11 @@
 2009-07-23  Adrian Robert  <Adrian.B.Robert@gmail.com>
 
 	* nsterm.m (EmacsView-keyUp:): Only act when running under Tiger.
+	Bugs 3792, 3720, 2402.
+	(ns_lookup_indexed_color): Check for bad index.
+	(ns_index_color): Init unused slot to 0.
+	(ns_dumpglyphs_box_or_relief): Replace useless xassert with an if().
+	Bug 3714, possibly 3082.
 
 2009-07-22  Jason Rumney  <jasonr@gnu.org>
 
diff --git a/src/nsterm.m b/src/nsterm.m
index e140b0eaefb..6249f217281 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1249,6 +1249,8 @@ NSColor *
 ns_lookup_indexed_color (unsigned long idx, struct frame *f)
 {
   struct ns_color_table *color_table = FRAME_NS_DISPLAY_INFO (f)->color_table;
+  if (idx < 1 || idx >= color_table->avail)
+    return nil;
   return color_table->colors[idx];
 }
 
@@ -1266,6 +1268,7 @@ ns_index_color (NSColor *color, struct frame *f)
       color_table->avail = 1; /* skip idx=0 as marker */
       color_table->colors
 	= (NSColor **)xmalloc (color_table->size * sizeof (NSColor *));
+      color_table->colors[0] = nil;
       color_table->empty_indices = [[NSMutableSet alloc] init];
     }
 
@@ -2650,9 +2653,8 @@ ns_dumpglyphs_box_or_relief (struct glyph_string *s)
     r = ns_fix_rect_ibw (r, FRAME_INTERNAL_BORDER_WIDTH (s->f),
                         FRAME_PIXEL_WIDTH (s->f));
 
-  if (s->face->box == FACE_SIMPLE_BOX)
+  /* TODO: Sometimes box_color is 0 and this seems wrong; should investigate. */  if (s->face->box == FACE_SIMPLE_BOX && s->face->box_color)
     {
-      xassert (s->face->box_color != nil);
       ns_draw_box (r, abs (thickness),
                    ns_lookup_indexed_color (face->box_color, s->f),
                   left_p, right_p);
-- 
2.39.5