From 42e4691cf555b9670c5af8d8452c59220f0aa45d Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 10 Aug 2023 15:05:22 +0200 Subject: [PATCH] audit_logging: Simplify json_add_stringn() with json_stringn() Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison Autobuild-User(master): Jeremy Allison Autobuild-Date(master): Mon Aug 14 20:51:11 UTC 2023 on atb-devel-224 --- lib/audit_logging/audit_logging.c | 8 +------- .../tests/audit_logging_error_test.c | 19 +++++++++++++++++-- lib/audit_logging/wscript_build | 1 + 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/audit_logging/audit_logging.c b/lib/audit_logging/audit_logging.c index 496e347659c..8ed15edffb6 100644 --- a/lib/audit_logging/audit_logging.c +++ b/lib/audit_logging/audit_logging.c @@ -644,13 +644,7 @@ int json_add_stringn(struct json_object *object, } if (value != NULL && len > 0) { - json_t *string = NULL; - char buffer[len+1]; - - strncpy(buffer, value, len); - buffer[len] = '\0'; - - string = json_string(buffer); + json_t *string = json_stringn(value, len); if (string == NULL) { DBG_ERR("Unable to add string [%s], " "could not create string object\n", diff --git a/lib/audit_logging/tests/audit_logging_error_test.c b/lib/audit_logging/tests/audit_logging_error_test.c index a7bea9d0532..12a81ff018d 100644 --- a/lib/audit_logging/tests/audit_logging_error_test.c +++ b/lib/audit_logging/tests/audit_logging_error_test.c @@ -118,6 +118,21 @@ json_t *__wrap_json_string(const char *value) return __real_json_string(value); } +/* + * cmocka wrappers for json_stringn + */ +json_t *__wrap_json_stringn(const char *value, size_t len); +json_t *__real_json_stringn(const char *value, size_t len); +json_t *__wrap_json_stringn(const char *value, size_t len) +{ + + bool fail = (bool)mock(); + if (fail) { + return NULL; + } + return __real_json_stringn(value, len); +} + /* * cmocka wrappers for json_dumps */ @@ -510,7 +525,7 @@ static void test_json_add_stringn(_UNUSED_ void **state) /* * Test json string failure */ - will_return(__wrap_json_string, true); + will_return(__wrap_json_stringn, true); rc = json_add_stringn(&object, "name", "value", 3); assert_false(json_is_invalid(&object)); @@ -519,7 +534,7 @@ static void test_json_add_stringn(_UNUSED_ void **state) /* * Test json object set new failure */ - will_return(__wrap_json_string, false); + will_return(__wrap_json_stringn, false); will_return(__wrap_json_object_set_new, JANSSON_FAILURE); rc = json_add_stringn(&object, "name", "value", 3); diff --git a/lib/audit_logging/wscript_build b/lib/audit_logging/wscript_build index 3d530975ac6..90c48eedffb 100644 --- a/lib/audit_logging/wscript_build +++ b/lib/audit_logging/wscript_build @@ -47,6 +47,7 @@ if bld.AD_DC_BUILD_IS_ENABLED(): -Wl,--wrap,json_array_extend -Wl,--wrap,json_object -Wl,--wrap,json_string + -Wl,--wrap,json_stringn -Wl,--wrap,json_integer -Wl,--wrap,json_array -Wl,--wrap,json_dumps