OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[coverity][samples] Fix BMP reader coverity issues (CID 2354064) (#30655)
Pawel Raaszf98b8b1dcb30
diff --git a/samples/c/common/opencv_c_wrapper/src/bmp_reader.c b/samples/c/common/opencv_c_wrapper/src/bmp_reader.cindex 7e3fa42ae0d..cd955bf5176 100644--- a/samples/c/common/opencv_c_wrapper/src/bmp_reader.c+++ b/samples/c/common/opencv_c_wrapper/src/bmp_reader.c@@ -15,6 +15,8 @@fclose(input); \return x;+#define OPENVINO_SAMPLE_BMP_DIM_LIMIT (32 * 1024)+int readBmpImage(const char* fileName, BitMap* image) {size_t cnt;FILE* input = 0;@@ -67,7 +69,19 @@ int readBmpImage(const char* fileName, BitMap* image) {CLEANUP_AND_RETURN(2);}- image->width = abs(image->infoHeader.width);+ // Limit BMP image size+ if ((image->infoHeader.width < 0) || (image->infoHeader.width > OPENVINO_SAMPLE_BMP_DIM_LIMIT)) {+ printf("[BMP] wrong width \n");+ CLEANUP_AND_RETURN(2);+ }++ if ((image->infoHeader.height < -OPENVINO_SAMPLE_BMP_DIM_LIMIT) ||+ (image->infoHeader.height > OPENVINO_SAMPLE_BMP_DIM_LIMIT)) {+ printf("[BMP] wrong height \n");+ CLEANUP_AND_RETURN(2);+ }++ image->width = image->infoHeader.width;image->height = abs(image->infoHeader.height);if (image->infoHeader.bits != 24) {@@ -80,14 +94,13 @@ int readBmpImage(const char* fileName, BitMap* image) {CLEANUP_AND_RETURN(4);}- size_t padSize = ((size_t)image->width) & 3U;- int row_size = image->width * 3;+ const size_t pad_size = ((size_t)image->width) & 3U;char pad[3];- int size = row_size * image->height;- size *= sizeof(char);+ const int row_size = image->width * 3;+ const int size = sizeof(char) * row_size * image->height;- if (size < 0) {- printf("[BMP] image size is too large\n");+ if (size <= 0) {+ printf("[BMP] Cannot allocate memory for image (can be too big)\n");CLEANUP_AND_RETURN(3);}@@ -104,15 +117,15 @@ int readBmpImage(const char* fileName, BitMap* image) {// reading by rows in invert verticallyint i;- for (i = 0; i < image->height; i++) {+ for (i = 0; i < image->height; ++i) {int storeAt = image->infoHeader.height < 0 ? i : image->height - 1 - i;cnt = fread(image->data + row_size * storeAt, row_size, sizeof(unsigned char), input);if (cnt != sizeof(unsigned char)) {printf("[BMP] file read error\n");CLEANUP_AND_RETURN(2);}- cnt = fread(pad, padSize, sizeof(unsigned char), input);- if ((padSize != 0 && cnt != 0) && (cnt != sizeof(unsigned char))) {+ cnt = fread(pad, pad_size, sizeof(unsigned char), input);+ if ((pad_size != 0 && cnt != 0) && (cnt != sizeof(unsigned char))) {printf("[BMP] file read error\n");CLEANUP_AND_RETURN(2);}diff --git a/samples/c/hello_classification/infer_result_util.c b/samples/c/hello_classification/infer_result_util.cindex 1a4e5d52442..31ab01e3541 100644--- a/samples/c/hello_classification/infer_result_util.c+++ b/samples/c/hello_classification/infer_result_util.c@@ -29,8 +29,10 @@ struct infer_result* tensor_to_infer_result(ov_tensor_t* tensor, size_t* result_*result_size = output_shape.dims[1];struct infer_result* results = (struct infer_result*)malloc(sizeof(struct infer_result) * (*result_size));- if (!results)+ if (!results) {+ ov_shape_free(&output_shape);return NULL;+ }void* data = NULL;status = ov_tensor_data(tensor, &data);diff --git a/samples/cpp/common/format_reader/src/bmp.cpp b/samples/cpp/common/format_reader/src/bmp.cppindex 9d4cae1bc70..caef88e1eec 100644--- a/samples/cpp/common/format_reader/src/bmp.cpp+++ b/samples/cpp/common/format_reader/src/bmp.cpp@@ -35,7 +35,19 @@ BitMap::BitMap(const string& filename) {input.read(reinterpret_cast<char*>(&infoHeader), sizeof(BmpInfoHeader));- bool rowsReversed = infoHeader.height < 0;+ // Limit BMP image size+ constexpr auto bmp_dim_limit = 32 * 1024;+ if ((infoHeader.width < 0) || (infoHeader.width > bmp_dim_limit)) {+ std::cerr << "[BMP] wrong width\n";+ return;+ }++ if ((infoHeader.height < -bmp_dim_limit) || (infoHeader.height > bmp_dim_limit)) {+ std::cerr << "[BMP] wrong height\n";+ return;+ }++ const auto rowsReversed = infoHeader.height < 0;_width = infoHeader.width;_height = abs(infoHeader.height);_shape.push_back(_height);@@ -50,18 +62,19 @@ BitMap::BitMap(const string& filename) {cerr << "[BMP] compression not supported\n";}- int padSize = _width & 3;+ const auto padSize = _width & 3;char pad[3];- size_t size = _width * _height * 3;+ const auto row_size = _width * 3;+ const auto size = row_size * _height;_data.reset(new unsigned char[size], std::default_delete<unsigned char[]>());input.seekg(header.offset, ios::beg);// reading by rows in invert vertically- for (uint32_t i = 0; i < _height; i++) {- uint32_t storeAt = rowsReversed ? i : (uint32_t)_height - 1 - i;- input.read(reinterpret_cast<char*>(_data.get()) + _width * 3 * storeAt, _width * 3);+ for (size_t i = 0; i < _height; ++i) {+ auto storeAt = rowsReversed ? i : static_cast<size_t>(_height - 1 - i);+ input.read(reinterpret_cast<char*>(_data.get()) + row_size * storeAt, row_size);input.read(pad, padSize);}}