From 7a600f84b403c778d348aeab7dc64fe300ae6c84 Mon Sep 17 00:00:00 2001 From: Zsolt Varga Date: Fri, 8 Mar 2024 13:41:01 +0100 Subject: [PATCH] fix trace log message size calculation --- opa.c | 2 +- trace.c | 31 +++++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/opa.c b/opa.c index 7d3b9e4f..08986479 100644 --- a/opa.c +++ b/opa.c @@ -285,7 +285,7 @@ void opa_socket_context_free(opa_socket_context ctx) opa_socket_context parse_opa_socket_eval_result(char *json) { JSON_Value *root_value = json_parse_string(json); - opa_socket_context ret = {}; + opa_socket_context ret = {0}; if (root_value) { diff --git a/trace.c b/trace.c index bc220e23..610492d5 100644 --- a/trace.c +++ b/trace.c @@ -178,7 +178,7 @@ char *compose_log_message(const char *message, int n, va_list args) { int i; va_list args_copy; - char *retval; + char *retval = NULL; va_copy(args_copy, args); @@ -189,20 +189,27 @@ char *compose_log_message(const char *message, int n, va_list args) goto out; } - int size = strlen(message) + 3; - for (i = 0; i < n; i++) + int size = strlen(message); + if (n > 0) + size += 3; // for the separator + + for (i = 0; i < n; i += 2) { - const char *arg = va_arg(args, const char *); - if (arg == NULL) + const char *var = va_arg(args, const char *); + const char *value = va_arg(args, const char *); + + if (!var) { - if (i % 2 == 0) - { - retval = ERR_PTR(-EINVAL); - goto out; - } - continue; + retval = ERR_PTR(-EINVAL); + goto out; + } + + if (var && value) + { + size += strlen(var) + strlen(value) + 2; // +2 for [] + if (i < n - 2) + size += 1; // +1 for a space } - size += strlen(arg) + 1; } char *log_message = kzalloc(size + 1, GFP_KERNEL);