diff options
author | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2015-04-11 22:37:38 -0700 |
---|---|---|
committer | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2015-04-11 22:40:30 -0700 |
commit | 814443b147e6159a0ad2842fabc1288ec6a0ee24 (patch) | |
tree | a56d1bb24f33552d19d163e292c15ed34c8ce149 | |
parent | 03ea04c32a6c1961d28d3b5dc66dad906dfc8ca6 (diff) |
Fix exception type for out-of-memory for XPath variables
When parsing XPath variables, we need to perform a heap allocation; if it
fails, an xpath_exception instead of bad_alloc used to be thrown.
Now we throw the exception of a correct type so that xpath_exception means
'parsing error'.
-rw-r--r-- | src/pugixml.cpp | 12 | ||||
-rw-r--r-- | tests/test_xpath_variables.cpp | 2 |
2 files changed, 8 insertions, 6 deletions
diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 5b77a27..5190937 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -7715,7 +7715,7 @@ PUGI__NS_BEGIN } } - PUGI__FN xpath_variable* get_variable_scratch(char_t (&buffer)[32], xpath_variable_set* set, const char_t* begin, const char_t* end) + PUGI__FN bool get_variable_scratch(char_t (&buffer)[32], xpath_variable_set* set, const char_t* begin, const char_t* end, xpath_variable** out_result) { size_t length = static_cast<size_t>(end - begin); char_t* scratch = buffer; @@ -7724,19 +7724,19 @@ PUGI__NS_BEGIN { // need to make dummy on-heap copy scratch = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); - if (!scratch) return 0; + if (!scratch) return false; } // copy string to zero-terminated buffer and perform lookup memcpy(scratch, begin, length * sizeof(char_t)); scratch[length] = 0; - xpath_variable* result = set->get(scratch); + *out_result = set->get(scratch); // free dummy buffer if (scratch != buffer) xml_memory::deallocate(scratch); - return result; + return true; } PUGI__NS_END @@ -10309,7 +10309,9 @@ PUGI__NS_BEGIN if (!_variables) throw_error("Unknown variable: variable set is not provided"); - xpath_variable* var = get_variable_scratch(_scratch, _variables, name.begin, name.end); + xpath_variable* var = 0; + if (!get_variable_scratch(_scratch, _variables, name.begin, name.end, &var)) + throw_error_oom(); if (!var) throw_error("Unknown variable: variable set does not contain the given name"); diff --git a/tests/test_xpath_variables.cpp b/tests/test_xpath_variables.cpp index 53b40cf..7a099c4 100644 --- a/tests/test_xpath_variables.cpp +++ b/tests/test_xpath_variables.cpp @@ -293,7 +293,7 @@ TEST(xpath_variables_long_name_out_of_memory) CHECK_FORCE_FAIL("Expected exception");
}
- catch (const xpath_exception&)
+ catch (const std::bad_alloc&)
{
}
#endif
|