Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users

Dashboard
Notifications
Mark all as read
Q&A

Reading contents of XML node

+1
−1

I'm writing some functions in C that parses a part of a XML file (using libxml), but instead of extracting the content of the XML node that has a specific name, it outputs a string that's not in UTF-8 or ASCII.

Here's the XML file in question:

<paths>
  <file_list>
    <group>
      <type>config_directory</type>
      <file>
        <name>AppData/scripts/preferences.empire_script.txt</name>
        <sum>5d064a6cfdc35f4714fd31d5d99735d0</sum>
      </file>
      <file>
        <name>preferences</name>
        <sum>a0f283d89d97a00011f292cc8f9c0d24</sum>
      </file>
    </group>
  </file_list>
</paths>

This is the first function I call, which parses the XML file and finds the XML node with name "file_list"

int 
verify()
{
  xmlDoc *doc = NULL;
  xmlNode *root_element, *cur_node;
  doc = xmlReadFile("test.xml", NULL, 0);
  if (doc == NULL)
    {
      return 1;
    }
  
  root_element = xmlDocGetRootElement (doc);

  if (root_element->type != XML_ELEMENT_NODE || !xmlStrEqual(root_element->name, (const xmlChar*) "paths"))
    {
      return 1;
    }

  cur_node = root_element->children;

  while (cur_node != NULL)
    {
      if (cur_node->type == XML_ELEMENT_NODE && xmlStrEqual(cur_node->name, (const xmlChar*) "file_list"))
	  {
	    if (parse_file_list(cur_node, e) != 0) return 1;
	  }
      cur_node = cur_node->next;
    }
  
  file_close(f);
  return 0;
}

Then I do the same thing in this function and find "group"

static int
parse_file_list(xmlNode *cur)
{
  cur = cur->children;
  while (cur != NULL)
    {
      if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "group"))
	  {
	    if (parse_group(cur) != 0) return 1;
	  }
      cur = cur->next;
    }
  return 0;
}

Here, it should parse the contents of the XML node "type", except I just get a segmentation fault when I call xmlNodeGetContent(cur)

static int
parse_group(xmlNode *cur)
{
  cur = cur->children;
  while (cur != NULL)
    {
      if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "type"))
	  {
	    xmlChar *key = xmlNodeGetContent(cur);
	    xmlFree(key);
	    break;
	  }
      cur = cur->next;
    }

  return 0;
}

Am I missing something?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

3 comments

"Am I missing something?" Who knows. Use a step debugger and step through your code. This should make it easier to encircle the code line that causes the seg fault. Also watch any variable used in the function that you suspect is crashing (parse_group?), and see whether the vars have the expected values at any time, especially when other funcs (such as conf_verify_key and parse_file) are being called. Since you get a seg fault, i suspect an invalid/uninitialized pointer or buffer overrun. elgonzo‭ 13 days ago

Side note: The type variable looks exceptionally fishy to me. Where is it set to a non-null value? What are conf_verify_key and parse_file really doing with the type variable used as one of their function parameters, especially when its value is null? Since i don't know anything about those, and the problem could prolly be also something entirely unrelated to type inside one of these two functions, i stop with my blind speculations here and leave the debugging/troubleshooting to you... elgonzo‭ 13 days ago

Check if root_element is NULL or not, straight after calling xmlDocGetRootElement. Lundin‭ 12 days ago

0 answers

Sign up to answer this question »