{"action":"closed","number":163,"pull_request":{"url":"https://api.github.com/repos/polotek/libxmljs/pulls/163","id":2248313,"html_url":"https://github.com/polotek/libxmljs/pull/163","diff_url":"https://github.com/polotek/libxmljs/pull/163.diff","patch_url":"https://github.com/polotek/libxmljs/pull/163.patch","issue_url":"https://api.github.com/repos/polotek/libxmljs/issues/163","number":163,"state":"closed","locked":false,"title":"Prevent SEGFAULT of XmlNodes after they are detached from the tree","user":{"login":"skabbes","id":592178,"avatar_url":"https://avatars.githubusercontent.com/u/592178?v=3","gravatar_id":"","url":"https://api.github.com/users/skabbes","html_url":"https://github.com/skabbes","followers_url":"https://api.github.com/users/skabbes/followers","following_url":"https://api.github.com/users/skabbes/following{/other_user}","gists_url":"https://api.github.com/users/skabbes/gists{/gist_id}","starred_url":"https://api.github.com/users/skabbes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/skabbes/subscriptions","organizations_url":"https://api.github.com/users/skabbes/orgs","repos_url":"https://api.github.com/users/skabbes/repos","events_url":"https://api.github.com/users/skabbes/events{/privacy}","received_events_url":"https://api.github.com/users/skabbes/received_events","type":"User","site_admin":false},"body":"[This](https://github.com/polotek/libxmljs/commit/4f7255d4c5d1c8e2a393560603def146cf758186) commit introduced a bug which allows detached XmlNodes to be double free'd.  I've done some research, and this commit is definitely necessary since `xmlUnlinkNode` does **not** free that memory, and neither does `xmlFreeDoc`.  However, we need a mechanism to prevent this behavior, since it is very difficult for end users to debug segfaults (no stack traces provided by Node).\n\nRepro Steps are laid out in the included test case, but basically go like this:\n\n1. Access children of node `a` (so that those nodes are being reference tracked by v8)\n2. Remove node `a` from the dom tree (which also remove all children nodes, including attributes)\n3. Trigger a garbage collection after all those variables are no longer in scope\n4. Calling xmlFreeNode in `a`'s destructor recursively destroys the xml tree of its children (since `a->parent == NULL`)\n5. Calling xmlFreeNode on a's children cause them to be freed again (since `child->parent != NULL`)\n6. However, `xml_obj->doc->_private` is no longer a valid reference to an XmlDocument, so the unref causes a SEGFAULT\n\n### Possible Solutions\n\n* Simply check `xml_obj->type == -1`, since libxml [automatically resets all memory to -1 after it free's it](http://git.gnome.org/browse/libxml2/tree/xmlmemory.c?id=v2.7.8#n438).  However, this is very much implementation dependent and doesn't allow us to property `unref` the parent document (since the doc pointer is also overwritten)\n\n* Store a pointer to the xmlDoc on the XmlNode object and compare that to `xml_obj->doc` before calling xmlFreeNode on that node.  This assumes that the xmlDoc will never be free'd before any of its children nodes (should be correct).  It also means that we need to maintain that pointer for every operation which may change the node's document (addPrevSibling, addNextSibling off the top of my head)\n\n* Keep track of which nodes have been removed in XmlDocument, and free them explicitly when ~XmlDocument is called.  The code would have to be smart enough to remove nodes from this mapping when they are re-added to the tree.\n\n* Immediately call xmlUnlinkNode and xmlFreeNode inside `XmlNode::remove` and make the api enforce that you cannot reuse any removed node or any of its children.\n\nI'm not super familiar with libxml or v8 or what's possible with them, so maybe someone here who understands the problem can weigh in @shtylman?\n\n","created_at":"2012-09-05T06:20:33Z","updated_at":"2015-10-16T18:46:15Z","closed_at":"2015-10-16T18:46:15Z","merged_at":null,"merge_commit_sha":"15b2ee974178742a4663c4ae41166daef71df1a4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/polotek/libxmljs/pulls/163/commits","review_comments_url":"https://api.github.com/repos/polotek/libxmljs/pulls/163/comments","review_comment_url":"https://api.github.com/repos/polotek/libxmljs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/polotek/libxmljs/issues/163/comments","statuses_url":"https://api.github.com/repos/polotek/libxmljs/statuses/056e4ec2d903839db1a3839417dd649814989f6d","head":{"label":"CodeFridge:feature/double_free","ref":"feature/double_free","sha":"056e4ec2d903839db1a3839417dd649814989f6d","user":{"login":"CodeFridge","id":1434294,"avatar_url":"https://avatars.githubusercontent.com/u/1434294?v=3","gravatar_id":"","url":"https://api.github.com/users/CodeFridge","html_url":"https://github.com/CodeFridge","followers_url":"https://api.github.com/users/CodeFridge/followers","following_url":"https://api.github.com/users/CodeFridge/following{/other_user}","gists_url":"https://api.github.com/users/CodeFridge/gists{/gist_id}","starred_url":"https://api.github.com/users/CodeFridge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CodeFridge/subscriptions","organizations_url":"https://api.github.com/users/CodeFridge/orgs","repos_url":"https://api.github.com/users/CodeFridge/repos","events_url":"https://api.github.com/users/CodeFridge/events{/privacy}","received_events_url":"https://api.github.com/users/CodeFridge/received_events","type":"Organization","site_admin":false},"repo":{"id":4196183,"name":"libxmljs","full_name":"CodeFridge/libxmljs","owner":{"login":"CodeFridge","id":1434294,"avatar_url":"https://avatars.githubusercontent.com/u/1434294?v=3","gravatar_id":"","url":"https://api.github.com/users/CodeFridge","html_url":"https://github.com/CodeFridge","followers_url":"https://api.github.com/users/CodeFridge/followers","following_url":"https://api.github.com/users/CodeFridge/following{/other_user}","gists_url":"https://api.github.com/users/CodeFridge/gists{/gist_id}","starred_url":"https://api.github.com/users/CodeFridge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CodeFridge/subscriptions","organizations_url":"https://api.github.com/users/CodeFridge/orgs","repos_url":"https://api.github.com/users/CodeFridge/repos","events_url":"https://api.github.com/users/CodeFridge/events{/privacy}","received_events_url":"https://api.github.com/users/CodeFridge/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CodeFridge/libxmljs","description":"libxml bindings for v8 javascript engine","fork":true,"url":"https://api.github.com/repos/CodeFridge/libxmljs","forks_url":"https://api.github.com/repos/CodeFridge/libxmljs/forks","keys_url":"https://api.github.com/repos/CodeFridge/libxmljs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CodeFridge/libxmljs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CodeFridge/libxmljs/teams","hooks_url":"https://api.github.com/repos/CodeFridge/libxmljs/hooks","issue_events_url":"https://api.github.com/repos/CodeFridge/libxmljs/issues/events{/number}","events_url":"https://api.github.com/repos/CodeFridge/libxmljs/events","assignees_url":"https://api.github.com/repos/CodeFridge/libxmljs/assignees{/user}","branches_url":"https://api.github.com/repos/CodeFridge/libxmljs/branches{/branch}","tags_url":"https://api.github.com/repos/CodeFridge/libxmljs/tags","blobs_url":"https://api.github.com/repos/CodeFridge/libxmljs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CodeFridge/libxmljs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CodeFridge/libxmljs/git/refs{/sha}","trees_url":"https://api.github.com/repos/CodeFridge/libxmljs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CodeFridge/libxmljs/statuses/{sha}","languages_url":"https://api.github.com/repos/CodeFridge/libxmljs/languages","stargazers_url":"https://api.github.com/repos/CodeFridge/libxmljs/stargazers","contributors_url":"https://api.github.com/repos/CodeFridge/libxmljs/contributors","subscribers_url":"https://api.github.com/repos/CodeFridge/libxmljs/subscribers","subscription_url":"https://api.github.com/repos/CodeFridge/libxmljs/subscription","commits_url":"https://api.github.com/repos/CodeFridge/libxmljs/commits{/sha}","git_commits_url":"https://api.github.com/repos/CodeFridge/libxmljs/git/commits{/sha}","comments_url":"https://api.github.com/repos/CodeFridge/libxmljs/comments{/number}","issue_comment_url":"https://api.github.com/repos/CodeFridge/libxmljs/issues/comments{/number}","contents_url":"https://api.github.com/repos/CodeFridge/libxmljs/contents/{+path}","compare_url":"https://api.github.com/repos/CodeFridge/libxmljs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CodeFridge/libxmljs/merges","archive_url":"https://api.github.com/repos/CodeFridge/libxmljs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CodeFridge/libxmljs/downloads","issues_url":"https://api.github.com/repos/CodeFridge/libxmljs/issues{/number}","pulls_url":"https://api.github.com/repos/CodeFridge/libxmljs/pulls{/number}","milestones_url":"https://api.github.com/repos/CodeFridge/libxmljs/milestones{/number}","notifications_url":"https://api.github.com/repos/CodeFridge/libxmljs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CodeFridge/libxmljs/labels{/name}","releases_url":"https://api.github.com/repos/CodeFridge/libxmljs/releases{/id}","created_at":"2012-05-01T20:21:15Z","updated_at":"2015-02-09T01:38:30Z","pushed_at":"2015-02-09T01:38:30Z","git_url":"git://github.com/CodeFridge/libxmljs.git","ssh_url":"git@github.com:CodeFridge/libxmljs.git","clone_url":"https://github.com/CodeFridge/libxmljs.git","svn_url":"https://github.com/CodeFridge/libxmljs","homepage":"https://github.com/polotek/libxmljs/wiki","size":2083,"stargazers_count":1,"watchers_count":1,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"polotek:master","ref":"master","sha":"2f449767002b6ee8b8ad14e41a04156fc87160a6","user":{"login":"polotek","id":107142,"avatar_url":"https://avatars.githubusercontent.com/u/107142?v=3","gravatar_id":"","url":"https://api.github.com/users/polotek","html_url":"https://github.com/polotek","followers_url":"https://api.github.com/users/polotek/followers","following_url":"https://api.github.com/users/polotek/following{/other_user}","gists_url":"https://api.github.com/users/polotek/gists{/gist_id}","starred_url":"https://api.github.com/users/polotek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/polotek/subscriptions","organizations_url":"https://api.github.com/users/polotek/orgs","repos_url":"https://api.github.com/users/polotek/repos","events_url":"https://api.github.com/users/polotek/events{/privacy}","received_events_url":"https://api.github.com/users/polotek/received_events","type":"User","site_admin":false},"repo":{"id":566073,"name":"libxmljs","full_name":"polotek/libxmljs","owner":{"login":"polotek","id":107142,"avatar_url":"https://avatars.githubusercontent.com/u/107142?v=3","gravatar_id":"","url":"https://api.github.com/users/polotek","html_url":"https://github.com/polotek","followers_url":"https://api.github.com/users/polotek/followers","following_url":"https://api.github.com/users/polotek/following{/other_user}","gists_url":"https://api.github.com/users/polotek/gists{/gist_id}","starred_url":"https://api.github.com/users/polotek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/polotek/subscriptions","organizations_url":"https://api.github.com/users/polotek/orgs","repos_url":"https://api.github.com/users/polotek/repos","events_url":"https://api.github.com/users/polotek/events{/privacy}","received_events_url":"https://api.github.com/users/polotek/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/polotek/libxmljs","description":"libxml bindings for v8 javascript engine","fork":false,"url":"https://api.github.com/repos/polotek/libxmljs","forks_url":"https://api.github.com/repos/polotek/libxmljs/forks","keys_url":"https://api.github.com/repos/polotek/libxmljs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/polotek/libxmljs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/polotek/libxmljs/teams","hooks_url":"https://api.github.com/repos/polotek/libxmljs/hooks","issue_events_url":"https://api.github.com/repos/polotek/libxmljs/issues/events{/number}","events_url":"https://api.github.com/repos/polotek/libxmljs/events","assignees_url":"https://api.github.com/repos/polotek/libxmljs/assignees{/user}","branches_url":"https://api.github.com/repos/polotek/libxmljs/branches{/branch}","tags_url":"https://api.github.com/repos/polotek/libxmljs/tags","blobs_url":"https://api.github.com/repos/polotek/libxmljs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/polotek/libxmljs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/polotek/libxmljs/git/refs{/sha}","trees_url":"https://api.github.com/repos/polotek/libxmljs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/polotek/libxmljs/statuses/{sha}","languages_url":"https://api.github.com/repos/polotek/libxmljs/languages","stargazers_url":"https://api.github.com/repos/polotek/libxmljs/stargazers","contributors_url":"https://api.github.com/repos/polotek/libxmljs/contributors","subscribers_url":"https://api.github.com/repos/polotek/libxmljs/subscribers","subscription_url":"https://api.github.com/repos/polotek/libxmljs/subscription","commits_url":"https://api.github.com/repos/polotek/libxmljs/commits{/sha}","git_commits_url":"https://api.github.com/repos/polotek/libxmljs/git/commits{/sha}","comments_url":"https://api.github.com/repos/polotek/libxmljs/comments{/number}","issue_comment_url":"https://api.github.com/repos/polotek/libxmljs/issues/comments{/number}","contents_url":"https://api.github.com/repos/polotek/libxmljs/contents/{+path}","compare_url":"https://api.github.com/repos/polotek/libxmljs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/polotek/libxmljs/merges","archive_url":"https://api.github.com/repos/polotek/libxmljs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/polotek/libxmljs/downloads","issues_url":"https://api.github.com/repos/polotek/libxmljs/issues{/number}","pulls_url":"https://api.github.com/repos/polotek/libxmljs/pulls{/number}","milestones_url":"https://api.github.com/repos/polotek/libxmljs/milestones{/number}","notifications_url":"https://api.github.com/repos/polotek/libxmljs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/polotek/libxmljs/labels{/name}","releases_url":"https://api.github.com/repos/polotek/libxmljs/releases{/id}","created_at":"2010-03-17T03:08:06Z","updated_at":"2015-10-12T05:52:25Z","pushed_at":"2015-10-16T07:01:17Z","git_url":"git://github.com/polotek/libxmljs.git","ssh_url":"git@github.com:polotek/libxmljs.git","clone_url":"https://github.com/polotek/libxmljs.git","svn_url":"https://github.com/polotek/libxmljs","homepage":"https://github.com/polotek/libxmljs/wiki","size":3094,"stargazers_count":555,"watchers_count":555,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":104,"mirror_url":null,"open_issues_count":32,"forks":104,"open_issues":32,"watchers":555,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/polotek/libxmljs/pulls/163"},"html":{"href":"https://github.com/polotek/libxmljs/pull/163"},"issue":{"href":"https://api.github.com/repos/polotek/libxmljs/issues/163"},"comments":{"href":"https://api.github.com/repos/polotek/libxmljs/issues/163/comments"},"review_comments":{"href":"https://api.github.com/repos/polotek/libxmljs/pulls/163/comments"},"review_comment":{"href":"https://api.github.com/repos/polotek/libxmljs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/polotek/libxmljs/pulls/163/commits"},"statuses":{"href":"https://api.github.com/repos/polotek/libxmljs/statuses/056e4ec2d903839db1a3839417dd649814989f6d"}},"merged":false,"mergeable":false,"mergeable_state":"dirty","merged_by":null,"comments":34,"review_comments":0,"commits":1,"additions":23,"deletions":0,"changed_files":1}}