From 2d795c731e6729309044607154978696a87fd900 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Sun, 5 Jul 2015 19:11:33 +0000 Subject: [PATCH] CPPXT-104 - Add exception handling to integer conversions --- xmltooling/base.h | 22 ++++++++++++++++++++-- xmltooling/util/XMLHelper.cpp | 8 +++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/xmltooling/base.h b/xmltooling/base.h index 0393062..c55b5d1 100644 --- a/xmltooling/base.h +++ b/xmltooling/base.h @@ -811,7 +811,16 @@ XMLCh* m_##proper; \ public: \ pair get##proper() const { \ - return make_pair((m_##proper!=nullptr),(m_##proper!=nullptr ? xercesc::XMLString::parseInt(m_##proper): 0)); \ + if (m_##proper) { \ + try { \ + return std::make_pair(true, xercesc::XMLString::parseInt(m_##proper)); \ + } \ + catch (...) { \ + return std::make_pair(true, 0); \ + } \ + } else { \ + return std::make_pair(false, 0); \ + } \ } \ void set##proper(const XMLCh* proper) { \ m_##proper = prepareForAssignment(m_##proper,proper); \ @@ -1369,7 +1378,16 @@ #define DECL_INTEGER_CONTENT(proper) \ XMLTOOLING_DOXYGEN(Returns proper in integer form after a NULL indicator.) \ std::pair get##proper() const { \ - return std::make_pair((getTextContent()!=nullptr), (getTextContent()!=nullptr ? xercesc::XMLString::parseInt(getTextContent()) : 0)); \ + if (getTextContent()) { \ + try { \ + return std::make_pair(true, xercesc::XMLString::parseInt(getTextContent())); \ + } \ + catch (...) { \ + return std::make_pair(true, 0); \ + } \ + } else { \ + return std::make_pair(false, 0); \ + } \ } \ XMLTOOLING_DOXYGEN(Sets proper.) \ void set##proper(int proper) { \ diff --git a/xmltooling/util/XMLHelper.cpp b/xmltooling/util/XMLHelper.cpp index a270de4..2682395 100644 --- a/xmltooling/util/XMLHelper.cpp +++ b/xmltooling/util/XMLHelper.cpp @@ -338,9 +338,11 @@ int XMLHelper::getAttrInt(const DOMElement* e, int defValue, const XMLCh* localN if (e) { const XMLCh* val = e->getAttributeNS(ns, localName); if (val && *val) { - int i = XMLString::parseInt(val); - if (i) - return i; + try { + return XMLString::parseInt(val); + } + catch (XMLException&) { + } } } return defValue; -- 2.1.4