From 454621e935ff836361c77df436fbd7066c9d44e0 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Wed, 4 Dec 2013 12:29:53 +0100 Subject: [PATCH] isl_hash_table_find: don't use user specified comparison function to grow table When isl_hash_table_find needs to grow the hash table, it would reuse the user specified comparison function to compare entries against each other. However, this may lead to undefined behavior as the comparison function is only meant to be used to compare entries to the "val" argument. Moreover, there is no need to use any comparison function while growing the table since all entries in the original table are supposed to be distinct already. Reported-by: Tobias Grosser Signed-off-by: Sven Verdoolaege --- isl_hash.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/isl_hash.c b/isl_hash.c index bd75a0d4..9bceb0b2 100644 --- a/isl_hash.c +++ b/isl_hash.c @@ -62,8 +62,21 @@ int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table, return 0; } -static int grow_table(struct isl_ctx *ctx, struct isl_hash_table *table, - int (*eq)(const void *entry, const void *val)) +/* Dummy comparison function that always returns false. + */ +static int no(const void *entry, const void *val) +{ + return 0; +} + +/* Extend "table" to twice its size. + * Return 0 on success and -1 on error. + * + * We reuse isl_hash_table_find to create entries in the extended table. + * Since all entries in the original table are assumed to be different, + * there is no need to compare them against each other. + */ +static int grow_table(struct isl_ctx *ctx, struct isl_hash_table *table) { int n; size_t old_size, size; @@ -91,7 +104,7 @@ static int grow_table(struct isl_ctx *ctx, struct isl_hash_table *table, continue; entry = isl_hash_table_find(ctx, table, entries[h].hash, - eq, entries[h].data, 1); + &no, NULL, 1); if (!entry) { table->bits--; free(table->entries); @@ -156,7 +169,7 @@ struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx, return NULL; if (4 * table->n >= 3 * size) { - if (grow_table(ctx, table, eq) < 0) + if (grow_table(ctx, table) < 0) return NULL; return isl_hash_table_find(ctx, table, key_hash, eq, val, 1); } -- 2.11.4.GIT