Fixed coverity issues in samples (#10421)

* Fixed coverity issues

* Fixed coverity isuues samples part 2

* Fixed code style

* Delete goto

* update after comments
This commit is contained in:
Anton Romanov
2022-02-18 08:08:09 +03:00
committed by GitHub
parent 76ade7a7d0
commit b8ac041da9
6 changed files with 44 additions and 43 deletions

View File

@@ -4,15 +4,20 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}

View File

@@ -360,6 +360,7 @@ std::string get_test_info_stream_header(benchmark_app::InputInfo& inputInfo) {
std::map<std::string, ov::TensorVector> get_tensors(std::map<std::string, std::vector<std::string>> inputFiles,
std::vector<benchmark_app::InputsInfo>& app_inputs_info) {
std::ios::fmtflags fmt(std::cout.flags());
std::map<std::string, ov::TensorVector> tensors;
if (app_inputs_info.empty()) {
throw std::logic_error("Inputs Info for network is empty!");
@@ -515,6 +516,7 @@ std::map<std::string, ov::TensorVector> get_tensors(std::map<std::string, std::v
slog::info << std::left << std::setw(maxNameWidth + 2) << inputLog.first << inputLog.second << slog::endl;
}
}
std::cout.flags(fmt);
return tensors;
}
@@ -523,6 +525,7 @@ std::map<std::string, ov::TensorVector> 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<std::string, ov::TensorVector> blobs;
std::vector<std::pair<size_t, size_t>> net_input_im_sizes;
@@ -687,6 +690,7 @@ std::map<std::string, ov::TensorVector> 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;
}

View File

@@ -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 {

View File

@@ -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;

View File

@@ -993,7 +993,7 @@ static UNUSED void printPerformanceCounts(std::vector<ov::ProfilingInfo> 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<ov::ProfilingInfo> 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,

View File

@@ -291,6 +291,7 @@ void print_performance_counters(std::map<std::string, ov::ProfilingInfo> 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<std::string, ov::ProfilingInfo> const&
for (const auto& it : utterancePerfMap) {
std::string const& counter_name = it.first;
float current_units_us = static_cast<float>(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<std::string, ov::ProfilingInfo> const&
stream << "Number of frames delivered to GNA HW: " << numberOfFramesOnHw;
stream << "/" << numberOfFrames;
stream << std::endl;
std::cout.flags(fmt);
#endif
}