Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mxml_write_node causing segfault #228

Closed
stefanu21 opened this issue Aug 28, 2018 · 7 comments
Closed

mxml_write_node causing segfault #228

stefanu21 opened this issue Aug 28, 2018 · 7 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@stefanu21
Copy link

when I'm writing a xml subchild as string I get an segfault in the function mxml_write_node

because current->parent in mxml-file.c:2984 is NULL!

I think the problem is here

while ((next = current->next) == NULL)
{
        if (current == node)
          break;

       /*
	* The ? and ! elements are special-cases and have no end tags...
	*/

	current = current->parent;
....
}

you should do current = current->parent; first and then do the check if we are now at the start node. In other case you have a problem when the start node has next elements. then you iterate trough all nodes until parent is zero...

When we print a subchild which has next elements you jump over the break condition....

@michaelrsweet
Copy link
Owner

What version of Mini-XML are you using?

Can you provide some sample code showing what you are doing?

@michaelrsweet michaelrsweet self-assigned this Aug 28, 2018
@stefanu21
Copy link
Author

version is 2.11

void test()
{
  mxml_node_t *xml, *node;
  const char *dummy = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
  char *tmp;

  xml = mxmlLoadString(NULL, dummy, MXML_OPAQUE_CALLBACK);
  node = mxmlFindElement(xml, xml, "from", NULL, NULL, MXML_DESCEND);
  tmp = mxmlSaveAllocString(node, MXML_NO_CALLBACK);

  printf("tmp: %s\n",tmp);
  free(tmp);
  mxmlDelete(xml);
  return;
}

@michaelrsweet
Copy link
Owner

OK, thanks for the test code I've confirmed the issue, although I'm surprised that this hasn't cropped up before... Investigating...

@michaelrsweet michaelrsweet added the bug Something isn't working label Aug 31, 2018
@michaelrsweet michaelrsweet modified the milestones: Future, Stable Aug 31, 2018
@michaelrsweet
Copy link
Owner

[master d32a541] Fix crash bug in mxmlWrite functions (Issue #228)

@stefanu21
Copy link
Author

Hi,
your patch fixes the crash but the output is not correct. When you look at my example code I only want to print node "from". With your fix I print all nodes....

@michaelrsweet michaelrsweet reopened this Oct 1, 2018
@michaelrsweet
Copy link
Owner

Well, the function currently saves the specified node tree, which includes the siblings. That said, the typical usage is to print the root node (<?xml ... ?>) so I think I can safely change this to not also save sibling nodes.

@michaelrsweet
Copy link
Owner

[master f86c267] Do not write siblings of the passed node (Issue #228)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants