From 149013ae5372fb63528e0d14202d9faf85b62c32 Mon Sep 17 00:00:00 2001 From: edgchen1 <18449977+edgchen1@users.noreply.github.com> Date: Tue, 28 Nov 2023 14:07:57 -0800 Subject: [PATCH] fix static analysis warnings --- include/onnxruntime/core/graph/graph.h | 2 +- .../core/providers/coreml/model/model.mm | 45 ++++++++++++------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/include/onnxruntime/core/graph/graph.h b/include/onnxruntime/core/graph/graph.h index fe0734c51f807..22827d43b200f 100644 --- a/include/onnxruntime/core/graph/graph.h +++ b/include/onnxruntime/core/graph/graph.h @@ -668,7 +668,7 @@ class Node { The Graph representation containing the graph inputs and outputs, the Node instances, and the edges connecting the nodes. */ -class Graph { +class Graph { // NOLINT(clang-analyzer-optin.performance.Padding): preserve existing data member order for readability public: /** Gets the Graph name. */ const std::string& Name() const noexcept; diff --git a/onnxruntime/core/providers/coreml/model/model.mm b/onnxruntime/core/providers/coreml/model/model.mm index 4a6743e9e5c52..32821fd02647a 100644 --- a/onnxruntime/core/providers/coreml/model/model.mm +++ b/onnxruntime/core/providers/coreml/model/model.mm @@ -32,6 +32,13 @@ using namespace onnxruntime::coreml; namespace { +// Converts a UTF8 const char* to an NSString. Throws on failure. +NSString* _Nonnull Utf8StringToNSString(const char* utf8_str) { + NSString* result = [NSString stringWithUTF8String:utf8_str]; + ORT_ENFORCE(result != nil, "NSString conversion failed."); + return result; +} + /** * Computes the static output shape used to allocate the output tensor. * `inferred_shape` is the inferred shape known at model compile time. It may contain dynamic dimensions (-1). @@ -152,19 +159,20 @@ Status CreateInputFeatureProvider(const std::unordered_map&)inputs get_output_tensor_mutable_raw_data_fn API_AVAILABLE_OS_VERSIONS; -@property MLModel* model API_AVAILABLE_OS_VERSIONS; +@property(nullable) MLModel* model API_AVAILABLE_OS_VERSIONS; @end @@ -297,12 +305,15 @@ - (void)dealloc { - (Status)loadModel { NSError* error = nil; NSURL* modelUrl = [NSURL URLWithString:coreml_model_path_]; - NSAssert(modelUrl != nil, @"modelUrl must not be nil"); + if (modelUrl == nil) { + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to create model URL from path"); + } + NSURL* compileUrl = [MLModel compileModelAtURL:modelUrl error:&error]; if (error != nil) { - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Error compiling model ", - [[error localizedDescription] cStringUsingEncoding:NSUTF8StringEncoding]); + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Error compiling model: ", + [[error localizedDescription] UTF8String]); } compiled_model_path_ = [compileUrl path]; @@ -313,9 +324,9 @@ - (Status)loadModel { : MLComputeUnitsAll; _model = [MLModel modelWithContentsOfURL:compileUrl configuration:config error:&error]; - if (error != NULL) { - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Error Creating MLModel ", - [[error localizedDescription] cStringUsingEncoding:NSUTF8StringEncoding]); + if (_model == nil) { + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to create MLModel", + (error != nil) ? MakeString(", error: ", [[error localizedDescription] UTF8String]) : ""); } return Status::OK(); @@ -327,7 +338,7 @@ - (Status)predict:(const std::unordered_map&)inputs Status status = Status::OK(); ORT_TRY { if (_model == nil) { - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "model is not loaded"); + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Model is not loaded"); } id input_features; @@ -342,12 +353,12 @@ - (Status)predict:(const std::unordered_map&)inputs if (error != nil) { return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Error executing model: ", - [[error localizedDescription] cStringUsingEncoding:NSUTF8StringEncoding]); + [[error localizedDescription] UTF8String]); } for (const auto& [output_name, output_tensor_info] : outputs) { MLFeatureValue* output_value = - [output_features featureValueForName:[NSString stringWithUTF8String:output_name.c_str()]]; + [output_features featureValueForName:Utf8StringToNSString(output_name.c_str())]; if (output_value == nil) { return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "output_features has no value for ", output_name); @@ -452,7 +463,7 @@ Status Predict(const std::unordered_map& inputs, return status; } - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Execution::LoadModel requires macos 10.15+ or ios 13+ "); + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Execution::LoadModel requires macos 10.15+ or ios 13+"); } Status Execution::Predict(const std::unordered_map& inputs, @@ -468,7 +479,7 @@ Status Predict(const std::unordered_map& inputs, } } - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Execution::LoadModel requires macos 10.15+ or ios 13+ "); + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Execution::Predict requires macos 10.15+ or ios 13+"); } Model::Model(const std::string& path, const logging::Logger& logger, uint32_t coreml_flags)