Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / xml_parse_utils.cpp
index 82327e8..7e8c5a6 100644 (file)
@@ -1,17 +1,26 @@
-// Copyright (C) 2018 Intel Corporation
+// Copyright (C) 2018-2019 Intel Corporation
 // SPDX-License-Identifier: Apache-2.0
 //
 
 #include "xml_parse_utils.h"
 #include "details/ie_exception.hpp"
 #include "ie_precision.hpp"
+#include <string>
+#include <limits>
 
 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str) {
     auto attr = node.attribute(str);
     if (attr.empty())
         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
                            << node.offset_debug();
-    return atoi(attr.value());
+    std::string str_value = std::string(attr.value());
+    std::size_t idx = 0;
+    int int_value = std::stoi(str_value, &idx, 10);
+    if (idx != str_value.length())
+        THROW_IE_EXCEPTION << "node <" << node.name() << "> has attribute \"" << str << "\" = \"" << str_value
+                           << "\" which is not an integer" << " at offset "
+                           << node.offset_debug();
+    return int_value;
 }
 
 uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *str) {
@@ -19,11 +28,14 @@ uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *st
     if (attr.empty())
         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
                            << node.offset_debug();
-    int64_t value = atoll(attr.value());
-    if (value < 0)
-        THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
+    std::string str_value = std::string(attr.value());
+    std::size_t idx = 0;
+    long long int_value = std::stoll(str_value, &idx, 10);
+    if (idx != str_value.length() || int_value < 0 || int_value > (std::numeric_limits<uint64_t>::max)())
+        THROW_IE_EXCEPTION << "node <" << node.name() << "> has attribute \"" << str << "\" = \"" << str_value
+                           << "\" which is not an unsigned 64 bit integer" << " at offset "
                            << node.offset_debug();
-    return static_cast<uint64_t>(value);
+    return static_cast<uint64_t>(int_value);
 }
 
 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str) {
@@ -31,11 +43,14 @@ unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *
     if (attr.empty())
         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
                            << node.offset_debug();
-    int value = atoi(attr.value());
-    if (value < 0)
-        THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
+    std::string str_value = std::string(attr.value());
+    std::size_t idx = 0;
+    long long int_value = std::stoll(str_value, &idx, 10);
+    if (idx != str_value.length() || int_value < 0 || int_value > (std::numeric_limits<unsigned int>::max)())
+        THROW_IE_EXCEPTION << "node <" << node.name() << "> has attribute \"" << str << "\" = \"" << str_value
+                           << "\" which is not an unsigned integer" << " at offset "
                            << node.offset_debug();
-    return static_cast<unsigned int>(value);
+    return static_cast<unsigned int>(int_value);
 }
 
 std::string XMLParseUtils::GetStrAttr(const pugi::xml_node &node, const char *str) {
@@ -57,7 +72,14 @@ float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str) {
     if (attr.empty())
         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
                            << node.offset_debug();
-    return static_cast<float>(atof(attr.value()));
+    std::string str_value = std::string(attr.value());
+    std::size_t idx = 0;
+    float float_value = std::stof(str_value, &idx);
+    if (idx != str_value.length())
+        THROW_IE_EXCEPTION << "node <" << node.name() << "> has attribute \"" << str << "\" = \"" << str_value
+                           << "\" which is not a floating point" << " at offset "
+                           << node.offset_debug();
+    return float_value;
 }
 
 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str) {
@@ -78,33 +100,25 @@ InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node
 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str, int defVal) {
     auto attr = node.attribute(str);
     if (attr.empty()) return defVal;
-    return atoi(attr.value());
+    return GetIntAttr(node, str);
 }
 
 uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *str, uint64_t defVal) {
     auto attr = node.attribute(str);
     if (attr.empty()) return defVal;
-    int64_t value = atoll(attr.value());
-    if (value < 0)
-        THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
-                           << node.offset_debug();
-    return static_cast<uint64_t>(value);
+    return GetUInt64Attr(node, str);
 }
 
 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str, unsigned int defVal) {
     auto attr = node.attribute(str);
     if (attr.empty()) return defVal;
-    int value = atoi(attr.value());
-    if (value < 0)
-        THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
-                           << node.offset_debug();
-    return static_cast<unsigned int>(value);
+    return GetUIntAttr(node, str);
 }
 
 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str, float defVal) {
     auto attr = node.attribute(str);
     if (attr.empty()) return defVal;
-    return static_cast<float>(atof(attr.value()));
+    return GetFloatAttr(node, str);
 }
 
 int XMLParseUtils::GetIntChild(const pugi::xml_node &node, const char *str, int defVal) {