From: Chanwoo Choi Date: Mon, 4 Jul 2022 03:32:06 +0000 (+0900) Subject: util: common: Replace prototype of json helper functions to get correct value X-Git-Tag: accepted/tizen/unified/20220707.133338~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F49%2F277249%2F9;p=platform%2Fcore%2Fsystem%2Fpass.git 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 --- 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;