diff --git a/src/modules/Image/colortable.c b/src/modules/Image/colortable.c
index 6a439b28459891535313e4788521d1c24af9dfe7..5be454c998db5ee7bccb060140fb3892031eafdc 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 55ab8dd60b977e1e7b8bd5415ca2015bfc6924a2..6b8199189c635b31f54ed00023ba7523865635e8 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__);