patch from Gary Coady to fix a race in dict reference counting in
* dict.c: patch from Gary Coady to fix a race in dict reference
counting in multithreaded apps.
Daniel
diff --git a/ChangeLog b/ChangeLog
index 666c7e4..598ec6d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Jan 21 17:54:06 CET 2005 Daniel Veillard <daniel@veillard.com>
+
+ * dict.c: patch from Gary Coady to fix a race in dict reference
+ counting in multithreaded apps.
+
Fri Jan 21 16:08:21 CET 2005 Daniel Veillard <daniel@veillard.com>
* parser.c: fixed bug #164556 where non-fatal errors stopped
diff --git a/dict.c b/dict.c
index 1ac95d2..a78ccee 100644
--- a/dict.c
+++ b/dict.c
@@ -60,6 +60,7 @@
*/
struct _xmlDict {
int ref_counter;
+ xmlRMutexPtr mutex;
struct _xmlDictEntry *dict;
int size;
@@ -288,8 +289,11 @@
dict->strings = NULL;
dict->subdict = NULL;
if (dict->dict) {
- memset(dict->dict, 0, MIN_DICT_SIZE * sizeof(xmlDictEntry));
- return(dict);
+ if ((dict->mutex = xmlNewRMutex()) != NULL) {
+ memset(dict->dict, 0, MIN_DICT_SIZE * sizeof(xmlDictEntry));
+ return(dict);
+ }
+ xmlFree(dict->dict);
}
xmlFree(dict);
}
@@ -329,7 +333,9 @@
int
xmlDictReference(xmlDictPtr dict) {
if (dict == NULL) return -1;
+ xmlRMutexLock(dict->mutex);
dict->ref_counter++;
+ xmlRMutexUnlock(dict->mutex);
return(0);
}
@@ -446,8 +452,14 @@
return;
/* decrement the counter, it may be shared by a parser and docs */
+ xmlRMutexLock(dict->mutex);
dict->ref_counter--;
- if (dict->ref_counter > 0) return;
+ if (dict->ref_counter > 0) {
+ xmlRMutexUnlock(dict->mutex);
+ return;
+ }
+
+ xmlRMutexUnlock(dict->mutex);
if (dict->subdict != NULL) {
xmlDictFree(dict->subdict);
@@ -477,6 +489,7 @@
xmlFree(pool);
pool = nextp;
}
+ xmlFreeRMutex(dict->mutex);
xmlFree(dict);
}