From e64636438edeb8b842d8ea56cd2cc1d18ebcca97 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Mon, 4 Jul 2022 12:32:06 +0900 Subject: [PATCH] util: common: Replace prototype of json helper functions to get correct value The get_int_from_object() returns '-EINVAL' when json object doesn't contain ' key'. But it isn't correct because 'int' value is able to get the minus value. It means that when get_int_from_object() return -EINVAL, it is not error on side of user. To fix them, return the error and then get the value as the parameter. Also, get_double_from_object() has the same issue. Also, in order that keep the consistent prototype of json helper functions, changes the functiosn as following: - int get_int_from_object(json_object *obj, const char *key, int *data); - int get_double_from_object(json_object *obj, const char *key, double *data); - int get_boolean_from_object(json_object *obj, const char *key, bool *data); Change-Id: I0a9b9da36312a0d86920f259b1f702095f104da6 Signed-off-by: Chanwoo Choi --- include/util/common.h | 6 +++--- src/util/common.c | 46 +++++++++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/include/util/common.h b/include/util/common.h index f361886..a11475e 100644 --- a/include/util/common.h +++ b/include/util/common.h @@ -127,9 +127,9 @@ int sys_get_str(const char *fname, char *str); int sysfs_get_int(char *path, int *val); const char *get_string_from_object(json_object *obj, const char *key); -const int get_int_from_object(json_object *obj, const char *key); -const double get_double_from_object(json_object *obj, const char *key); -const int get_boolean_from_object(json_object *obj, const char *key); +int get_int_from_object(json_object *obj, const char *key, int *data); +int get_double_from_object(json_object *obj, const char *key, double *data); +int get_boolean_from_object(json_object *obj, const char *key, bool *data); json_object *get_object_from_object(json_object *obj, const char *key); int get_property(json_object *obj, const char *key, int type, bool mandatory, diff --git a/src/util/common.c b/src/util/common.c index aae3582..b9368d5 100644 --- a/src/util/common.c +++ b/src/util/common.c @@ -99,34 +99,40 @@ const char *get_string_from_object(json_object *obj, const char *key) return json_object_get_string(tmp); } -const int get_int_from_object(json_object *obj, const char *key) +int get_int_from_object(json_object *obj, const char *key, int *data) { json_object *tmp = NULL; if (!json_object_object_get_ex(obj, key, &tmp)) return -EINVAL; - return json_object_get_int(tmp); + *data = json_object_get_int(tmp); + + return 0; } -const double get_double_from_object(json_object *obj, const char *key) +int get_double_from_object(json_object *obj, const char *key, double *data) { json_object *tmp = NULL; if (!json_object_object_get_ex(obj, key, &tmp)) return -EINVAL; - return json_object_get_double(tmp); + *data = json_object_get_double(tmp); + + return 0; } -const int get_boolean_from_object(json_object *obj, const char *key) +int get_boolean_from_object(json_object *obj, const char *key, bool *data) { json_object *tmp = NULL; if (!json_object_object_get_ex(obj, key, &tmp)) return -EINVAL; - return (int)json_object_get_boolean(tmp); + *data = json_object_get_boolean(tmp); + + return 0; } json_object *get_object_from_object(json_object *obj, const char *key) @@ -143,25 +149,19 @@ int get_property(json_object *obj, const char *key, int type, bool mandatory, void *data) { int ret = 0; - int val_int; - double val_double; const char *val_str; + bool val_bool; switch (type) { case DATA_TYPE_INT: - val_int = get_int_from_object(obj, key); - if (val_int < 0) - ret = val_int; - else - *(int *)data = val_int; + ret = get_int_from_object(obj, key, (int *)data); + if (ret < 0) + *(int *)data = ret; break; case DATA_TYPE_DOUBLE: - val_double = get_double_from_object(obj, key); - if (val_double < 0) - ret = -EINVAL; - else - *(double *)data = val_double; - break; + ret = get_double_from_object(obj, key, (double *)data); + if (ret < 0) + *(double *)data = (double)ret; case DATA_TYPE_STRING: val_str = get_string_from_object(obj, key); if (!val_str) @@ -170,11 +170,11 @@ int get_property(json_object *obj, const char *key, snprintf((char *)data, BUFF_MAX, "%s", val_str); break; case DATA_TYPE_BOOLEAN: - val_int = get_int_from_object(obj, key); - if (val_int < 0) - ret = val_int; + ret = get_boolean_from_object(obj, key, (bool *)&val_bool); + if (ret < 0) + *(bool *)data = true; else - *(int *)data = val_int; + *(bool *)data = val_bool; break; default: ret = -EINVAL; -- 2.7.4