Browse Source

Fix potential inithashtable() race condition

potential race condition on configuration reload.
z3APA3A 10 years ago
parent
commit
1be182c65f
1 changed files with 4 additions and 0 deletions
  1. 4 0
      src/auth.c

+ 4 - 0
src/auth.c

@@ -922,6 +922,7 @@ int inithashtable(struct hashtable *ht, unsigned nhashsize){
 	unsigned i;
 	unsigned i;
 
 
 	if(nhashsize<4) return 1;
 	if(nhashsize<4) return 1;
+	pthread_mutex_lock(&hash_mutex);
 	if(ht->hashtable){
 	if(ht->hashtable){
 		myfree(ht->hashtable);
 		myfree(ht->hashtable);
 		ht->hashtable = NULL;
 		ht->hashtable = NULL;
@@ -932,11 +933,13 @@ int inithashtable(struct hashtable *ht, unsigned nhashsize){
 	}
 	}
 	ht->hashsize = 0;
 	ht->hashsize = 0;
 	if(!(ht->hashtable = myalloc((nhashsize>>2) * sizeof(struct hashentry *)))){
 	if(!(ht->hashtable = myalloc((nhashsize>>2) * sizeof(struct hashentry *)))){
+		pthread_mutex_unlock(&hash_mutex);
 		return 2;
 		return 2;
 	}
 	}
 	if(!(ht->hashvalues = myalloc(nhashsize * sizeof(struct hashentry)))){
 	if(!(ht->hashvalues = myalloc(nhashsize * sizeof(struct hashentry)))){
 		myfree(ht->hashtable);
 		myfree(ht->hashtable);
 		ht->hashtable = NULL;
 		ht->hashtable = NULL;
+		pthread_mutex_unlock(&hash_mutex);
 		return 3;
 		return 3;
 	}
 	}
 	ht->hashsize = nhashsize;
 	ht->hashsize = nhashsize;
@@ -946,6 +949,7 @@ int inithashtable(struct hashtable *ht, unsigned nhashsize){
 		(ht->hashvalues + i)->next = ht->hashvalues + i + 1;
 		(ht->hashvalues + i)->next = ht->hashvalues + i + 1;
 	}
 	}
 	ht->hashempty = ht->hashvalues;
 	ht->hashempty = ht->hashvalues;
+	pthread_mutex_unlock(&hash_mutex);
 	return 0;
 	return 0;
 }
 }