Skip to content

Commit 71418cc

Browse files
committed
Fix xml_parse_result::encoding for empty document load
The new early-out for size==0 has a subtly different behavior from the main path: encoding is left at auto instead of being set to the inferred encoding (which is always UTF-8 for empty inputs). In general, xml_parse_result::encoding is a bit inconsistent for error returns: the parse() path sets it including errors, but other paths don't. That said, this is somewhat defensible since there's some difference between not having parsed anything and having parsed something. In any case, before the early out, fragment parse of an empty document would return UTF-8 encoding, so this change restores the behavior and adds a test for it.
1 parent 29b39be commit 71418cc

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/pugixml.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4850,7 +4850,12 @@ PUGI_IMPL_NS_BEGIN
48504850
auto_deleter<void> contents_guard(own ? contents : NULL, xml_memory::deallocate);
48514851

48524852
// early-out for empty documents to avoid buffer allocation overhead
4853-
if (size == 0) return make_parse_result((options & parse_fragment) ? status_ok : status_no_document_element);
4853+
if (size == 0)
4854+
{
4855+
xml_parse_result result = make_parse_result((options & parse_fragment) ? status_ok : status_no_document_element);
4856+
result.encoding = buffer_encoding;
4857+
return result;
4858+
}
48544859

48554860
// get private buffer
48564861
char_t* buffer = NULL;

tests/test_document.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,15 @@ TEST(document_load_buffer_empty_fragment)
13181318
}
13191319
}
13201320

1321+
TEST(document_load_buffer_empty_encoding)
1322+
{
1323+
xml_document doc;
1324+
xml_parse_result result = doc.load_buffer(0, 0, parse_fragment);
1325+
1326+
CHECK(result.status == status_ok);
1327+
CHECK(result.encoding == encoding_utf8);
1328+
}
1329+
13211330
TEST(document_load_buffer_null)
13221331
{
13231332
xml_document doc;

0 commit comments

Comments
 (0)