From aa1a61c59f2064592d3000a241b01f7ff4bbe0a8 Mon Sep 17 00:00:00 2001
From: Arseny Kapoulkine <arseny.kapoulkine@gmail.com>
Date: Wed, 5 Nov 2014 09:32:52 +0100
Subject: Fix xml_node::offset_debug for corner cases

Computed offsets for documents with nodes that were added using append_buffer
or newly appended nodes without name/value information were invalid.
---
 src/pugixml.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

(limited to 'src')

diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index 973cfd6..3fe492b 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -5402,8 +5402,8 @@ namespace pugi
 
 		impl::xml_document_struct& doc = impl::get_document(_root);
 
-		const char_t* buffer = doc.buffer;
-		if (!buffer) return -1;
+		// we can determine the offset reliably only if there is exactly once parse buffer
+		if (!doc.buffer || doc.extra_buffers) return -1;
 
 		switch (type())
 		{
@@ -5413,13 +5413,13 @@ namespace pugi
 		case node_element:
 		case node_declaration:
 		case node_pi:
-			return (_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) ? -1 : _root->name - buffer;
+			return _root->name && (_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0 ? _root->name - doc.buffer : -1;
 
 		case node_pcdata:
 		case node_cdata:
 		case node_comment:
 		case node_doctype:
-			return (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) ? -1 : _root->value - buffer;
+			return _root->value && (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0 ? _root->value - doc.buffer : -1;
 
 		default:
 			return -1;
-- 
cgit v1.2.3