]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
Avoid endless looping in case of XML errors.
authorPeter Stamfest <peter@stamfest.at>
Wed, 19 Feb 2014 21:25:48 +0000 (22:25 +0100)
committerPeter Stamfest <peter@stamfest.at>
Wed, 5 Mar 2014 07:23:41 +0000 (08:23 +0100)
src/rrd_restore.c

index a1fd53f57a3dcb4648738a7b82867dc48addbb93..2910557cf33fbb14b70f8ff441219e900a2312f5 100644 (file)
@@ -50,7 +50,8 @@ static xmlChar* get_xml_element (
     xmlTextReaderPtr reader
     )
 {
-    while(xmlTextReaderRead(reader)){
+    int rc;
+    while((rc = xmlTextReaderRead(reader)) == 1){
         int type;
         xmlChar *name;
         type = xmlTextReaderNodeType(reader);
@@ -79,7 +80,36 @@ static xmlChar* get_xml_element (
         /* all seems well, return the happy news */
         return name;
     }
-    rrd_set_error("the xml ended while we were looking for an element");
+    if (rc == 0) {
+       rrd_set_error("the xml ended while we were looking for an element");
+    } else {
+       xmlErrorPtr err = xmlGetLastError();
+       /* argh: err->message often contains \n at the end. This is not 
+          what we want: Bite the bullet by copying the message, replacing any 
+          \n, constructing the rrd error message and freeing the temp. buffer.
+       */
+       char *msgcpy = NULL, *c;
+       if (err != NULL && err->message != NULL) {
+           msgcpy = strdup(err->message);
+           if (msgcpy != NULL) {
+               for (c = msgcpy ; *c ; c++) {
+                   if (*c == '\n') *c = ' ';
+               }
+               /* strip whitespace from end of message */
+               for (c-- ; c != msgcpy ; c--) {
+                   if (!isprint(*c)) {
+                       *c = 0;
+                   }
+               }
+           } else {
+               /* out of memory during error handling, hmmmm */
+           }
+       }
+
+       rrd_set_error("error reading/parsing XML: %s", 
+                     msgcpy != NULL ? msgcpy : "?");
+       if (msgcpy) free(msgcpy);
+    }
     return NULL;
 } /* get_xml_element */