Fix thread issues

Gustavo Sverzut Barbieri <[email protected]> Thu, 18 May 2006 15:18:40 -0300
Newsgroups gmane.comp.gnome.gdome
Organization INdT
Message-ID <[email protected]>
--Boundary-00=_BqLbEq5S5pAQTSj
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

While developing a Gdome application using threads, I found an issue with the 
way it handles singletons, like dome-xml-domimpl.c and gdome-xpath-xpeval.c.

You first test if global instance is NULL, if not you allocate the new, 
_EMPTY_ instance directly to the global instance, then goes to fill needed 
fields, like "vtab".

Problem happens if you allocate and is preempted by system. Global pointer is 
not NULL anymore, however you don't have any required field, like "vtab", and 
can segfault your application.

This could be solved easily without any locks, just use a local variable while 
the instance is not setup, just then make it global.

Attached patch fix this problem.

Thanks,

-- 
Gustavo Sverzut Barbieri
------------------------
INdT, Recife, Brazil

Jabber: [email protected]
   MSN: [email protected]
  ICQ#: 17249123
 Skype: gsbarbieri
Mobile: +55 (81) 9927 0010
 Phone:  +1 (347) 624 6296; [email protected]
   GPG: 0xB640E1A2 @ wwwkeys.pgp.net

--Boundary-00=_BqLbEq5S5pAQTSj
Content-Type: text/x-diff; charset="us-ascii";
	name="gdome2-cvs-threadsafe.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="gdome2-cvs-threadsafe.patch"

diff -ur gdome2-fix/libgdome/gdomecore/gdome-xml-domimpl.c gdome2-orig/libgdome/gdomecore/gdome-xml-domimpl.c
--- gdome2-fix/libgdome/gdomecore/gdome-xml-domimpl.c	2006-05-18 15:02:22.000000000 -0300
+++ gdome2-orig/libgdome/gdomecore/gdome-xml-domimpl.c	2005-05-13 04:05:56.000000000 -0300
@@ -70,11 +70,10 @@
 GdomeDOMImplementation *
 gdome_xml_di_mkref (void) {
 	if (gdome_xml_DOMImplementation == NULL) {
-		Gdome_xml_DOMImplementation *n;
-		n = g_new0 (Gdome_xml_DOMImplementation, 1);
-		n->refcnt = 1;
-		n->vtab = &gdome_xml_di_vtab;
-		gdome_xml_DOMImplementation = n;
+		gdome_xml_DOMImplementation = g_new (Gdome_xml_DOMImplementation, 1);
+		memset(gdome_xml_DOMImplementation, 0, sizeof(Gdome_xml_DOMImplementation));
+		gdome_xml_DOMImplementation->refcnt = 1;
+		gdome_xml_DOMImplementation->vtab = &gdome_xml_di_vtab;
 	} else
 		gdome_xml_DOMImplementation->refcnt++;
 
diff -ur gdome2-fix/libgdome/xpath/gdome-xpath-xpeval.c gdome2-orig/libgdome/xpath/gdome-xpath-xpeval.c
--- gdome2-fix/libgdome/xpath/gdome-xpath-xpeval.c	2006-05-18 15:01:55.000000000 -0300
+++ gdome2-orig/libgdome/xpath/gdome-xpath-xpeval.c	2006-05-18 15:11:48.000000000 -0300
@@ -53,11 +53,9 @@
 gdome_xpath_xpeval_mkref (void)
 {
   if (gdome_xpath_XPathEvaluator == NULL) {
-		Gdome_xpath_XPathEvaluator *n;
-		n = g_new0 (Gdome_xpath_XPathEvaluator, 1);
-    n->refcnt = 1;
-    n->vtab = &gdome_xpath_xpeval_vtab;
-		gdome_xpath_XPathEvaluator = n;
+    gdome_xpath_XPathEvaluator = g_new (Gdome_xpath_XPathEvaluator, 1);
+    gdome_xpath_XPathEvaluator->refcnt = 1;
+    gdome_xpath_XPathEvaluator->vtab = &gdome_xpath_xpeval_vtab;
   } else
     gdome_xpath_XPathEvaluator->refcnt++;
 

--Boundary-00=_BqLbEq5S5pAQTSj
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
gdome mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gdome

--Boundary-00=_BqLbEq5S5pAQTSj--