[C API][COVERITY SCAN]Fix the TAINTED_SCALAR and DEADCODE in Coverity Scan (#12087)

* Fix the Coverity scan issues

* Fix the insecure data handling (TAINTED_SCALAR) issue found in coverity scan
This commit is contained in:
RICKIE777 2022-07-10 17:21:03 +08:00 committed by GitHub
parent 0c7282a1f5
commit bbf54e01e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -104,10 +104,29 @@ void print_model_input_output_info(ov_model_t* model) {
*/
bool is_supported_image_size(const char* size_str, size_t* width, size_t* height) {
char* p_end = NULL;
const char* _size = size_str;
size_t _width = 0, _height = 0;
_width = strtoul(size_str, &p_end, 10);
_height = strtoul(p_end + 1, NULL, 10);
while (_size && *_size != 'x' && *_size != '\0') {
if ((*_size <= '9') && (*_size >= '0')) {
_width = (_width * 10) + (*_size - '0');
_size++;
} else {
goto err;
}
}
if (_size)
_size++;
while (_size && *_size != '\0') {
if ((*_size <= '9') && (*_size >= '0')) {
_height = (_height * 10) + (*_size - '0');
_size++;
} else {
goto err;
}
}
if (_width > 0 && _height > 0) {
if (_width % 2 == 0 && _height % 2 == 0) {
*width = _width;
@ -118,11 +137,13 @@ bool is_supported_image_size(const char* size_str, size_t* width, size_t* height
return false;
}
} else {
printf("Incorrect format of image size parameter, expected WIDTHxHEIGHT, "
"actual: %s\n",
size_str);
return false;
goto err;
}
err:
printf("Incorrect format of image size parameter, expected WIDTHxHEIGHT, "
"actual: %s\n",
size_str);
return false;
}
size_t read_image_from_file(const char* img_path, unsigned char* img_data, size_t size) {
@ -170,8 +191,6 @@ int main(int argc, char** argv) {
ov_preprocess_input_tensor_info_t* input_tensor_info = NULL;
ov_preprocess_input_process_steps_t* input_process = NULL;
ov_preprocess_input_model_info_t* p_input_model = NULL;
ov_preprocess_output_info_t* output_info = NULL;
ov_preprocess_output_tensor_info_t* output_tensor_info = NULL;
ov_compiled_model_t* compiled_model = NULL;
ov_infer_request_t* infer_request = NULL;
ov_tensor_t* output_tensor = NULL;
@ -259,6 +278,10 @@ int main(int argc, char** argv) {
// -------- Step 6. Prepare input data --------
img_size = input_width * (input_height * 3 / 2);
if (!img_size) {
fprintf(stderr, "[ERROR] Invalid Image size, line %d\n", __LINE__);
goto err;
}
img_data = (unsigned char*)calloc(img_size, sizeof(unsigned char));
if (NULL == img_data) {
fprintf(stderr, "[ERROR] calloc returned NULL, line %d\n", __LINE__);
@ -309,10 +332,6 @@ err:
ov_infer_request_free(infer_request);
if (compiled_model)
ov_compiled_model_free(compiled_model);
if (output_tensor_info)
ov_preprocess_output_tensor_info_free(output_tensor_info);
if (output_info)
ov_preprocess_output_info_free(output_info);
if (p_input_model)
ov_preprocess_input_model_info_free(p_input_model);
if (input_process)