lib: resource-monitor: Fix get_value_string issue by allocating string buffer 95/279295/2
authorChanwoo Choi <cw00.choi@samsung.com>
Tue, 2 Aug 2022 06:06:12 +0000 (15:06 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Sun, 7 Aug 2022 20:32:13 +0000 (05:32 +0900)
The resource-monitor has the following policy of getting sting value
1. resource-monitor allocate the meory for string value
2. user of resource-monitor have to free the allocated memory

But, pass_resource_monitor_get_value_string didn't allocate
the memory for string value until now. Allocate memory for getting
string value on pass_resource_monitor_get_value_string.

Change-Id: I1517ab4362d754cb75b54d1887c1d031de14787d
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
lib/resource-monitor/resource-monitor.c
lib/resource-monitor/resource-monitor.h
tests/integration-test/resource-monitor-tests.cpp
tools/resource-monitor/resource-monitor.c

index 2a4b637..c21bdb9 100644 (file)
@@ -694,13 +694,17 @@ int pass_resource_monitor_get_value_double(int id, int resource_id, u_int64_t at
 }
 
 EXPORT
-int pass_resource_monitor_get_value_string(int id, int resource_id, u_int64_t attr_id, char *value)
+int pass_resource_monitor_get_value_string(int id, int resource_id, u_int64_t attr_id, char **value)
 {
        char buffer[GENERIC_BUFF_MAX + 1];
+       char value_string[BUFF_MAX];
        int buffer_len;
        int response_req;
        int ret;
 
+       if (!value)
+               return -EINVAL;
+
        buffer_len = sprintf(buffer, "%d$%d$%"PRIu64,
                        REQUEST_GET_VALUE_STRING, resource_id, attr_id);
        if (send(id, buffer, buffer_len, 0) < 0) {
@@ -716,7 +720,7 @@ int pass_resource_monitor_get_value_string(int id, int resource_id, u_int64_t at
        }
 
        buffer[buffer_len] = '\0';
-       if (sscanf(buffer, "%d$%[^$]$%d", &response_req, value, &ret) < 3)
+       if (sscanf(buffer, "%d$%[^$]$%d", &response_req, value_string, &ret) < 3)
                return -EINVAL;
 
        if (response_req != REQUEST_GET_VALUE_STRING) {
@@ -724,6 +728,8 @@ int pass_resource_monitor_get_value_string(int id, int resource_id, u_int64_t at
                return -EINVAL;
        }
 
+       *value = g_strdup(value_string);
+
        return ret;
 }
 
index 1a97cb6..1311975 100644 (file)
@@ -278,7 +278,7 @@ int pass_resource_monitor_get_value_int64(int id, int resource_id, u_int64_t att
 int pass_resource_monitor_get_value_uint(int id, int resource_id, u_int64_t attr, u_int32_t *value);
 int pass_resource_monitor_get_value_uint64(int id, int resource_id, u_int64_t attr, u_int64_t *value);
 int pass_resource_monitor_get_value_double(int id, int resource_id, u_int64_t attr, double *value);
-int pass_resource_monitor_get_value_string(int id, int resource_id, u_int64_t attr, char *value);
+int pass_resource_monitor_get_value_string(int id, int resource_id, u_int64_t attr, char **value);
 
 /**
  * @brief Get [int/int64/uint/uint64/double/string] array of resource attribute
index effe0cb..2b67de4 100644 (file)
@@ -648,11 +648,14 @@ static int __pass_resource_monitor_get_value(int mon_id, int res_id, int num_att
                        EXPECT_EQ(ret, 0);
                        break;
                case DATA_TYPE_STRING:
-                       char value_str[BUFF_MAX];
+               {
+                       char *value_str = NULL;
                        ret = pass_resource_monitor_get_value_string(mon_id, res_id,
-                                               attrs[i].attr_id, value_str);
+                                               attrs[i].attr_id, &value_str);
+                       g_free(value_str);
                        EXPECT_EQ(ret, 0);
                        break;
+               }
                case DATA_TYPE_ARRAY:
                        int length;
 
index cb44a5a..e454859 100644 (file)
@@ -16,6 +16,8 @@
  * limitations under the License.
  */
 
+#include <glib.h>
+
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -45,7 +47,7 @@ struct resource_attr_data {
        int32_t value_int32;
        int64_t value_int64;
        double value_double;
-       char value_string[BUFF_MAX];
+       char *value_string;
 };
 
 static struct resource_attr_data cpu_attrs[] = {
@@ -402,12 +404,13 @@ static inline void get_resource_attr_value(struct resource_data *res, int i)
                ret = pass_resource_monitor_get_value_string(
                                                res->mon_id, res->res_id,
                                                res->attrs[i].id,
-                                               res->attrs[i].value_string);
+                                               &res->attrs[i].value_string);
                if (ret < 0) break;
 
                printf("%40s | %-5s | %s",
                                res->attrs[i].value_string,
                                res->attrs[i].unit, res->attrs[i].desc);
+               g_free(res->attrs[i].value_string);
                break;
        case DATA_TYPE_ARRAY:
                ret = get_resource_attr_array_value(res, i);