From 4247ec7f3fce743399dc1be889903876534f6cea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Sat, 16 Feb 2013 13:00:36 +0100
Subject: [PATCH] Image.ColorTable: Fixed some memory leaks in add().

If the intermediate hashtable was too small, it (and the colors
it contained) was leaked when the table was reallocated.

Also adds a fall though marker.
---
 src/modules/Image/colortable.c        | 16 ++++++++++++----
 src/modules/Image/colortable_lookup.h |  2 ++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/modules/Image/colortable.c b/src/modules/Image/colortable.c
index 6a439b2845..5be454c998 100644
--- a/src/modules/Image/colortable.c
+++ b/src/modules/Image/colortable.c
@@ -1366,9 +1366,10 @@ static void _img_add_colortable(struct neo_colortable *rdest,
       if (!(mark=insert_in_hash_nd(en->color,hash,hashsize)))
       {
 	 struct color_hash_entry *oldhash=hash;
-	 j=hashsize;
+	 size_t oldhashsize = hashsize;
 
 rerun_rehash_add_1:
+	 j = oldhashsize;
 
 	 hashsize*=2;
 
@@ -1387,7 +1388,10 @@ rerun_rehash_add_1:
 	    if (oldhash[j].pixels)
 	    {
 	       mark=insert_in_hash_nd(oldhash[j].color,hash,hashsize);
-	       if (!mark) goto rerun_rehash_add_1;
+	       if (!mark) {
+		 free(hash);
+		 goto rerun_rehash_add_1;
+	       }
 	       mark->no=oldhash[i].no;
 	       mark->pixels=oldhash[i].pixels;
 	    }
@@ -1412,9 +1416,10 @@ rerun_rehash_add_1:
       if (!(mark=insert_in_hash_nd(en->color,hash,hashsize)))
       {
 	 struct color_hash_entry *oldhash=hash;
-	 j=hashsize;
+	 size_t oldhashsize = hashsize;
 
 rerun_rehash_add_2:
+	 j = oldhashsize;
 
 	 hashsize*=2;
 
@@ -1433,7 +1438,10 @@ rerun_rehash_add_2:
 	    if (oldhash[j].pixels)
 	    {
 	       mark=insert_in_hash_nd(oldhash[j].color,hash,hashsize);
-	       if (!mark) goto rerun_rehash_add_2;
+	       if (!mark) {
+		 free(hash);
+		 goto rerun_rehash_add_2;
+	       }
 	       if (mark->pixels!=1)
 		  mark->no=oldhash[i].no;
 	       mark->pixels=oldhash[i].pixels;
diff --git a/src/modules/Image/colortable_lookup.h b/src/modules/Image/colortable_lookup.h
index 55ab8dd60b..6b8199189c 100644
--- a/src/modules/Image/colortable_lookup.h
+++ b/src/modules/Image/colortable_lookup.h
@@ -561,6 +561,8 @@ void (*NCTLU_SELECT_FUNCTION(struct neo_colortable *nct))
 #endif /* COLORTABLE_DEBUG */
 	       return &NCTLU_FLAT_CUBICLES_NAME;
 	 }
+	 /* FALL_THROUGH */
+
       default:
 	 Pike_fatal("lookup select (%s:%d) couldn't find the lookup mode\n",
 	       __FILE__,__LINE__);
-- 
GitLab