diff --git a/inference-engine/ie_bridges/c/samples/common/opencv_c_wraper/opencv_c_wraper.cpp b/inference-engine/ie_bridges/c/samples/common/opencv_c_wraper/opencv_c_wraper.cpp index fefeaee5099..8fa56fdc1e4 100644 --- a/inference-engine/ie_bridges/c/samples/common/opencv_c_wraper/opencv_c_wraper.cpp +++ b/inference-engine/ie_bridges/c/samples/common/opencv_c_wraper/opencv_c_wraper.cpp @@ -31,8 +31,13 @@ int image_read(const char *img_path, c_mat_t *img) { img->mat_width = mat.size().width; img->mat_height = mat.size().height; img->mat_type = mat.type(); - img->mat_data_size = img->mat_channels * img->mat_width * img->mat_height; + img->mat_data_size = mat.elemSize() * img->mat_width * img->mat_height; img->mat_data = (unsigned char *)malloc(sizeof(unsigned char) * img->mat_data_size); + + if (img->mat_data == NULL) { + return -1; + } + for (int i = 0; i < img->mat_data_size; ++i) { img->mat_data[i] = mat.data[i]; } @@ -54,8 +59,13 @@ int image_resize(const c_mat_t *src_img, c_mat_t *dst_img, const int width, cons dst_img->mat_width = mat_dst.size().width; dst_img->mat_height = mat_dst.size().height; dst_img->mat_type = mat_dst.type(); - dst_img->mat_data_size = dst_img->mat_channels * dst_img->mat_width * dst_img->mat_height; + dst_img->mat_data_size = mat_dst.elemSize() * dst_img->mat_width * dst_img->mat_height; dst_img->mat_data = (unsigned char *)malloc(sizeof(unsigned char) * dst_img->mat_data_size); + + if (dst_img->mat_data == NULL) { + return -1; + } + for (int i = 0; i < dst_img->mat_data_size; ++i) { dst_img->mat_data[i] = mat_dst.data[i]; } diff --git a/inference-engine/ie_bridges/c/samples/hello_classification/main.c b/inference-engine/ie_bridges/c/samples/hello_classification/main.c index d961bce6180..8ad4839c20a 100644 --- a/inference-engine/ie_bridges/c/samples/hello_classification/main.c +++ b/inference-engine/ie_bridges/c/samples/hello_classification/main.c @@ -39,6 +39,9 @@ struct classify_res *output_blob_to_classify_res(ie_blob_t *blob, size_t *n) { *n = output_dim.dims[1]; struct classify_res *cls = (struct classify_res *)malloc(sizeof(struct classify_res) * (*n)); + if (!cls) { + return NULL; + } ie_blob_buffer_t blob_cbuffer; status = ie_blob_get_cbuffer(blob, &blob_cbuffer); diff --git a/inference-engine/ie_bridges/c/samples/hello_nv12_input_classification/main.c b/inference-engine/ie_bridges/c/samples/hello_nv12_input_classification/main.c index 529c8ac92de..01eb5ad0c57 100644 --- a/inference-engine/ie_bridges/c/samples/hello_nv12_input_classification/main.c +++ b/inference-engine/ie_bridges/c/samples/hello_nv12_input_classification/main.c @@ -38,6 +38,9 @@ struct classify_res *output_blob_to_classify_res(ie_blob_t *blob, size_t *n) { *n = output_dim.dims[1]; struct classify_res *cls = (struct classify_res *)malloc(sizeof(struct classify_res) * (*n)); + if (!cls) { + return NULL; + } ie_blob_buffer_t blob_cbuffer; status = ie_blob_get_cbuffer(blob, &blob_cbuffer); @@ -76,8 +79,8 @@ size_t read_image_from_file(const char *img_path, unsigned char *img_data, size_ fseek(fp, 0, SEEK_SET); read_size = fread(img_data, 1, size, fp); } + fclose(fp); } - fclose(fp); return read_size; } diff --git a/inference-engine/ie_bridges/c/samples/object_detection_sample_ssd/main.c b/inference-engine/ie_bridges/c/samples/object_detection_sample_ssd/main.c index 2cb5ce23d3f..9d249d5d0d5 100644 --- a/inference-engine/ie_bridges/c/samples/object_detection_sample_ssd/main.c +++ b/inference-engine/ie_bridges/c/samples/object_detection_sample_ssd/main.c @@ -122,6 +122,7 @@ void readInputFilesArgument(const char *arg) { for (i = 0; i < file_num; ++i) { free(file_paths[i]); } + free(file_path); free(file_paths); file_num = 0; } @@ -279,6 +280,10 @@ int main(int argc, char **argv) { ie_version_free(&version); char **argv_temp =(char **)calloc(argc, sizeof(char *)); + if (!argv_temp) { + return EXIT_FAILURE; + } + int i, j; for (i = 0; i < argc; ++i) { argv_temp[i] = argv[i]; @@ -419,6 +424,10 @@ int main(int argc, char **argv) { /** Collect images data **/ c_mat_t *originalImages = (c_mat_t *)calloc(file_num, sizeof(c_mat_t)); c_mat_t *images = (c_mat_t *)calloc(file_num, sizeof(c_mat_t)); + + if (!originalImages || !images) + goto err; + int image_num = 0; for (i = 0; i < file_num; ++i) { c_mat_t img = {NULL, 0, 0, 0, 0, 0}; @@ -435,20 +444,27 @@ int main(int argc, char **argv) { resized_img.mat_height = img.mat_height; resized_img.mat_type = img.mat_type; resized_img.mat_data = calloc(1, resized_img.mat_data_size); + if (resized_img.mat_data == NULL) { + image_free(&img); + continue; + } + for (j = 0; j < resized_img.mat_data_size; ++j) resized_img.mat_data[j] = img.mat_data[j]; } else { printf("%sImage is resized from (%d, %d) to (%zu, %zu)\n", \ - warn, img.mat_width, img.mat_height, input_width, input_height); + warn, img.mat_width, img.mat_height, input_width, input_height); - image_resize(&img, &resized_img, (int)input_width, (int)input_height); + if (image_resize(&img, &resized_img, (int)input_width, (int)input_height) == -1) { + printf("%sImage %s cannot be resized!\n", warn, file_paths[i]); + image_free(&img); + continue; + } } - if (resized_img.mat_data) { - originalImages[image_num] = img; - images[image_num] = resized_img; - ++image_num; - } + originalImages[image_num] = img; + images[image_num] = resized_img; + ++image_num; } if (!image_num) { @@ -523,8 +539,8 @@ int main(int argc, char **argv) { if (config_msg) { ie_config_t * config = parseConfig(config_msg, '#'); status = ie_core_load_network(core, network, device_name, config, &exe_network); + config_free(config); if (status != OK) { - config_free(config); goto err; } } else { diff --git a/inference-engine/ie_bridges/c/tests/ie_c_api_test.cpp b/inference-engine/ie_bridges/c/tests/ie_c_api_test.cpp index b8c1f61adfc..9cb3af24eb1 100644 --- a/inference-engine/ie_bridges/c/tests/ie_c_api_test.cpp +++ b/inference-engine/ie_bridges/c/tests/ie_c_api_test.cpp @@ -53,8 +53,8 @@ size_t read_image_from_file(const char* img_path, unsigned char *img_data, size_ fseek(fp, 0, SEEK_SET); read_size = fread(img_data, 1, size, fp); } + fclose(fp); } - fclose(fp); return read_size; }