From 4409a74dcfd82af8b962d4e6b31f9b92934fff81 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 8 Jun 2021 10:16:37 +0300 Subject: [PATCH] samples: Fixed klocwork issues in speech (#6066) --- .../samples/speech_sample/fileutils.cpp | 39 +++++++++++-------- thirdparty/cnpy/cnpy.cpp | 12 +++--- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/inference-engine/samples/speech_sample/fileutils.cpp b/inference-engine/samples/speech_sample/fileutils.cpp index f3211a21a4b..102cca25297 100644 --- a/inference-engine/samples/speech_sample/fileutils.cpp +++ b/inference-engine/samples/speech_sample/fileutils.cpp @@ -108,15 +108,18 @@ void NumpyFile::GetFileInfo(const char* fileName, uint32_t numArrayToFindSize, u cnpy::npz_t my_npz1 = cnpy::npz_load(fileName); auto it = my_npz1.begin(); std::advance(it, numArrayToFindSize); + if (it != my_npz1.end()) { + numArrays = my_npz1.size(); + cnpy::NpyArray my_npy = it->second; + numMemoryBytes = my_npy.data_holder->size(); - numArrays = my_npz1.size(); - cnpy::NpyArray my_npy = it->second; - numMemoryBytes = my_npy.data_holder->size(); - - if (ptrNumArrays != NULL) - *ptrNumArrays = numArrays; - if (ptrNumMemoryBytes != NULL) - *ptrNumMemoryBytes = numMemoryBytes; + if (ptrNumArrays != NULL) + *ptrNumArrays = numArrays; + if (ptrNumMemoryBytes != NULL) + *ptrNumMemoryBytes = numMemoryBytes; + } else { + throw std::runtime_error(std::string("Failed to get info %s GetFileInfo()!\n") + fileName); + } } void NumpyFile::LoadFile(const char* fileName, uint32_t arrayIndex, std::string& ptrName, std::vector& memory, uint32_t* ptrNumRows, @@ -124,16 +127,20 @@ void NumpyFile::LoadFile(const char* fileName, uint32_t arrayIndex, std::string& cnpy::npz_t my_npz1 = cnpy::npz_load(fileName); auto it = my_npz1.begin(); std::advance(it, arrayIndex); - ptrName = it->first; - cnpy::NpyArray my_npy = it->second; - *ptrNumRows = my_npy.shape[0]; - *ptrNumColumns = my_npy.shape[1]; + if (it != my_npz1.end()) { + ptrName = it->first; + cnpy::NpyArray my_npy = it->second; + *ptrNumRows = my_npy.shape[0]; + *ptrNumColumns = my_npy.shape[1]; - for (size_t i = 0; i < my_npy.data_holder->size(); i++) { - memory.at(i) = my_npy.data_holder->at(i); + for (size_t i = 0; i < my_npy.data_holder->size(); i++) { + memory.at(i) = my_npy.data_holder->at(i); + } + + *ptrNumBytesPerElement = sizeof(float); + } else { + throw std::runtime_error(std::string("Failed to open %s for reading in LoadFile()!\n") + fileName); } - - *ptrNumBytesPerElement = sizeof(float); } void NumpyFile::SaveFile(const char* fileName, bool shouldAppend, std::string name, void* ptrMemory, uint32_t numRows, uint32_t numColumns) { diff --git a/thirdparty/cnpy/cnpy.cpp b/thirdparty/cnpy/cnpy.cpp index a3a3e0ef406..26d0614bca1 100644 --- a/thirdparty/cnpy/cnpy.cpp +++ b/thirdparty/cnpy/cnpy.cpp @@ -183,9 +183,9 @@ void cnpy::parse_zip_footer(FILE* fp, uint16_t& nrecs, size_t& global_header_siz } cnpy::NpyArray load_the_npy_file(FILE* fp) { - std::vector shape; - size_t word_size; - bool fortran_order; + std::vector shape(0); + size_t word_size = 0; + bool fortran_order = false; cnpy::parse_npy_header(fp,word_size,shape,fortran_order); if (word_size >= 0 && word_size < ULLONG_MAX) { cnpy::NpyArray arr(shape, word_size, fortran_order); @@ -225,9 +225,9 @@ cnpy::NpyArray load_the_npz_array(FILE* fp, uint32_t compr_bytes, uint32_t uncom err = inflate(&d_stream, Z_FINISH); err = inflateEnd(&d_stream); - std::vector shape; - size_t word_size; - bool fortran_order; + std::vector shape(0); + size_t word_size = 0; + bool fortran_order = false; cnpy::parse_npy_header(&buffer_uncompr[0],word_size,shape,fortran_order); if (word_size >= 0 && word_size < ULLONG_MAX) { cnpy::NpyArray array(shape, word_size, fortran_order);