[Filter/Prop] parse combination option
authorJaeyun <jy1210.jung@samsung.com>
Fri, 30 Apr 2021 03:06:29 +0000 (12:06 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Mon, 3 May 2021 11:59:54 +0000 (20:59 +0900)
1. parse combi option as unsigned int and check the index (less than 16).
2. fix possible mem leak (release combi option list).

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
gst/nnstreamer/tensor_filter/tensor_filter.c
gst/nnstreamer/tensor_filter/tensor_filter_common.c

index 9425871..8cb3537 100644 (file)
@@ -572,10 +572,10 @@ gst_tensor_filter_transform (GstBaseTransform * trans,
 
   /* 1.1 Prepare tensors to invoke. */
   if (priv->combi.in_combi_defined) {
-    gint info_idx = 0;
+    guint info_idx = 0;
 
     for (list = priv->combi.in_combi; list != NULL; list = list->next) {
-      i = GPOINTER_TO_INT (list->data);
+      i = GPOINTER_TO_UINT (list->data);
       invoke_tensors[info_idx++] = in_tensors[i];
     }
   } else {
@@ -637,7 +637,7 @@ gst_tensor_filter_transform (GstBaseTransform * trans,
   /* If output combination is defined, append input tensors first */
   if (priv->combi.out_combi_i_defined) {
     for (list = priv->combi.out_combi_i; list != NULL; list = list->next) {
-      i = GPOINTER_TO_INT (list->data);
+      i = GPOINTER_TO_UINT (list->data);
       gst_memory_ref (in_mem[i]);
       gst_buffer_append_memory (outbuf, in_mem[i]);
     }
@@ -648,7 +648,7 @@ gst_tensor_filter_transform (GstBaseTransform * trans,
       gboolean out_combi = FALSE;
 
       for (list = priv->combi.out_combi_o; list != NULL; list = list->next) {
-        if (i == GPOINTER_TO_INT (list->data)) {
+        if (i == GPOINTER_TO_UINT (list->data)) {
           out_combi = TRUE;
           break;
         }
index 4086554..9f096c8 100644 (file)
@@ -1021,6 +1021,10 @@ gst_tensor_filter_common_free_property (GstTensorFilterPrivate * priv)
   gst_tensors_info_free (&priv->in_config.info);
   gst_tensors_info_free (&priv->out_config.info);
 
+  g_list_free (priv->combi.in_combi);
+  g_list_free (priv->combi.out_combi_i);
+  g_list_free (priv->combi.out_combi_o);
+
   if (priv->stat.recent_latencies != NULL) {
     GQueue *queue = priv->stat.recent_latencies;
     gint64 *latency;
@@ -1757,21 +1761,23 @@ static gint
 _gtfc_setprop_INPUTCOMBINATION (GstTensorFilterPrivate * priv,
     GList ** prop_list, const GValue * value)
 {
-  gint64 val;
+  guint64 val;
   const gchar *param = g_value_get_string (value);
   gchar **strv = g_strsplit_set (param, ",", -1);
   gint i, ret = 0, num = g_strv_length (strv);
+
+  /* release old list */
+  g_list_free (*prop_list);
   *prop_list = NULL;
 
   for (i = 0; i < num; i++) {
-    val = g_ascii_strtoll (strv[i], NULL, 10);
-    if (errno == ERANGE) {
-      ml_loge ("Overflow occured during converting %s to a gint64 value",
-          strv[i]);
+    val = g_ascii_strtoull (strv[i], NULL, 10);
+    if (errno == ERANGE || val >= NNS_TENSOR_SIZE_LIMIT) {
+      ml_loge ("Invalid value %s, cannot set combination option.", strv[i]);
       ret = ERANGE;
       break;
     }
-    *prop_list = g_list_append (*prop_list, GINT_TO_POINTER (val));
+    *prop_list = g_list_append (*prop_list, GUINT_TO_POINTER (val));
   }
   g_strfreev (strv);
 
@@ -1786,23 +1792,24 @@ static gint
 _gtfc_setprop_OUTPUTCOMBINATION (GstTensorFilterPrivate * priv,
     GList ** prop_list1, GList ** prop_list2, const GValue * value)
 {
-  gint64 val;
+  guint64 val;
   const gchar *param = g_value_get_string (value);
   gchar **strv = g_strsplit_set (param, ",", -1);
   gint i, ret = 0, num = g_strv_length (strv);
-  priv->combi.out_combi_i = NULL;
-  priv->combi.out_combi_o = NULL;
-  *prop_list1 = NULL;
-  *prop_list2 = NULL;
+
+  /* release old list */
+  g_list_free (*prop_list1);
+  g_list_free (*prop_list2);
+  *prop_list1 = *prop_list2 = NULL;
 
   for (i = 0; i < num; i++) {
     if (strv[i][0] == 'i') {
-      val = g_ascii_strtoll (&strv[i][1], NULL, 10);
-      *prop_list1 = g_list_append (*prop_list1, GINT_TO_POINTER (val));
+      val = g_ascii_strtoull (&strv[i][1], NULL, 10);
+      *prop_list1 = g_list_append (*prop_list1, GUINT_TO_POINTER (val));
       priv->combi.out_combi_i_defined = TRUE;
     } else if (strv[i][0] == 'o') {
-      val = g_ascii_strtoll (&strv[i][1], NULL, 10);
-      *prop_list2 = g_list_append (*prop_list2, GINT_TO_POINTER (val));
+      val = g_ascii_strtoull (&strv[i][1], NULL, 10);
+      *prop_list2 = g_list_append (*prop_list2, GUINT_TO_POINTER (val));
       priv->combi.out_combi_o_defined = TRUE;
     } else {
       ml_loge ("Wrong format for output combination properties. "
@@ -1812,9 +1819,8 @@ _gtfc_setprop_OUTPUTCOMBINATION (GstTensorFilterPrivate * priv,
       break;
     }
 
-    if (errno == ERANGE) {
-      ml_loge ("Overflow occured during converting %s to a gint64 value",
-          strv[i]);
+    if (errno == ERANGE || val >= NNS_TENSOR_SIZE_LIMIT) {
+      ml_loge ("Invalid value %s, cannot set combination option.", strv[i]);
       ret = ERANGE;
       break;
     }
@@ -1926,15 +1932,15 @@ gst_tensor_filter_property_to_string (GValue * value,
 
   if (prop_id == PROP_INPUTCOMBINATION) {
     for (list = priv->combi.in_combi; list != NULL; list = list->next)
-      g_ptr_array_add (arr, g_strdup_printf ("%i",
-              GPOINTER_TO_INT (list->data)));
+      g_ptr_array_add (arr, g_strdup_printf ("%u",
+              GPOINTER_TO_UINT (list->data)));
   } else if (prop_id == PROP_OUTPUTCOMBINATION) {
     for (list = priv->combi.out_combi_i; list != NULL; list = list->next)
-      g_ptr_array_add (arr, g_strdup_printf ("i%i",
-              GPOINTER_TO_INT (list->data)));
+      g_ptr_array_add (arr, g_strdup_printf ("i%u",
+              GPOINTER_TO_UINT (list->data)));
     for (list = priv->combi.out_combi_o; list != NULL; list = list->next)
-      g_ptr_array_add (arr, g_strdup_printf ("o%i",
-              GPOINTER_TO_INT (list->data)));
+      g_ptr_array_add (arr, g_strdup_printf ("o%u",
+              GPOINTER_TO_UINT (list->data)));
   }
 
   g_ptr_array_add (arr, NULL);
@@ -2130,7 +2136,7 @@ gst_tensor_filter_common_get_combined_in_info (GstTensorFilterPrivate * priv,
 
   if (priv->combi.in_combi_defined) {
     for (list = priv->combi.in_combi; list != NULL; list = list->next) {
-      i = GPOINTER_TO_INT (list->data);
+      i = GPOINTER_TO_UINT (list->data);
 
       if (i >= in->num_tensors) {
         nns_loge ("Invalid input index %u, failed to combine info.", i);
@@ -2177,7 +2183,7 @@ gst_tensor_filter_common_get_combined_out_info (GstTensorFilterPrivate * priv,
   if (priv->combi.out_combi_i_defined || priv->combi.out_combi_o_defined) {
     if (priv->combi.out_combi_i_defined) {
       for (list = priv->combi.out_combi_i; list != NULL; list = list->next) {
-        i = GPOINTER_TO_INT (list->data);
+        i = GPOINTER_TO_UINT (list->data);
 
         if (i >= in->num_tensors) {
           nns_loge ("Invalid input index %u, failed to combine info.", i);
@@ -2190,7 +2196,7 @@ gst_tensor_filter_common_get_combined_out_info (GstTensorFilterPrivate * priv,
 
     if (priv->combi.out_combi_o_defined) {
       for (list = priv->combi.out_combi_o; list != NULL; list = list->next) {
-        i = GPOINTER_TO_INT (list->data);
+        i = GPOINTER_TO_UINT (list->data);
 
         if (i >= out->num_tensors) {
           nns_loge ("Invalid output index %u, failed to combine info.", i);