isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_hash.c
index 5abf9fd..7b96c51 100644 (file)
@@ -1,15 +1,16 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  */
 
 #include <stdlib.h>
-#include "isl_hash.h"
-#include "isl_ctx.h"
+#include <strings.h>
+#include <isl/hash.h>
+#include <isl/ctx.h>
 
 uint32_t isl_hash_string(uint32_t hash, const char *s)
 {
@@ -63,6 +64,7 @@ int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
 static int grow_table(struct isl_ctx *ctx, struct isl_hash_table *table,
                        int (*eq)(const void *entry, const void *val))
 {
+       int n;
        size_t old_size, size;
        struct isl_hash_table_entry *entries;
        uint32_t h;
@@ -77,6 +79,8 @@ static int grow_table(struct isl_ctx *ctx, struct isl_hash_table *table,
                return -1;
        }
 
+       n = table->n;
+       table->n = 0;
        table->bits++;
 
        for (h = 0; h < old_size; ++h) {
@@ -91,6 +95,7 @@ static int grow_table(struct isl_ctx *ctx, struct isl_hash_table *table,
                        table->bits--;
                        free(table->entries);
                        table->entries = entries;
+                       table->n = n;
                        return -1;
                }
 
@@ -163,7 +168,7 @@ struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx,
 
 int isl_hash_table_foreach(struct isl_ctx *ctx,
                                struct isl_hash_table *table,
-                               int (*fn)(void *entry, void *user), void *user)
+                               int (*fn)(void **entry, void *user), void *user)
 {
        size_t size;
        uint32_t h;
@@ -171,7 +176,7 @@ int isl_hash_table_foreach(struct isl_ctx *ctx,
        size = 1 << table->bits;
        for (h = 0; h < size; ++ h)
                if (table->entries[h].data &&
-                   fn(table->entries[h].data, user) < 0)
+                   fn(&table->entries[h].data, user) < 0)
                        return -1;
        
        return 0;