[CodeClean] fix svace issue
authorJaeyun Jung <jy1210.jung@samsung.com>
Fri, 17 May 2024 06:54:52 +0000 (15:54 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Thu, 23 May 2024 08:17:00 +0000 (17:17 +0900)
Code clean, fix svace issues.
- check max number of tensors and prevent underflow.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
ext/nnstreamer/extra/nnstreamer_python3_helper.cc
ext/nnstreamer/tensor_converter/tensor_converter_python3.cc
ext/nnstreamer/tensor_filter/tensor_filter_python3.cc
gst/nnstreamer/tensor_query/tensor_query_client.c

index f346500..7d26480 100644 (file)
@@ -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;
index 599c81c..da79d33 100644 (file)
@@ -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);
 
index 4f40ab3..e8db6a0 100644 (file)
@@ -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
 }
 
index 5755f95..4123b0f 100644 (file)
@@ -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) {