From fff02dab0db95695f480a3771e700a1a266d52ca Mon Sep 17 00:00:00 2001 From: Jaeyun Jung Date: Fri, 17 May 2024 15:54:52 +0900 Subject: [PATCH] [CodeClean] fix svace issue Code clean, fix svace issues. - check max number of tensors and prevent underflow. Signed-off-by: Jaeyun Jung --- ext/nnstreamer/extra/nnstreamer_python3_helper.cc | 25 ++++++++++++---------- .../tensor_converter/tensor_converter_python3.cc | 9 ++++++-- .../tensor_filter/tensor_filter_python3.cc | 4 ++-- gst/nnstreamer/tensor_query/tensor_query_client.c | 3 ++- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/ext/nnstreamer/extra/nnstreamer_python3_helper.cc b/ext/nnstreamer/extra/nnstreamer_python3_helper.cc index f346500..7d26480 100644 --- a/ext/nnstreamer/extra/nnstreamer_python3_helper.cc +++ b/ext/nnstreamer/extra/nnstreamer_python3_helper.cc @@ -46,13 +46,16 @@ TensorShape_setDims (TensorShapeObject *self, PyObject *args) { PyObject *dims; PyObject *new_dims; - unsigned int i, len; + Py_ssize_t i, len; /** PyArg_ParseTuple() returns borrowed references */ if (!PyArg_ParseTuple (args, "O", &dims)) Py_RETURN_NONE; len = PyList_Size (dims); + if (len < 0 || len > NNS_TENSOR_RANK_LIMIT) + Py_RETURN_NONE; + if (len < NNS_TENSOR_RANK_LIMIT) { for (i = 0; i < NNS_TENSOR_RANK_LIMIT - len; i++) PyList_Append (dims, PyLong_FromLong (0)); @@ -394,14 +397,14 @@ addToSysPath (const gchar *path) int parseTensorsInfo (PyObject *result, GstTensorsInfo *info) { - guint i, j, rank; + Py_ssize_t i, j, num = PyList_Size (result); - if (PyList_Size (result) < 0) + if (num < 0 || num > NNS_TENSOR_SIZE_LIMIT) return -1; gst_tensors_info_init (info); - info->num_tensors = PyList_Size (result); - for (i = 0; i < info->num_tensors; i++) { + info->num_tensors = (unsigned int) num; + for (i = 0; i < num; i++) { /** don't own the reference */ PyObject *tensor_shape = PyList_GetItem (result, (Py_ssize_t) i); if (nullptr == tensor_shape) { @@ -432,8 +435,8 @@ parseTensorsInfo (PyObject *result, GstTensorsInfo *info) return -EINVAL; } - rank = (guint) PyList_Size (shape_dims); - if (rank > NNS_TENSOR_RANK_LIMIT) { + Py_ssize_t rank = PyList_Size (shape_dims); + if (rank < 0 || rank > NNS_TENSOR_RANK_LIMIT) { Py_ERRMSG ("The function %s has failed, max rank of tensor dimension is %d.", __FUNCTION__, NNS_TENSOR_RANK_LIMIT); Py_SAFEDECREF (shape_dims); @@ -449,7 +452,7 @@ parseTensorsInfo (PyObject *result, GstTensorsInfo *info) PyErr_Print (); PyErr_Clear (); info->info[i].dimension[j] = 0; - Py_ERRMSG ("Python nnstreamer plugin has returned dimensions of the %u'th tensor not in an array. Python code should return int-type array for dimensions. Indexes are counted from 0.\n", + Py_ERRMSG ("Python nnstreamer plugin has returned dimensions of the %zd'th tensor not in an array. Python code should return int-type array for dimensions. Indexes are counted from 0.\n", i + 1); Py_SAFEDECREF (shape_dims); return -EINVAL; @@ -460,18 +463,18 @@ parseTensorsInfo (PyObject *result, GstTensorsInfo *info) } else if (PyFloat_Check (item)) { /** Regard this as a warning. Don't return -EINVAL with this */ val = (int) PyFloat_AsDouble (item); - Py_ERRMSG ("Python nnstreamer plugin has returned the %u'th dimension value of the %u'th tensor in floating-point type (%f), which is casted as unsigned-int. Python code should return int-type for dimension values. Indexes are counted from 0.\n", + Py_ERRMSG ("Python nnstreamer plugin has returned the %zd'th dimension value of the %zd'th tensor in floating-point type (%f), which is casted as unsigned-int. Python code should return int-type for dimension values. Indexes are counted from 0.\n", j + 1, i + 1, PyFloat_AsDouble (item)); } else { info->info[i].dimension[j] = 0; - Py_ERRMSG ("Python nnstreamer plugin has returned the %u'th dimension value of the %u'th tensor neither in integer or floating-pointer. Python code should return int-type for dimension values. Indexes are counted from 0.\n", + Py_ERRMSG ("Python nnstreamer plugin has returned the %zd'th dimension value of the %zd'th tensor neither in integer or floating-pointer. Python code should return int-type for dimension values. Indexes are counted from 0.\n", j + 1, i + 1); Py_SAFEDECREF (shape_dims); return -EINVAL; } if (val < 0) { - Py_ERRMSG ("The %u'th dimension value of the %u'th tensor is invalid (%d).", + Py_ERRMSG ("The %zd'th dimension value of the %zd'th tensor is invalid (%d).", j + 1, i + 1, val); Py_SAFEDECREF (shape_dims); return -EINVAL; diff --git a/ext/nnstreamer/tensor_converter/tensor_converter_python3.cc b/ext/nnstreamer/tensor_converter/tensor_converter_python3.cc index 599c81c..da79d33 100644 --- a/ext/nnstreamer/tensor_converter/tensor_converter_python3.cc +++ b/ext/nnstreamer/tensor_converter/tensor_converter_python3.cc @@ -173,10 +173,15 @@ PYConverterCore::convert (GstBuffer *in_buf, GstTensorsConfig *config) if (output) { GstTensorInfo *_info; - unsigned int num_tensors = PyList_Size (output); + Py_ssize_t num_tensors = PyList_Size (output); + + if (num_tensors < 0 || num_tensors > NNS_TENSOR_SIZE_LIMIT) { + Py_ERRMSG ("Fail to get output from 'convert', invalid output size."); + goto done; + } out_buf = gst_buffer_new (); - for (unsigned int i = 0; i < num_tensors; i++) { + for (i = 0; i < (guint) num_tensors; i++) { PyArrayObject *output_array = (PyArrayObject *) PyList_GetItem (output, (Py_ssize_t) i); diff --git a/ext/nnstreamer/tensor_filter/tensor_filter_python3.cc b/ext/nnstreamer/tensor_filter/tensor_filter_python3.cc index 4f40ab3..e8db6a0 100644 --- a/ext/nnstreamer/tensor_filter/tensor_filter_python3.cc +++ b/ext/nnstreamer/tensor_filter/tensor_filter_python3.cc @@ -834,8 +834,8 @@ TensorFilterPython::fini_filter_py () */ #if 0 /** Python should be initialized and finalized only once */ - if (Py_IsInitialized ()) - Py_Finalize (); + if (Py_IsInitialized ()) + Py_Finalize (); #endif } diff --git a/gst/nnstreamer/tensor_query/tensor_query_client.c b/gst/nnstreamer/tensor_query/tensor_query_client.c index 5755f95..4123b0f 100644 --- a/gst/nnstreamer/tensor_query/tensor_query_client.c +++ b/gst/nnstreamer/tensor_query/tensor_query_client.c @@ -727,7 +727,8 @@ try_pop: data_h = g_async_queue_timeout_pop (self->msg_queue, self->timeout * G_TIME_SPAN_MILLISECOND); if (data_h) { - self->requested_num--; + if (self->requested_num > 0) + self->requested_num--; ret = nns_edge_data_get_count (data_h, &num_data); if (ret == NNS_EDGE_ERROR_NONE && num_data > 0) { -- 2.7.4