From f1dd31e657175077b26fc20873acc67a5f29ff7c Mon Sep 17 00:00:00 2001 From: jatinwadhwa921 Date: Mon, 12 May 2025 11:10:57 +0530 Subject: [PATCH] [OVEP] Fixed coverity issues --- .../openvino/openvino_execution_provider.cc | 2 +- .../openvino/openvino_provider_factory.cc | 14 +++++++------- .../core/providers/openvino/ov_allocator.cc | 12 ++++++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/onnxruntime/core/providers/openvino/openvino_execution_provider.cc b/onnxruntime/core/providers/openvino/openvino_execution_provider.cc index f9d4ab13cf2ce..70bc64c0f65bc 100644 --- a/onnxruntime/core/providers/openvino/openvino_execution_provider.cc +++ b/onnxruntime/core/providers/openvino/openvino_execution_provider.cc @@ -55,7 +55,7 @@ static std::vector parseDevices(const std::string& device_string, OpenVINOExecutionProvider::OpenVINOExecutionProvider(const ProviderInfo& info, std::shared_ptr shared_context) : IExecutionProvider{onnxruntime::kOpenVINOExecutionProvider}, session_context_(info), - shared_context_{shared_context}, + shared_context_{std::move(shared_context)}, ep_ctx_handle_{session_context_.openvino_sdk_version, *GetLogger()} { InitProviderOrtApi(); } diff --git a/onnxruntime/core/providers/openvino/openvino_provider_factory.cc b/onnxruntime/core/providers/openvino/openvino_provider_factory.cc index f7f15dc62fd11..e5526ecd52bb9 100644 --- a/onnxruntime/core/providers/openvino/openvino_provider_factory.cc +++ b/onnxruntime/core/providers/openvino/openvino_provider_factory.cc @@ -138,7 +138,7 @@ std::string ParseDeviceType(std::shared_ptr ov_core, const ProviderOptio if (std::find(std::begin(available_devices), std::end(available_devices), device) != std::end(available_devices)) device_found = true; if (device_prefix != "CPU" && luid_list.size() > 0) { - for (auto dev : available_devices) { + for (const auto& dev : available_devices) { ov::device::LUID ov_luid = OVCore::Get()->core.get_property(dev, ov::device::luid); std::stringstream ov_luid_str; ov_luid_str << ov_luid; @@ -153,7 +153,7 @@ std::string ParseDeviceType(std::shared_ptr ov_core, const ProviderOptio } if (luid_list.size() > 0) { std::string ov_luid_devices; - for (auto luid_str : luid_list) { + for (const auto& luid_str : luid_list) { if (ov_luid_map.contains(luid_str)) { std::string ov_dev = ov_luid_map.at(luid_str); std::string ov_dev_strip = split(ov_dev, '.')[0]; @@ -170,14 +170,14 @@ std::string ParseDeviceType(std::shared_ptr ov_core, const ProviderOptio } if (!device_mode.empty()) { selected_device = device_mode + ":" + ov_luid_devices; - for (auto dev_str : devices_to_check) { - auto default_dev = split(dev_str, '.')[0]; + for (const auto& dev_str : devices_to_check) { + const auto default_dev = split(dev_str, '.')[0]; if (ov_luid_devices.find(default_dev) == std::string::npos) selected_device = selected_device + "," + dev_str; } } else { - selected_device = ov_luid_devices; + selected_device = std::move(ov_luid_devices); } } // If invalid device is chosen error is thrown @@ -215,7 +215,7 @@ static void ParseProviderInfo(const ProviderOptions& provider_options, // Minor optimization: we'll hold an OVCore reference to ensure we don't create a new core between ParseDeviceType and // (potential) SharedContext creation. auto ov_core = OVCore::Get(); - pi.device_type = ParseDeviceType(ov_core, provider_options); + pi.device_type = ParseDeviceType(std::move(ov_core), provider_options); if (provider_options.contains("device_id")) { std::string dev_id = provider_options.at("device_id").data(); @@ -355,7 +355,7 @@ static void ParseProviderInfo(const ProviderOptions& provider_options, struct OpenVINOProviderFactory : IExecutionProviderFactory { OpenVINOProviderFactory(ProviderInfo provider_info, std::shared_ptr shared_context) - : provider_info_(std::move(provider_info)), shared_context_(shared_context) {} + : provider_info_(std::move(provider_info)), shared_context_(std::move(shared_context)) {} ~OpenVINOProviderFactory() override {} diff --git a/onnxruntime/core/providers/openvino/ov_allocator.cc b/onnxruntime/core/providers/openvino/ov_allocator.cc index 431f5730c0342..1bbe71441cbff 100644 --- a/onnxruntime/core/providers/openvino/ov_allocator.cc +++ b/onnxruntime/core/providers/openvino/ov_allocator.cc @@ -22,7 +22,7 @@ void* OVRTAllocator::Alloc(size_t size) { try { ov::Tensor* tensor = new ov::Tensor(remote_ctx_.create_host_tensor(ov::element::Type_t::u8, {size})); - std::unique_lock lock(mutex_); + std::lock_guard lock(mutex_); allocated_.insert({tensor->data(), tensor}); return reinterpret_cast(tensor->data()); } catch (const ov::Exception& e) { @@ -32,12 +32,16 @@ void* OVRTAllocator::Alloc(size_t size) { void OVRTAllocator::Free(void* p) { try { - std::unique_lock lock(mutex_); + ov::Tensor* tensor = nullptr; + { + std::lock_guard lock(mutex_); auto it = allocated_.find(p); if (it != allocated_.end()) { - ov::Tensor* tensor = it->second; + tensor = it->second; allocated_.erase(it); - lock.unlock(); + } + } + if (tensor) { delete tensor; } } catch (const ov::Exception& e) {