From 826ca89aade75087c3dbbb25fa0c73a6814d75b7 Mon Sep 17 00:00:00 2001 From: zhaozhenlong Date: Wed, 6 Jan 2021 20:59:29 +0800 Subject: [PATCH] fix convert int32 overflow and wrong height in resize --- .../lite/src/runtime/kernel/arm/base/resize_base.cc | 4 ++-- .../tools/converter/parser/onnx/onnx_slice_parser.cc | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/mindspore/lite/src/runtime/kernel/arm/base/resize_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/resize_base.cc index f5565689197..d156b8aa79f 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/resize_base.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/resize_base.cc @@ -62,12 +62,12 @@ int ResizeBaseCPUKernel::CheckParameters() { MS_LOG(INFO) << "Out shape is not assigned"; const_shape_ = false; } else { - new_height_ = reinterpret_cast(out_shape)[0]; + new_height_ = out_tensors_.at(0)->shape()[1]; if (new_height_ < 1) { MS_LOG(ERROR) << "Resize new_height should >= 1, but got " << new_height_; return RET_INVALID_OP_ATTR; } - new_width_ = reinterpret_cast(out_shape)[1]; + new_width_ = out_tensors_.at(0)->shape()[2]; if (new_width_ < 1) { MS_LOG(ERROR) << "Resize new_width should >= 1, but got " << new_width_; return RET_INVALID_OP_ATTR; diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc index facbc955c44..c28eba6ded9 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc @@ -15,6 +15,7 @@ */ #include "tools/converter/parser/onnx/onnx_slice_parser.h" +#include #include #include #include @@ -36,31 +37,32 @@ lite::PrimitiveC *OnnxSliceParser::ParseLitePrimitive(const onnx::GraphProto &on std::vector ends; std::vector axes; std::vector steps; + constexpr int64_t int_32_max = INT32_MAX; for (const auto &onnx_node_attr : onnx_node.attribute()) { const auto &attribute_name = onnx_node_attr.name(); if (attribute_name == "starts") { const int num = onnx_node_attr.ints_size(); starts.clear(); for (int i = 0; i < num; ++i) { - starts.push_back(static_cast(onnx_node_attr.ints()[i])); + starts.push_back(static_cast(std::min(onnx_node_attr.ints()[i], int_32_max))); } } else if (attribute_name == "axes") { const int num = onnx_node_attr.ints_size(); axes.clear(); for (int i = 0; i < num; ++i) { - axes.push_back(static_cast(onnx_node_attr.ints()[i])); + axes.push_back(static_cast(std::min(onnx_node_attr.ints()[i], int_32_max))); } } else if (attribute_name == "ends") { const int num = onnx_node_attr.ints_size(); ends.clear(); for (int i = 0; i < num; ++i) { - ends.push_back(static_cast(onnx_node_attr.ints()[i])); + ends.push_back(static_cast(std::min(onnx_node_attr.ints()[i], int_32_max))); } } else if (attribute_name == "steps") { const int num = onnx_node_attr.ints_size(); steps.clear(); for (int i = 0; i < num; ++i) { - steps.push_back(static_cast(onnx_node_attr.ints()[i])); + steps.push_back(static_cast(std::min(onnx_node_attr.ints()[i], int_32_max))); } } }