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
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Reading contents of XML node

Post

Reading contents of XML node

+3
−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?

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

2 comment threads

libxml tag (3 comments)
General comments (3 comments)
General comments
elgonzo‭ wrote almost 3 years ago · edited almost 3 years ago

"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‭ wrote almost 3 years ago · edited almost 3 years 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...

Lundin‭ wrote almost 3 years ago

Check if root_element is NULL or not, straight after calling xmlDocGetRootElement.