From b8ac041da973478dd4ded4f9de7efa795cae01c8 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 18 Feb 2022 08:08:09 +0300 Subject: [PATCH] Fixed coverity issues in samples (#10421) * Fixed coverity issues * Fixed coverity isuues samples part 2 * Fixed code style * Delete goto * update after comments --- .../c/common/opencv_c_wrapper/bmp_reader.c | 63 ++++++++----------- samples/cpp/benchmark_app/inputs_filling.cpp | 4 ++ .../cpp/benchmark_app/statistics_report.cpp | 2 + .../cpp/benchmark_app/statistics_report.hpp | 6 +- .../common/utils/include/samples/common.hpp | 3 +- samples/cpp/speech_sample/utils.hpp | 9 ++- 6 files changed, 44 insertions(+), 43 deletions(-) diff --git a/samples/c/common/opencv_c_wrapper/bmp_reader.c b/samples/c/common/opencv_c_wrapper/bmp_reader.c index 05ec4861901..848415d204a 100644 --- a/samples/c/common/opencv_c_wrapper/bmp_reader.c +++ b/samples/c/common/opencv_c_wrapper/bmp_reader.c @@ -4,15 +4,20 @@ #include #include +#define CLEANUP_AND_RETURN(x) \ + if (x && !image && !image->data) \ + free(image->data); \ + if (input != NULL) \ + fclose(input); \ + return x; + int readBmpImage(const char* fileName, BitMap* image) { size_t cnt; - int status = 0; FILE* input = 0; if (NULL == fileName || NULL == image) { printf("[BMP] bad arguments\n"); - status = -1; - goto Exit; + CLEANUP_AND_RETURN(-1); } memset(image, 0, sizeof(BitMap)); @@ -20,49 +25,42 @@ int readBmpImage(const char* fileName, BitMap* image) { input = fopen(fileName, "rb"); if (input == NULL) { printf("[BMP] file %s is not opened\n", fileName); - status = 1; - goto Exit; + CLEANUP_AND_RETURN(-1); } cnt = fread(&image->header.type, sizeof(image->header.type), sizeof(unsigned char), input); if (cnt != sizeof(image->header.type)) { printf("[BMP] file read error\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(-2); } if (image->header.type != 'M' * 256 + 'B') { printf("[BMP] file is not bmp type\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(2); } cnt = fread(&image->header.size, sizeof(image->header.size), sizeof(unsigned char), input); if (cnt != sizeof(image->header.size)) { printf("[BMP] file read error\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(2); } cnt = fread(&image->header.reserved, sizeof(image->header.reserved), sizeof(unsigned char), input); if (cnt != sizeof(image->header.reserved)) { printf("[BMP] file read error\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(2); } cnt = fread(&image->header.offset, sizeof(image->header.offset), sizeof(unsigned char), input); if (cnt != sizeof(image->header.offset)) { printf("[BMP] file read error\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(2); } cnt = fread(&image->infoHeader, sizeof(BmpInfoHeader), sizeof(unsigned char), input); if (cnt != sizeof(image->header.offset)) { printf("[BMP] file read error\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(2); } image->width = image->infoHeader.width; @@ -70,12 +68,12 @@ int readBmpImage(const char* fileName, BitMap* image) { if (image->infoHeader.bits != 24) { printf("[BMP] 24bpp only supported. But input has: %d\n", image->infoHeader.bits); - return 3; + CLEANUP_AND_RETURN(3); } if (image->infoHeader.compression != 0) { printf("[BMP] compression not supported\n"); - return 4; + CLEANUP_AND_RETURN(4); } int padSize = image->width & 3; @@ -86,42 +84,31 @@ int readBmpImage(const char* fileName, BitMap* image) { image->data = malloc(sizeof(char) * size); if (NULL == image->data) { printf("[BMP] memory allocation failed\n"); - return 5; + CLEANUP_AND_RETURN(5); } if (0 != fseek(input, image->header.offset, SEEK_SET)) { printf("[BMP] file seek error\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(2); } // reading by rows in invert vertically int i; - for (i = 0; i < image->height; i++) { - unsigned int storeAt = image->infoHeader.height < 0 ? i : (unsigned int)image->height - 1 - i; + int image_height = image->height; + for (i = 0; i < image_height; i++) { + unsigned int storeAt = image->infoHeader.height < 0 ? i : (unsigned int)image_height - 1 - i; cnt = fread(image->data + row_size * storeAt, row_size, sizeof(unsigned char), input); if (cnt != row_size) { printf("[BMP] file read error\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(2); } cnt = fread(pad, padSize, sizeof(unsigned char), input); if (cnt != padSize) { printf("[BMP] file read error\n"); - status = 2; - goto Exit; + CLEANUP_AND_RETURN(2); } } -Exit: - if (0 != status && NULL != image && NULL != image->data) { - free(image->data); - } - - if (NULL != input) { - fclose(input); - } - - return status; + return 0; } diff --git a/samples/cpp/benchmark_app/inputs_filling.cpp b/samples/cpp/benchmark_app/inputs_filling.cpp index c057daaa4b5..00f40469f3d 100644 --- a/samples/cpp/benchmark_app/inputs_filling.cpp +++ b/samples/cpp/benchmark_app/inputs_filling.cpp @@ -360,6 +360,7 @@ std::string get_test_info_stream_header(benchmark_app::InputInfo& inputInfo) { std::map get_tensors(std::map> inputFiles, std::vector& app_inputs_info) { + std::ios::fmtflags fmt(std::cout.flags()); std::map tensors; if (app_inputs_info.empty()) { throw std::logic_error("Inputs Info for network is empty!"); @@ -515,6 +516,7 @@ std::map get_tensors(std::map get_tensors_static_case(const std::vecto const size_t& batchSize, benchmark_app::InputsInfo& app_inputs_info, size_t requestsNum) { + std::ios::fmtflags fmt(std::cout.flags()); std::map blobs; std::vector> net_input_im_sizes; @@ -687,6 +690,7 @@ std::map get_tensors_static_case(const std::vecto slog::info << std::left << std::setw(maxNameWidth + 2) << inputLog.first << inputLog.second << slog::endl; } } + std::cout.flags(fmt); return blobs; } diff --git a/samples/cpp/benchmark_app/statistics_report.cpp b/samples/cpp/benchmark_app/statistics_report.cpp index 650861fddf6..89f8a74e747 100644 --- a/samples/cpp/benchmark_app/statistics_report.cpp +++ b/samples/cpp/benchmark_app/statistics_report.cpp @@ -243,8 +243,10 @@ const nlohmann::json StatisticsReportJSON::perf_counters_to_json( } void LatencyMetrics::write_to_stream(std::ostream& stream) const { + std::ios::fmtflags fmt(std::cout.flags()); stream << data_shape << ";" << std::fixed << std::setprecision(2) << median_or_percentile << ";" << avg << ";" << min << ";" << max; + std::cout.flags(fmt); } void LatencyMetrics::write_to_slog() const { diff --git a/samples/cpp/benchmark_app/statistics_report.hpp b/samples/cpp/benchmark_app/statistics_report.hpp index ceadc5f49b8..d54d46e58b2 100644 --- a/samples/cpp/benchmark_app/statistics_report.hpp +++ b/samples/cpp/benchmark_app/statistics_report.hpp @@ -96,9 +96,9 @@ public: std::string csv_name; std::string json_name; - int i_val; - double d_val; - unsigned long long ull_val; + int i_val = 0; + double d_val = 0; + unsigned long long ull_val = 0; std::string s_val; LatencyMetrics metrics_val; Type type; diff --git a/samples/cpp/common/utils/include/samples/common.hpp b/samples/cpp/common/utils/include/samples/common.hpp index 532e361402d..0f353a8a775 100644 --- a/samples/cpp/common/utils/include/samples/common.hpp +++ b/samples/cpp/common/utils/include/samples/common.hpp @@ -993,7 +993,7 @@ static UNUSED void printPerformanceCounts(std::vector perform if (bshowHeader) { stream << std::endl << "performance counts:" << std::endl << std::endl; } - + std::ios::fmtflags fmt(std::cout.flags()); for (const auto& it : performanceData) { std::string toPrint(it.node_name); const int maxLayerName = 30; @@ -1028,6 +1028,7 @@ static UNUSED void printPerformanceCounts(std::vector perform std::cout << std::endl; std::cout << "Full device name: " << deviceName << std::endl; std::cout << std::endl; + std::cout.flags(fmt); } static UNUSED void printPerformanceCounts(ov::InferRequest request, diff --git a/samples/cpp/speech_sample/utils.hpp b/samples/cpp/speech_sample/utils.hpp index a07011e15a6..4626cf55207 100644 --- a/samples/cpp/speech_sample/utils.hpp +++ b/samples/cpp/speech_sample/utils.hpp @@ -291,6 +291,7 @@ void print_performance_counters(std::map const& const uint64_t numberOfFramesOnHw, std::string FLAGS_d) { #if !defined(__arm__) && !defined(_M_ARM) && !defined(__aarch64__) && !defined(_M_ARM64) + std::ios::fmtflags fmt(std::cout.flags()); stream << std::endl << "Performance counts:" << std::endl; stream << std::setw(10) << std::right << "" << "Counter descriptions"; @@ -307,7 +308,12 @@ void print_performance_counters(std::map const& for (const auto& it : utterancePerfMap) { std::string const& counter_name = it.first; float current_units_us = static_cast(it.second.real_time.count()) / freq; - float call_units_us = current_units_us / numberOfFrames; + float call_units_us = 0; + if (numberOfFrames == 0) { + throw std::logic_error("Number off frames = 0, division by zero."); + } else { + call_units_us = current_units_us / numberOfFrames; + } if (FLAGS_d.find("GNA") != std::string::npos) { stream << std::setw(30) << std::left << counter_name.substr(4, counter_name.size() - 1); } else { @@ -324,6 +330,7 @@ void print_performance_counters(std::map const& stream << "Number of frames delivered to GNA HW: " << numberOfFramesOnHw; stream << "/" << numberOfFrames; stream << std::endl; + std::cout.flags(fmt); #endif }