diff mbox series

[FFmpeg-devel] lavfi/dnn: Fix OpenVINO missing model file corrupt issue.

Message ID 20220804093101.30059-1-ting.fu@intel.com
State New
Headers show
Series [FFmpeg-devel] lavfi/dnn: Fix OpenVINO missing model file corrupt issue. | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Ting Fu Aug. 4, 2022, 9:31 a.m. UTC
DNN OpenVINO backend would not report missing model file if it does not
exist. It would corrupt directly with out any error infomation. This commit
would check both .xml and .bin file existance before loading model.

Signed-off-by: Ting Fu <ting.fu@intel.com>
---
 libavfilter/dnn/dnn_backend_openvino.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

Comments

Anton Khirnov Aug. 4, 2022, 10:39 a.m. UTC | #1
Quoting Ting Fu (2022-08-04 11:31:01)
> DNN OpenVINO backend would not report missing model file if it does not
> exist. It would corrupt directly with out any error infomation. This commit

"corrupt"?

The patch looks completely wrong. Testing for file existence explicitly
is known to be a bad pattern that leads to all kinds of races, security
issues, and other bugs. Just trying to open the file and returning an
error if that fails is the right thing to do.
diff mbox series

Patch

diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c
index b494f26f55..47e3fc8280 100644
--- a/libavfilter/dnn/dnn_backend_openvino.c
+++ b/libavfilter/dnn/dnn_backend_openvino.c
@@ -34,6 +34,7 @@ 
 #include "../internal.h"
 #include "safe_queue.h"
 #include <c_api/ie_c_api.h>
+#include <unistd.h>
 #include "dnn_backend_common.h"
 
 typedef struct OVOptions{
@@ -728,7 +729,7 @@  DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_
     OVContext *ctx = NULL;
     IEStatusCode status;
     size_t node_count = 0;
-    char *node_name = NULL;
+    char *node_name, *model_binary_file, *extension_chr = NULL;
 
     model = av_mallocz(sizeof(DNNModel));
     if (!model){
@@ -758,6 +759,22 @@  DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_
     if (status != OK)
         goto err;
 
+    //file existance check
+    model_binary_file = (char *)malloc(sizeof(model_filename));
+    strcpy(model_binary_file, model_filename);
+    extension_chr = strstr(model_binary_file, ".xml");
+    if (!extension_chr) {
+        av_log(ctx, AV_LOG_ERROR, "Model file name \"%s\" with incorrect extension.",
+               model_filename);
+        goto err;
+    }
+    strcpy(extension_chr, ".bin");
+    if (access(model_filename, F_OK) || access(model_binary_file, F_OK)) {
+        av_log(ctx, AV_LOG_ERROR, "Model file \"%s\" or \"%s\" does not exist.",
+               model_filename, model_binary_file);
+        goto err;
+    }
+
     status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
     if (status != OK) {
         ie_version_t ver;
@@ -806,6 +823,7 @@  DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_
     return model;
 
 err:
+    free(model_binary_file);
     ff_dnn_free_model_ov(&model);
     return NULL;
 }