XPath: evaluate_string now guarantees zero-terminated result (unless the buffer size is zero)
git-svn-id: http://pugixml.googlecode.com/svn/trunk@762 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
parent
9834e61717
commit
a32b4392bb
2 changed files with 15 additions and 7 deletions
|
@ -9321,12 +9321,18 @@ namespace pugi
|
|||
|
||||
xpath_string r = evaluate_string_impl(_root, n, sd);
|
||||
|
||||
size_t size = r.length() + 1;
|
||||
size_t full_size = r.length() + 1;
|
||||
|
||||
// $$ zero-terminate?
|
||||
if (capacity > 0) memcpy(buffer, r.c_str(), (size < capacity ? size : capacity) * sizeof(char_t));
|
||||
if (capacity > 0)
|
||||
{
|
||||
size_t size = (full_size < capacity) ? full_size : capacity;
|
||||
assert(size > 0);
|
||||
|
||||
memcpy(buffer, r.c_str(), (size - 1) * sizeof(char_t));
|
||||
buffer[size - 1] = 0;
|
||||
}
|
||||
|
||||
return size;
|
||||
return full_size;
|
||||
}
|
||||
|
||||
xpath_node_set xpath_query::evaluate_node_set(const xpath_node& n) const
|
||||
|
|
|
@ -220,17 +220,19 @@ TEST(xpath_api_evaluate_string)
|
|||
|
||||
// test for just not enough space
|
||||
std::basic_string<char_t> s2 = base;
|
||||
CHECK(q.evaluate_string(&s2[0], 10, xml_node()) == 11 && memcmp(&s2[0], STR("0123456789xxxxxx"), 16 * sizeof(char_t)) == 0);
|
||||
CHECK(q.evaluate_string(&s2[0], 10, xml_node()) == 11 && memcmp(&s2[0], STR("012345678\0xxxxxx"), 16 * sizeof(char_t)) == 0);
|
||||
|
||||
// test for not enough space
|
||||
std::basic_string<char_t> s3 = base;
|
||||
CHECK(q.evaluate_string(&s3[0], 5, xml_node()) == 11 && memcmp(&s3[0], STR("01234xxxxxxxxxxx"), 16 * sizeof(char_t)) == 0);
|
||||
CHECK(q.evaluate_string(&s3[0], 5, xml_node()) == 11 && memcmp(&s3[0], STR("0123\0xxxxxxxxxxx"), 16 * sizeof(char_t)) == 0);
|
||||
|
||||
// test for single character buffer
|
||||
std::basic_string<char_t> s4 = base;
|
||||
CHECK(q.evaluate_string(&s4[0], 1, xml_node()) == 11 && memcmp(&s4[0], STR("0xxxxxxxxxxxxxxx"), 16 * sizeof(char_t)) == 0);
|
||||
CHECK(q.evaluate_string(&s4[0], 1, xml_node()) == 11 && memcmp(&s4[0], STR("\0xxxxxxxxxxxxxxx"), 16 * sizeof(char_t)) == 0);
|
||||
|
||||
// test for empty buffer
|
||||
std::basic_string<char_t> s5 = base;
|
||||
CHECK(q.evaluate_string(&s5[0], 0, xml_node()) == 11 && memcmp(&s5[0], STR("xxxxxxxxxxxxxxxx"), 16 * sizeof(char_t)) == 0);
|
||||
CHECK(q.evaluate_string(0, 0, xml_node()) == 11);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue