diff options
Diffstat (limited to 'Swiften/Parser')
| -rw-r--r-- | Swiften/Parser/ExpatParser.cpp | 8 | ||||
| -rw-r--r-- | Swiften/Parser/LibXMLParser.cpp | 16 |
2 files changed, 15 insertions, 9 deletions
diff --git a/Swiften/Parser/ExpatParser.cpp b/Swiften/Parser/ExpatParser.cpp index 77d959c..8415c42 100644 --- a/Swiften/Parser/ExpatParser.cpp +++ b/Swiften/Parser/ExpatParser.cpp @@ -1,25 +1,24 @@ /* * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Parser/ExpatParser.h> #include <cassert> +#include <limits> #include <memory> #include <string> #include <expat.h> -#include <boost/numeric/conversion/cast.hpp> - #include <Swiften/Base/String.h> #include <Swiften/Parser/XMLParserClient.h> #pragma clang diagnostic ignored "-Wdisabled-macro-expansion" namespace Swift { static const char NAMESPACE_SEPARATOR = '\x01'; @@ -78,19 +77,22 @@ ExpatParser::ExpatParser(XMLParserClient* client) : XMLParser(client), p(new Pri XML_SetXmlDeclHandler(p->parser_, handleXMLDeclaration); XML_SetEntityDeclHandler(p->parser_, handleEntityDeclaration); } ExpatParser::~ExpatParser() { XML_ParserFree(p->parser_); } bool ExpatParser::parse(const std::string& data) { - bool success = XML_Parse(p->parser_, data.c_str(), boost::numeric_cast<int>(data.size()), false) == XML_STATUS_OK; + if (data.size() > std::numeric_limits<int>::max()) { + return false; + } + bool success = XML_Parse(p->parser_, data.c_str(), static_cast<int>(data.size()), false) == XML_STATUS_OK; /*if (!success) { std::cout << "ERROR: " << XML_ErrorString(XML_GetErrorCode(p->parser_)) << " while parsing " << data << std::endl; }*/ return success; } void ExpatParser::stopParser() { XML_StopParser(p->parser_, static_cast<XML_Bool>(0)); } diff --git a/Swiften/Parser/LibXMLParser.cpp b/Swiften/Parser/LibXMLParser.cpp index be0a92d..5bd3737 100644 --- a/Swiften/Parser/LibXMLParser.cpp +++ b/Swiften/Parser/LibXMLParser.cpp @@ -1,24 +1,23 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Parser/LibXMLParser.h> #include <cassert> #include <cstring> +#include <limits> #include <memory> #include <string> -#include <boost/numeric/conversion/cast.hpp> - #include <libxml/parser.h> #include <Swiften/Base/Log.h> #include <Swiften/Parser/XMLParserClient.h> namespace Swift { struct LibXMLParser::Private { xmlSAXHandler handler_; @@ -30,33 +29,35 @@ static void handleStartElement(void* parser, const xmlChar* name, const xmlChar* if (nbDefaulted != 0) { // Just because i don't understand what this means yet :-) SWIFT_LOG(error) << "Unexpected nbDefaulted on XML element" << std::endl; } for (int i = 0; i < nbAttributes*5; i += 5) { std::string attributeNS = ""; if (attributes[i+2]) { attributeNS = std::string(reinterpret_cast<const char*>(attributes[i+2])); } + assert(attributes[i+4] >= attributes[i+3]); attributeValues.addAttribute( std::string(reinterpret_cast<const char*>(attributes[i])), attributeNS, std::string(reinterpret_cast<const char*>(attributes[i+3]), - boost::numeric_cast<size_t>(attributes[i+4]-attributes[i+3]))); + static_cast<size_t>(attributes[i+4]-attributes[i+3]))); } static_cast<XMLParser*>(parser)->getClient()->handleStartElement(reinterpret_cast<const char*>(name), (xmlns ? reinterpret_cast<const char*>(xmlns) : std::string()), attributeValues); } static void handleEndElement(void *parser, const xmlChar* name, const xmlChar*, const xmlChar* xmlns) { static_cast<XMLParser*>(parser)->getClient()->handleEndElement(reinterpret_cast<const char*>(name), (xmlns ? reinterpret_cast<const char*>(xmlns) : std::string())); } static void handleCharacterData(void* parser, const xmlChar* data, int len) { - static_cast<XMLParser*>(parser)->getClient()->handleCharacterData(std::string(reinterpret_cast<const char*>(data), boost::numeric_cast<size_t>(len))); + assert(len >= 0); + static_cast<XMLParser*>(parser)->getClient()->handleCharacterData(std::string(reinterpret_cast<const char*>(data), static_cast<size_t>(len))); } static void handleError(void*, const char* /*m*/, ... ) { /* va_list args; va_start(args, m); vfprintf(stdout, m, args); va_end(args); */ @@ -88,19 +89,22 @@ LibXMLParser::LibXMLParser(XMLParserClient* client) : XMLParser(client), p(new P } LibXMLParser::~LibXMLParser() { if (p->context_) { xmlFreeParserCtxt(p->context_); } } bool LibXMLParser::parse(const std::string& data) { - if (xmlParseChunk(p->context_, data.c_str(), boost::numeric_cast<int>(data.size()), false) == XML_ERR_OK) { + if (data.size() > std::numeric_limits<int>::max()) { + return false; + } + if (xmlParseChunk(p->context_, data.c_str(), static_cast<int>(data.size()), false) == XML_ERR_OK) { return true; } xmlError* error = xmlCtxtGetLastError(p->context_); if (error->code == XML_WAR_NS_URI || error->code == XML_WAR_NS_URI_RELATIVE) { xmlCtxtResetLastError(p->context_); p->context_->errNo = XML_ERR_OK; return true; } return false; |
Swift