From 78635d55fb819d422d0c4c32bb63aab95f735e4b Mon Sep 17 00:00:00 2001 From: Li Yuxuan Date: Thu, 9 Mar 2023 11:11:28 +0800 Subject: audit_logging: Use `json_int_t` instead of `int` for `json_add_int` value type Functions like `add_lock_to_json` and `add_profile_item_to_json` pass some values to `json_add_int` with `intmax_t` types. This may cause arithmetic overflow when the value grows very fast, such as the read_bytes profiling data. Use `json_add_int` instead of `int` to avoid the overflow. RN: Make json output show intmax_t value properly Signed-off-by: Li Yuxuan Reviewed-by: Douglas Bagnall Reviewed-by: Andrew Bartlett Autobuild-User(master): Andrew Bartlett Autobuild-Date(master): Thu Mar 9 21:33:43 UTC 2023 on atb-devel-224 --- lib/audit_logging/audit_logging.c | 14 ++++++++------ lib/audit_logging/audit_logging.h | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'lib/audit_logging') diff --git a/lib/audit_logging/audit_logging.c b/lib/audit_logging/audit_logging.c index 43acf9512c9..3ab14b2a187 100644 --- a/lib/audit_logging/audit_logging.c +++ b/lib/audit_logging/audit_logging.c @@ -385,31 +385,33 @@ bool json_is_invalid(const struct json_object *object) * -1 the operation failed * */ -int json_add_int(struct json_object *object, const char *name, const int value) +int json_add_int(struct json_object *object, const char *name, const json_int_t value) { int ret = 0; json_t *integer = NULL; if (json_is_invalid(object)) { - DBG_ERR("Unable to add int [%s] value [%d], " + DBG_ERR("Unable to add int [%s] value [%jd], " "target object is invalid\n", name, - value); + (intmax_t)value); return JSON_ERROR; } integer = json_integer(value); if (integer == NULL) { - DBG_ERR("Unable to create integer value [%s] value [%d]\n", + DBG_ERR("Unable to create integer value [%s] value [%jd]\n", name, - value); + (intmax_t)value); return JSON_ERROR; } ret = json_object_set_new(object->root, name, integer); if (ret != 0) { json_decref(integer); - DBG_ERR("Unable to add int [%s] value [%d]\n", name, value); + DBG_ERR("Unable to add int [%s] value [%jd]\n", + name, + (intmax_t)value); } return ret; } diff --git a/lib/audit_logging/audit_logging.h b/lib/audit_logging/audit_logging.h index 49576ece68d..eb7c103944d 100644 --- a/lib/audit_logging/audit_logging.h +++ b/lib/audit_logging/audit_logging.h @@ -58,7 +58,7 @@ _WARN_UNUSED_RESULT_ bool json_is_invalid(const struct json_object *object); _WARN_UNUSED_RESULT_ int json_add_int(struct json_object *object, const char *name, - const int value); + const json_int_t value); _WARN_UNUSED_RESULT_ int json_add_bool(struct json_object *object, const char *name, const bool value); -- cgit v1.2.1