[libxml2] Fix a race in xmlNewInputStream



commit 0d51cfebc9eecf311e50eabdf1c2412211220e6d
Author: Daniel Veillard <veillard redhat com>
Date:   Tue May 15 11:18:40 2012 +0800

    Fix a race in xmlNewInputStream
    
    For https://bugzilla.gnome.org/show_bug.cgi?id=643148
    Reported by Bill Clarke <llib computer org>, it used a global variable
    as a counter for the input id and this was not thread safe. To avoid the
    race without adding unneeded locking in the parser path, move the id to
    the parser context instead.

 include/libxml/parser.h |    2 ++
 parserInternals.c       |   15 ++++++++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)
---
diff --git a/include/libxml/parser.h b/include/libxml/parser.h
index 54f1660..04edb9d 100644
--- a/include/libxml/parser.h
+++ b/include/libxml/parser.h
@@ -308,6 +308,8 @@ struct _xmlParserCtxt {
     int                nodeInfoNr;    /* Depth of the parsing stack */
     int                nodeInfoMax;   /* Max depth of the parsing stack */
     xmlParserNodeInfo *nodeInfoTab;   /* array of nodeInfos */
+
+    int                input_id;      /* we need to label inputs */
 };
 
 /**
diff --git a/parserInternals.c b/parserInternals.c
index 2404ddf..746b7fd 100644
--- a/parserInternals.c
+++ b/parserInternals.c
@@ -1372,13 +1372,13 @@ xmlFreeInputStream(xmlParserInputPtr input) {
  * xmlNewInputStream:
  * @ctxt:  an XML parser context
  *
- * Create a new input stream structure
+ * Create a new input stream structure.
+ *
  * Returns the new input stream or NULL
  */
 xmlParserInputPtr
 xmlNewInputStream(xmlParserCtxtPtr ctxt) {
     xmlParserInputPtr input;
-    static int id = 0;
 
     input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
     if (input == NULL) {
@@ -1389,11 +1389,15 @@ xmlNewInputStream(xmlParserCtxtPtr ctxt) {
     input->line = 1;
     input->col = 1;
     input->standalone = -1;
+
     /*
-     * we don't care about thread reentrancy unicity for a single
-     * parser context (and hence thread) is sufficient.
+     * If the context is NULL the id cannot be initialized, but that
+     * should not happen while parsing which is the situation where
+     * the id is actually needed.
      */
-    input->id = id++;
+    if (ctxt != NULL)
+        input->id = ctxt->input_id++;
+
     return(input);
 }
 
@@ -1757,6 +1761,7 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
     ctxt->charset = XML_CHAR_ENCODING_UTF8;
     ctxt->catalogs = NULL;
     ctxt->nbentities = 0;
+    ctxt->input_id = 1;
     xmlInitNodeInfoSeq(&ctxt->node_seq);
     return(0);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]