88#if defined(__MINGW32__) || defined(__MINGW64__)
99# include < filesystem>
1010#endif
11+ #include < limits>
1112#include < queue>
1213
1314#include " decoder_proto.hpp"
1415#include " framework.pb.h"
1516#include " input_model.hpp"
1617#include " openvino/core/log_util.hpp"
18+ #include " openvino/core/memory_util.hpp"
1719#include " openvino/frontend/paddle/node_context.hpp"
1820#include " openvino/opsets/opset7.hpp"
1921#include " openvino/util/common_util.hpp"
@@ -160,8 +162,29 @@ void InputModel::InputModelImpl::load_places() {
160162
161163namespace {
162164bool read_tensor (std::istream& is, char * data, size_t len) {
163- is.read (data, len);
164- return (size_t )is.gcount () == len;
165+ if (len == 0 ) {
166+ return true ;
167+ }
168+ if (len > static_cast <size_t >(std::numeric_limits<std::streamsize>::max ())) {
169+ return false ;
170+ }
171+ is.read (data, static_cast <std::streamsize>(len));
172+ return is && (size_t )is.gcount () == len;
173+ }
174+
175+ constexpr size_t kMaxTensorDescSize = 64 * 1024 * 1024 ;
176+
177+ template <typename DimsT>
178+ ov::Shape make_shape_checked (const DimsT& dims) {
179+ ov::Shape shape;
180+ shape.reserve (dims.size ());
181+ for (const auto & dim : dims) {
182+ FRONT_END_GENERAL_CHECK (dim >= 0 , " Negative dimension in Paddle weight tensor." );
183+ FRONT_END_GENERAL_CHECK (static_cast <unsigned long long >(dim) <= std::numeric_limits<size_t >::max (),
184+ " Dimension is too large for size_t in Paddle weight tensor." );
185+ shape.push_back (static_cast <size_t >(dim));
186+ }
187+ return shape;
165188}
166189
167190template <typename T>
@@ -288,10 +311,11 @@ void InputModel::InputModelImpl::load_consts(const std::basic_string<T>& folder_
288311
289312 FRONT_END_GENERAL_CHECK (var_desc.type ().type () == ::paddle::framework::proto::VarType::LOD_TENSOR);
290313 const auto & tensor = var_desc.type ().lod_tensor ().tensor ();
291- Shape shape (tensor.dims (). cbegin (), tensor. dims (). cend ());
314+ Shape shape = make_shape_checked (tensor.dims ());
292315 const auto & type = get_ov_type (tensor.data_type ());
293- const auto & data_length = shape_size (shape) * type.size ();
294- std::vector<uint8_t > tensor_data (data_length);
316+ auto data_length = ov::util::get_memory_size_safe (type, shape);
317+ FRONT_END_GENERAL_CHECK (data_length, " Weight tensor size overflow for constant " , name, " ." );
318+ std::vector<uint8_t > tensor_data (*data_length);
295319
296320 bool read_succeed = false ;
297321 if (!folder_with_weights.empty ()) {
@@ -304,21 +328,28 @@ void InputModel::InputModelImpl::load_consts(const std::basic_string<T>& folder_
304328 FRONT_END_GENERAL_CHECK (is && is.is_open (), " Cannot open file for constant value." );
305329 const size_t header_size = 16 ;
306330 std::vector<char > header (header_size);
307- is.read (&header[0 ], header_size);
331+ FRONT_END_GENERAL_CHECK ( is.read (&header[0 ], header_size), " Failed to read constant header for " , name, " . " );
308332
309333 uint32_t dims_len = 0 ;
310- is.read (reinterpret_cast <char *>(&dims_len), 4 );
334+ FRONT_END_GENERAL_CHECK (is.read (reinterpret_cast <char *>(&dims_len), sizeof (dims_len)),
335+ " Failed to read dims length for " ,
336+ name,
337+ " ." );
338+ FRONT_END_GENERAL_CHECK (dims_len <= kMaxTensorDescSize , " Dims struct size too large for " , name, " ." );
311339 std::vector<char > dims_struct (dims_len);
312- is.read (&dims_struct[0 ], dims_len);
313- read_succeed = read_tensor (is, reinterpret_cast <char *>(&tensor_data[0 ]), data_length);
340+ FRONT_END_GENERAL_CHECK (is.read (dims_struct.data (), dims_len),
341+ " Failed to read dims struct for " ,
342+ name,
343+ " ." );
344+ read_succeed = read_tensor (is, reinterpret_cast <char *>(tensor_data.data ()), *data_length);
314345 } else {
315346 FRONT_END_GENERAL_CHECK (false , " Folder with weights must be provided." );
316347 }
317348 FRONT_END_GENERAL_CHECK (read_succeed,
318349 " File containing constant with name " ,
319350 name,
320351 " wasn't successfully read." );
321- auto const_node = opset7::Constant::create (type, shape, & tensor_data[ 0 ] );
352+ auto const_node = opset7::Constant::create (type, shape, tensor_data. data () );
322353 const_node->set_friendly_name (name);
323354 m_tensor_values[name] = const_node;
324355 }
@@ -353,30 +384,47 @@ void InputModel::InputModelImpl::load_consts(std::istream* weight_stream) {
353384 {
354385 const size_t header_size = 16 ;
355386 std::vector<char > header (header_size);
356- weight_stream->read (&header[0 ], header_size);
387+ FRONT_END_GENERAL_CHECK (weight_stream->read (&header[0 ], header_size),
388+ " Failed to read weight header for " ,
389+ name,
390+ " ." );
357391 }
358392
359393 int32_t size;
360- weight_stream->read (reinterpret_cast <char *>(&size), sizeof (size));
394+ FRONT_END_GENERAL_CHECK (weight_stream->read (reinterpret_cast <char *>(&size), sizeof (size)),
395+ " Failed to read TensorDesc size for " ,
396+ name,
397+ " ." );
398+ FRONT_END_GENERAL_CHECK (size > 0 && static_cast <size_t >(size) <= kMaxTensorDescSize ,
399+ " TensorDesc size is invalid for " ,
400+ name,
401+ " ." );
361402
362- std::unique_ptr<char []> buf (new char [size]);
363- weight_stream->read (reinterpret_cast <char *>(buf.get ()), size);
403+ std::vector<char > buf (static_cast <size_t >(size));
404+ FRONT_END_GENERAL_CHECK (weight_stream->read (buf.data (), size),
405+ " Failed to read TensorDesc data for " ,
406+ name,
407+ " ." );
364408
365409 std::unique_ptr<::paddle::framework::proto::VarType_TensorDesc> tensor_desc (
366410 new ::paddle::framework::proto::VarType_TensorDesc ());
367- tensor_desc->ParseFromArray (buf.get (), size);
368- Shape shape (tensor_desc->dims ().cbegin (), tensor_desc->dims ().cend ());
411+ FRONT_END_GENERAL_CHECK (tensor_desc->ParseFromArray (buf.data (), size),
412+ " Failed to parse TensorDesc for " ,
413+ name,
414+ " ." );
415+ Shape shape = make_shape_checked (tensor_desc->dims ());
369416 const auto & type = get_ov_type (tensor_desc->data_type ());
370- const auto & data_length = shape_size (shape) * type.size ();
371- std::vector<uint8_t > tensor_data (data_length);
417+ auto data_length = ov::util::get_memory_size_safe (type, shape);
418+ FRONT_END_GENERAL_CHECK (data_length, " Weight tensor size overflow for constant " , name, " ." );
419+ std::vector<uint8_t > tensor_data (*data_length);
372420
373- bool read_succeed = read_tensor (*weight_stream, reinterpret_cast <char *>(& tensor_data[ 0 ]), data_length);
421+ bool read_succeed = read_tensor (*weight_stream, reinterpret_cast <char *>(tensor_data. data ()), * data_length);
374422 FRONT_END_GENERAL_CHECK (read_succeed,
375423 " File containing constant with name " ,
376424 name,
377425 " wasn't successfully read." );
378426
379- auto const_node = opset7::Constant::create (type, shape, & tensor_data[ 0 ] );
427+ auto const_node = opset7::Constant::create (type, shape, tensor_data. data () );
380428 const_node->set_friendly_name (name);
381429 m_tensor_values[name] = const_node;
382430 }
0 commit comments