diff --git a/rts/Hash.c b/rts/Hash.c
index 7139c9e01b0c7370c5bc82e7d9e016394cab72c9..1a9f1c029d57e659ddec851d8eeab263679cb058 100644
--- a/rts/Hash.c
+++ b/rts/Hash.c
@@ -440,14 +440,15 @@ freeHashTable(HashTable *table, void (*freeDataFun)(void *) )
 
     /* Free table segments */
     while (segment >= 0) {
-        while (index >= 0) {
-            HashList *next;
-            for (HashList *hl = table->dir[segment][index]; hl != NULL; hl = next) {
-                next = hl->next;
-                if (freeDataFun != NULL)
+        if (freeDataFun) {
+            while (index >= 0) {
+                HashList *next;
+                for (HashList *hl = table->dir[segment][index]; hl != NULL; hl = next) {
+                    next = hl->next;
                     (*freeDataFun)((void *) hl->data);
+                }
+                index--;
             }
-            index--;
         }
         stgFree(table->dir[segment]);
         segment--;