From 0e052cb2cab204427e49c40f6bb6acf8a4352dab Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 2 Aug 2022 15:06:12 +0900 Subject: [PATCH] lib: resource-monitor: Fix get_value_string issue by allocating string buffer 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 --- lib/resource-monitor/resource-monitor.c | 10 ++++++++-- lib/resource-monitor/resource-monitor.h | 2 +- tests/integration-test/resource-monitor-tests.cpp | 7 +++++-- tools/resource-monitor/resource-monitor.c | 7 +++++-- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/resource-monitor/resource-monitor.c b/lib/resource-monitor/resource-monitor.c index 2a4b637..c21bdb9 100644 --- a/lib/resource-monitor/resource-monitor.c +++ b/lib/resource-monitor/resource-monitor.c @@ -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; } diff --git a/lib/resource-monitor/resource-monitor.h b/lib/resource-monitor/resource-monitor.h index 1a97cb6..1311975 100644 --- a/lib/resource-monitor/resource-monitor.h +++ b/lib/resource-monitor/resource-monitor.h @@ -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 diff --git a/tests/integration-test/resource-monitor-tests.cpp b/tests/integration-test/resource-monitor-tests.cpp index effe0cb..2b67de4 100644 --- a/tests/integration-test/resource-monitor-tests.cpp +++ b/tests/integration-test/resource-monitor-tests.cpp @@ -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; diff --git a/tools/resource-monitor/resource-monitor.c b/tools/resource-monitor/resource-monitor.c index cb44a5a..e454859 100644 --- a/tools/resource-monitor/resource-monitor.c +++ b/tools/resource-monitor/resource-monitor.c @@ -16,6 +16,8 @@ * limitations under the License. */ +#include + #include #include #include @@ -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); -- 2.34.1