From: Inki Dae Date: Fri, 12 Aug 2022 05:29:00 +0000 (+0900) Subject: mv_machine_learning: fix coverity issues X-Git-Tag: submit/tizen/20220812.070529~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F94%2F279594%2F2;p=platform%2Fcore%2Fapi%2Fmediavision.git mv_machine_learning: fix coverity issues [Version] : 0.23.14 [Issue type] : bug fix Fixed coverity issues. Change-Id: I2e376d517d99722366a05f8ab584858808ec2194 Signed-off-by: Inki Dae --- diff --git a/mv_machine_learning/face_recognition/src/face_recognition.cpp b/mv_machine_learning/face_recognition/src/face_recognition.cpp index 66473cc..8a6505c 100644 --- a/mv_machine_learning/face_recognition/src/face_recognition.cpp +++ b/mv_machine_learning/face_recognition/src/face_recognition.cpp @@ -62,14 +62,20 @@ void FaceRecognition::CheckFeatureVectorFile(unique_ptr& o // Change new feature vector file to existing one in case that current process is terminated just after removing existing feature vector file but // new feature vector file isn't changed to existing one yet. if (FaceRecogUtil::IsFileExist(new_fvm->GetFileName()) && !FaceRecogUtil::IsFileExist(old_fvm->GetFileName())) { - rename(new_fvm->GetFileName().c_str(), old_fvm->GetFileName().c_str()); + int ret = ::rename(new_fvm->GetFileName().c_str(), old_fvm->GetFileName().c_str()); + if (ret) + throw InvalidOperation("Fail to rename new feature vector file to original one."); + return; } // Make sure to remove a temp file in case that current process is terminated just after generating new feature vector file // which is not correct file but existing one isn't removed. In this cae, existing file is used again. - if (FaceRecogUtil::IsFileExist(new_fvm->GetFileName())) - remove(new_fvm->GetFileName().c_str()); + if (FaceRecogUtil::IsFileExist(new_fvm->GetFileName())) { + int ret = ::remove(new_fvm->GetFileName().c_str()); + if (ret) + throw InvalidOperation("Fail to remove new feature vector file."); + } } unique_ptr FaceRecognition::CreateDSM(const mv_inference_backend_type_e backend_type) @@ -78,7 +84,7 @@ unique_ptr FaceRecognition::CreateDSM(const mv_inference_backend case MV_INFERENCE_BACKEND_NNTRAINER: return make_unique(); default: - throw InvalidParameter("Invalid training engine backend type."); + break; } throw InvalidParameter("Invalid training engine backend type."); @@ -90,7 +96,7 @@ unique_ptr FaceRecognition::CreateFVM(const mv_inference_b case MV_INFERENCE_BACKEND_NNTRAINER: return make_unique(file_name); default: - throw InvalidParameter("Invalid training engine backend type."); + break; } throw InvalidParameter("Invalid training engine backend type."); @@ -99,65 +105,85 @@ unique_ptr FaceRecognition::CreateFVM(const mv_inference_b void FaceRecognition::UpdateDataSet(unique_ptr& data_set, vector& feature_vec, const int label_idx, const int label_cnt) { size_t data_set_cnt = 0; - auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path); - auto fvm_new = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path + ".new"); - // Make sure feature vector file. - CheckFeatureVectorFile(fvm, fvm_new); + try { + auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path); + auto fvm_new = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path + ".new"); - data_set = CreateDSM(_config.training_engine_backend_type); + // Make sure feature vector file. + CheckFeatureVectorFile(fvm, fvm_new); - // 1. If data set file exists then load the file to DataSetManager object first - // and then write them to the data set file with updated label value, and then - // write a new dataset to the data set file. - // Otherwise, it writes only new data set to the data set file. - if (FaceRecogUtil::IsFileExist(fvm->GetFileName())) { - data_set->LoadDataSet(fvm->GetFileName()); + data_set = CreateDSM(_config.training_engine_backend_type); - vector> feature_vectors = data_set->GetData(); - vector label_idx_vectors = data_set->GetLabelIdx(); + // 1. If data set file exists then load the file to DataSetManager object first + // and then write them to the data set file with updated label value, and then + // write a new dataset to the data set file. + // Otherwise, it writes only new data set to the data set file. + if (FaceRecogUtil::IsFileExist(fvm->GetFileName())) { + data_set->LoadDataSet(fvm->GetFileName()); - // 1) Write existing feature vectors and its one-hot encoding table considered - // for new label count to the data set file. - for (unsigned int idx = 0; idx < feature_vectors.size(); ++idx) - fvm_new->WriteFeatureVec(feature_vectors[idx], label_cnt, label_idx_vectors[idx]); + vector> feature_vectors = data_set->GetData(); + vector label_idx_vectors = data_set->GetLabelIdx(); + + // 1) Write existing feature vectors and its one-hot encoding table considered + // for new label count to the data set file. + for (unsigned int idx = 0; idx < feature_vectors.size(); ++idx) + fvm_new->WriteFeatureVec(feature_vectors[idx], label_cnt, label_idx_vectors[idx]); - data_set_cnt += feature_vectors.size(); + data_set_cnt += feature_vectors.size(); - // 2) If same feature vector isn't duplicated then write the feature vector to data set file. - if (!data_set->IsFeatureVectorDuplicated(feature_vec)) { + // 2) If same feature vector isn't duplicated then write the feature vector to data set file. + if (!data_set->IsFeatureVectorDuplicated(feature_vec)) { + fvm_new->WriteFeatureVec(feature_vec, label_cnt, label_idx); + LOGD("Added a new feature vector to data set file."); + data_set_cnt++; + } + } else { + // 1) Write only a new data set to the data st file. fvm_new->WriteFeatureVec(feature_vec, label_cnt, label_idx); LOGD("Added a new feature vector to data set file."); data_set_cnt++; } - } else { - // 1) Write only a new data set to the data st file. - fvm_new->WriteFeatureVec(feature_vec, label_cnt, label_idx); - LOGD("Added a new feature vector to data set file."); - data_set_cnt++; - } - // 2. Write feature vector header. - fvm_new->WriteHeader(feature_vec.size(), label_cnt, data_set_cnt); + // 2. Write feature vector header. + fvm_new->WriteHeader(feature_vec.size(), label_cnt, data_set_cnt); + + int ret = 0; + + // 3. Change new data file to existing one. + if (FaceRecogUtil::IsFileExist(fvm->GetFileName())) { + ret = ::remove(fvm->GetFileName().c_str()); + if (ret) + throw InvalidOperation("Fail to remove feature vector file."); + } - // 3. Change new data file to existing one. - remove(fvm->GetFileName().c_str()); - rename(fvm_new->GetFileName().c_str(), fvm->GetFileName().c_str()); + ret = ::rename(fvm_new->GetFileName().c_str(), fvm->GetFileName().c_str()); + if (ret) + throw InvalidOperation("Fail to rename new feature vector file to original one."); - data_set->Clear(); - data_set->LoadDataSet(fvm->GetFileName()); + data_set->Clear(); + data_set->LoadDataSet(fvm->GetFileName()); + } catch (const BaseException& e) { + LOGE("%s", e.what()); + throw e; + } } void FaceRecognition::UpdateDataSet(unique_ptr& data_set) { - data_set = CreateDSM(_config.training_engine_backend_type); + try { + data_set = CreateDSM(_config.training_engine_backend_type); - auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path); + auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path); - if (FaceRecogUtil::IsFileExist(fvm->GetFileName()) == false) - throw InvalidOperation("Feature vector file not found."); + if (FaceRecogUtil::IsFileExist(fvm->GetFileName()) == false) + throw InvalidOperation("Feature vector file not found."); - data_set->LoadDataSet(fvm->GetFileName()); + data_set->LoadDataSet(fvm->GetFileName()); + } catch (const BaseException& e) { + LOGE("%s", e.what()); + throw e; + } } void FaceRecognition::SetConfig(FaceRecognitionConfig& config) @@ -568,9 +594,18 @@ int FaceRecognition::DeleteLabel(string label_name) fvm_new->WriteHeader(feature_vectors[0].size(), label_cnt, data_set_cnt); - // Change new data file to existing one. - remove(fvm->GetFileName().c_str()); - rename(fvm_new->GetFileName().c_str(), fvm->GetFileName().c_str()); + int ret = 0; + + if (FaceRecogUtil::IsFileExist(fvm->GetFileName())) { + // Change new data file to existing one. + ret = ::remove(fvm->GetFileName().c_str()); + if (ret) + throw InvalidOperation("Fail to remove feature vector file."); + } + + ret = ::rename(fvm_new->GetFileName().c_str(), fvm->GetFileName().c_str()); + if (ret) + throw InvalidOperation("Fail to rename new feature vector file to original one."); if (data_set_cnt == 0) { _training_model->RemoveModel(); diff --git a/mv_machine_learning/face_recognition/src/simple_shot.cpp b/mv_machine_learning/face_recognition/src/simple_shot.cpp index 8d31e5a..c3a5ff8 100644 --- a/mv_machine_learning/face_recognition/src/simple_shot.cpp +++ b/mv_machine_learning/face_recognition/src/simple_shot.cpp @@ -27,6 +27,7 @@ #include "simple_shot.h" #include "data_set_manager.h" #include "feature_vector_manager.h" +#include "file_util.h" using namespace std; using namespace TrainingEngineInterface::Common; @@ -119,14 +120,22 @@ void SimpleShot::ConfigureModel(int num_of_class) void SimpleShot::SaveModel(const string file_path) { - int ret = TRAINING_ENGINE_ERROR_NONE; - string bin_file_path = file_path.substr(0, file_path.find('.')) + ".bin"; + int ret = 0; // NNStreamer returns an error if internal model file(ini and bin files) exists before generating it. // So remove existing files. - ::remove(bin_file_path.c_str()); - ::remove(file_path.c_str()); + if (FaceRecogUtil::IsFileExist(bin_file_path)) { + ret = ::remove(bin_file_path.c_str()); + if (ret) + throw InvalidOperation("Fail to remove internal model file."); + } + + if (FaceRecogUtil::IsFileExist(file_path)) { + ret = ::remove(file_path.c_str()); + if (ret) + throw InvalidOperation("Fail to remove internal model file."); + } ret = _training->SaveModel(_model.get(), file_path); if (ret != TRAINING_ENGINE_ERROR_NONE) @@ -135,11 +144,19 @@ void SimpleShot::SaveModel(const string file_path) void SimpleShot::RemoveModel(const string file_path) { - int ret = TRAINING_ENGINE_ERROR_NONE; - string bin_file_path = file_path.substr(0, file_path.find('.')) + ".bin"; + int ret = 0; // Remove existing files forcely. - ::remove(bin_file_path.c_str()); - ::remove(file_path.c_str()); + if (FaceRecogUtil::IsFileExist(bin_file_path)) { + ret = ::remove(bin_file_path.c_str()); + if (ret) + throw InvalidOperation("Fail to remove internal model file."); + } + + if (FaceRecogUtil::IsFileExist(file_path)) { + ret = ::remove(file_path.c_str()); + if (ret) + throw InvalidOperation("Fail to remove internal model file."); + } } \ No newline at end of file diff --git a/mv_machine_learning/inference/src/mv_inference_open.cpp b/mv_machine_learning/inference/src/mv_inference_open.cpp index a9e30e3..da48ac8 100644 --- a/mv_machine_learning/inference/src/mv_inference_open.cpp +++ b/mv_machine_learning/inference/src/mv_inference_open.cpp @@ -216,7 +216,7 @@ static int configure_model_open(Inference *pInfer, mv_engine_config_h engine_con if (std::string(modelMetaFilePath).empty()) { LOGW("Skip ParseMetadata and run without Metadata"); - goto release_model_meta_file_path; + goto release_model_user_file_path; } if (!IsJsonFile(std::string(modelMetaFilePath))) { diff --git a/mv_machine_learning/training/src/label_manager.cpp b/mv_machine_learning/training/src/label_manager.cpp index 11f1881..6a4deb4 100644 --- a/mv_machine_learning/training/src/label_manager.cpp +++ b/mv_machine_learning/training/src/label_manager.cpp @@ -132,10 +132,17 @@ unsigned int LabelManager::RemoveLabel(const string given_label) if (label_index == 0) { // Just keep original version in case of no copy so drop the new label file. - remove(new_label_file.c_str()); + int ret = ::remove(new_label_file.c_str()); + if (ret) + throw InvalidOperation("Fail to remove label file."); } else { - remove(_label_file.c_str()); - rename(new_label_file.c_str(), _label_file.c_str()); + int ret = ::remove(_label_file.c_str()); + if (ret) + throw InvalidOperation("Fail to remove label file."); + + ret = ::rename(new_label_file.c_str(), _label_file.c_str()); + if (ret) + throw InvalidOperation("Fail to rename new labal file to original one."); } return label_index; diff --git a/packaging/capi-media-vision.spec b/packaging/capi-media-vision.spec index 871bd15..7121aa7 100644 --- a/packaging/capi-media-vision.spec +++ b/packaging/capi-media-vision.spec @@ -1,6 +1,6 @@ Name: capi-media-vision Summary: Media Vision library for Tizen Native API -Version: 0.23.13 +Version: 0.23.14 Release: 0 Group: Multimedia/Framework License: Apache-2.0 and BSD-3-Clause