[FFmpeg-devel] [PATCH 3/3] dnn/queue: add error check and cleanup

Guo, Yejun yejun.guo at intel.com
Wed Dec 30 08:26:50 EET 2020


Signed-off-by: Guo, Yejun <yejun.guo at intel.com>
---
 libavfilter/dnn/dnn_backend_openvino.c | 16 ++++++++++++---
 libavfilter/dnn/queue.c                | 28 ++++++++++++++++++++------
 libavfilter/dnn/queue.h                |  4 ++--
 libavfilter/dnn/safe_queue.c           | 16 ++++++++++-----
 libavfilter/dnn/safe_queue.h           |  4 ++--
 5 files changed, 50 insertions(+), 18 deletions(-)

diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c
index a35d72a38c..5dc855d0d9 100644
--- a/libavfilter/dnn/dnn_backend_openvino.c
+++ b/libavfilter/dnn/dnn_backend_openvino.c
@@ -208,7 +208,10 @@ static void infer_completion_callback(void *args)
 
     if (task->async) {
         request->task = NULL;
-        safe_queue_push_back(task->ov_model->request_queue, request);
+        if (safe_queue_push_back(task->ov_model->request_queue, request) < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
+            return;
+        }
     }
 
     task->done = 1;
@@ -436,7 +439,10 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options,
         item->infer_request = request;
         item->callback.completeCallBackFunc = infer_completion_callback;
         item->callback.args = item;
-        safe_queue_push_back(ov_model->request_queue, item);
+        if (safe_queue_push_back(ov_model->request_queue, item) < 0) {
+            av_freep(&item);
+            goto err;
+        }
     }
 
     ov_model->task_queue = queue_create();
@@ -527,7 +533,11 @@ DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i
     task->output_name = output_names[0];
     task->out_frame = out_frame;
     task->ov_model = ov_model;
-    queue_push_back(ov_model->task_queue, task);
+    if (queue_push_back(ov_model->task_queue, task) < 0) {
+        av_freep(&task);
+        av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
+        return DNN_ERROR;
+    }
 
     request = safe_queue_pop_front(ov_model->request_queue);
     if (!request) {
diff --git a/libavfilter/dnn/queue.c b/libavfilter/dnn/queue.c
index da0517968d..51e24b76d3 100644
--- a/libavfilter/dnn/queue.c
+++ b/libavfilter/dnn/queue.c
@@ -40,8 +40,8 @@ struct _queue_entry {
 static inline queue_entry *create_entry(void *val)
 {
     queue_entry *entry = av_malloc(sizeof(*entry));
-    av_assert0(entry != NULL);
-    entry->value = val;
+    if (entry)
+        entry->value = val;
     return entry;
 }
 
@@ -53,6 +53,14 @@ queue* queue_create(void)
 
     q->head = create_entry(q);
     q->tail = create_entry(q);
+
+    if (!q->head || !q->tail) {
+        av_freep(&q->head);
+        av_freep(&q->tail);
+        av_freep(&q);
+        return NULL;
+    }
+
     q->head->next = q->tail;
     q->tail->prev = q->head;
     q->head->prev = NULL;
@@ -99,14 +107,16 @@ void *queue_peek_back(queue *q)
     return q->tail->prev->value;
 }
 
-void queue_push_front(queue *q, void *v)
+int queue_push_front(queue *q, void *v)
 {
     queue_entry *new_entry;
     queue_entry *original_next;
     if (!q)
-        return;
+        return 0;
 
     new_entry = create_entry(v);
+    if (!new_entry)
+        return -1;
     original_next = q->head->next;
 
     q->head->next = new_entry;
@@ -114,16 +124,20 @@ void queue_push_front(queue *q, void *v)
     new_entry->prev = q->head;
     new_entry->next = original_next;
     q->length++;
+
+    return q->length;
 }
 
-void queue_push_back(queue *q, void *v)
+int queue_push_back(queue *q, void *v)
 {
     queue_entry *new_entry;
     queue_entry *original_prev;
     if (!q)
-        return;
+        return 0;
 
     new_entry = create_entry(v);
+    if (!new_entry)
+        return -1;
     original_prev = q->tail->prev;
 
     q->tail->prev = new_entry;
@@ -131,6 +145,8 @@ void queue_push_back(queue *q, void *v)
     new_entry->next = q->tail;
     new_entry->prev = original_prev;
     q->length++;
+
+    return q->length;
 }
 
 void *queue_pop_front(queue *q)
diff --git a/libavfilter/dnn/queue.h b/libavfilter/dnn/queue.h
index 75f42f403b..58fb416001 100644
--- a/libavfilter/dnn/queue.h
+++ b/libavfilter/dnn/queue.h
@@ -32,8 +32,8 @@ size_t queue_size(queue *q);
 void *queue_peek_front(queue *q);
 void *queue_peek_back(queue *q);
 
-void queue_push_front(queue *q, void *v);
-void queue_push_back(queue *q, void *v);
+int queue_push_front(queue *q, void *v);
+int queue_push_back(queue *q, void *v);
 
 void *queue_pop_front(queue *q);
 void *queue_pop_back(queue *q);
diff --git a/libavfilter/dnn/safe_queue.c b/libavfilter/dnn/safe_queue.c
index 4f47e68fb3..b2a2ca5a46 100644
--- a/libavfilter/dnn/safe_queue.c
+++ b/libavfilter/dnn/safe_queue.c
@@ -56,8 +56,10 @@ safe_queue *safe_queue_create(void)
         return NULL;
 
     sq->q = queue_create();
-    if (!sq->q)
+    if (!sq->q) {
+        av_freep(&sq);
         return NULL;
+    }
 
     ff_mutex_init(&sq->mutex, NULL);
     dnn_cond_init(&sq->cond, NULL);
@@ -80,20 +82,24 @@ size_t safe_queue_size(safe_queue *sq)
     return sq ? queue_size(sq->q) : 0;
 }
 
-void safe_queue_push_front(safe_queue *sq, void *v)
+int safe_queue_push_front(safe_queue *sq, void *v)
 {
+    int ret;
     ff_mutex_lock(&sq->mutex);
-    queue_push_front(sq->q, v);
+    ret = queue_push_front(sq->q, v);
     dnn_cond_signal(&sq->cond);
     ff_mutex_unlock(&sq->mutex);
+    return ret;
 }
 
-void safe_queue_push_back(safe_queue *sq, void *v)
+int safe_queue_push_back(safe_queue *sq, void *v)
 {
+    int ret;
     ff_mutex_lock(&sq->mutex);
-    queue_push_back(sq->q, v);
+    ret = queue_push_back(sq->q, v);
     dnn_cond_signal(&sq->cond);
     ff_mutex_unlock(&sq->mutex);
+    return ret;
 }
 
 void *safe_queue_pop_front(safe_queue *sq)
diff --git a/libavfilter/dnn/safe_queue.h b/libavfilter/dnn/safe_queue.h
index aaa15fadf2..64e90880e8 100644
--- a/libavfilter/dnn/safe_queue.h
+++ b/libavfilter/dnn/safe_queue.h
@@ -28,8 +28,8 @@ void safe_queue_destroy(safe_queue *sq);
 
 size_t safe_queue_size(safe_queue *sq);
 
-void safe_queue_push_front(safe_queue *sq, void *v);
-void safe_queue_push_back(safe_queue *sq, void *v);
+int safe_queue_push_front(safe_queue *sq, void *v);
+int safe_queue_push_back(safe_queue *sq, void *v);
 
 void *safe_queue_pop_front(safe_queue *sq);
 
-- 
2.17.1



More information about the ffmpeg-devel mailing list