diff --git a/mindspore/lite/src/common/file_utils.cc b/mindspore/lite/src/common/file_utils.cc index f178299ef58..dcafee5d1d5 100644 --- a/mindspore/lite/src/common/file_utils.cc +++ b/mindspore/lite/src/common/file_utils.cc @@ -122,7 +122,13 @@ char *ReadFileSegment(const std::string &file, int64_t offset, int64_t len) { } ifs.seekg(0, std::ios::end); - size_t total_size = ifs.tellg(); + auto goffset = ifs.tellg(); + if (goffset < 0) { + MS_LOG(ERROR) << "read file range failed"; + ifs.close(); + return nullptr; + } + size_t total_size = static_cast(goffset); if (offset_pos + len_pos > total_size) { MS_LOG(ERROR) << "file segment out of range"; ifs.close(); @@ -171,7 +177,14 @@ char *ReadFile(const char *file, size_t *size) { } ifs->seekg(0, std::ios::end); - *size = ifs->tellg(); + auto goffset = ifs->tellg(); + if (goffset < 0) { + MS_LOG(ERROR) << "read file range failed"; + ifs->close(); + delete ifs; + return nullptr; + } + *size = static_cast(goffset); auto buf = new (std::nothrow) char[*size]; if (buf == nullptr) { MS_LOG(ERROR) << "malloc buf failed, file: " << file;