From 3a123f79b9d0e374c86ca87f7b607c89207ceaf6 Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Tue, 11 Jun 2024 16:08:46 +0200 Subject: [PATCH 1/2] macros: extract type information from MESSAGE, PROGRAM and HOST MESSAGE in particular can contain not just string data, but for example JSON as well. This is necessary for the $(format-json) function to work properly with a json type MESSAGE. Kudos to b1oodborne from Discord for finding this issue. Signed-off-by: Attila Szakacs --- lib/template/macros.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/template/macros.c b/lib/template/macros.c index ea5e27a896..e473a69fd8 100644 --- a/lib/template/macros.c +++ b/lib/template/macros.c @@ -1,6 +1,8 @@ /* * Copyright (c) 2002-2014 Balabit * Copyright (c) 1998-2014 Balázs Scheidler + * Copyright (c) 2024 Axoflow + * Copyright (c) 2024 Attila Szakacs * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -233,12 +235,12 @@ static struct timespec app_uptime; static GHashTable *macro_hash; static void -_result_append_value(GString *result, const LogMessage *lm, NVHandle handle) +_result_append_value(GString *result, const LogMessage *lm, NVHandle handle, LogMessageValueType *type) { const gchar *str; gssize len = 0; - str = log_msg_get_value(lm, handle, &len); + str = log_msg_get_value_with_type(lm, handle, &len, type); g_string_append_len(result, str, len); } @@ -530,7 +532,7 @@ log_macro_expand(gint id, LogTemplateEvalOptions *options, const LogMessage *msg const gchar *p1, *p2; int remaining, length; gssize host_len; - const gchar *host = log_msg_get_value(msg, LM_V_HOST, &host_len); + const gchar *host = log_msg_get_value_with_type(msg, LM_V_HOST, &host_len, &t); p1 = memchr(host, '@', host_len); @@ -547,7 +549,7 @@ log_macro_expand(gint id, LogTemplateEvalOptions *options, const LogMessage *msg } else { - _result_append_value(result, msg, LM_V_HOST); + _result_append_value(result, msg, LM_V_HOST, &t); } break; } @@ -561,14 +563,14 @@ log_macro_expand(gint id, LogTemplateEvalOptions *options, const LogMessage *msg gssize len; const gchar *p; - p = log_msg_get_value(msg, LM_V_LEGACY_MSGHDR, &len); + p = log_msg_get_value_with_type(msg, LM_V_LEGACY_MSGHDR, &len, &t); if (len > 0) g_string_append_len(result, p, len); else { /* message, complete with program name and pid */ len = result->len; - _result_append_value(result, msg, LM_V_PROGRAM); + _result_append_value(result, msg, LM_V_PROGRAM, &t); if (len != result->len) { const gchar *pid = log_msg_get_value(msg, LM_V_PID, &len); @@ -585,7 +587,7 @@ log_macro_expand(gint id, LogTemplateEvalOptions *options, const LogMessage *msg } case M_MESSAGE: { - _result_append_value(result, msg, LM_V_MESSAGE); + _result_append_value(result, msg, LM_V_MESSAGE, &t); break; } case M_SOURCE_IP: From 5b6178e987e0810a215e1b47e4faff7654c9dc9b Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Tue, 11 Jun 2024 16:22:57 +0200 Subject: [PATCH 2/2] news: add entry for #162 Signed-off-by: Attila Szakacs --- news/bugfix-162.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 news/bugfix-162.md diff --git a/news/bugfix-162.md b/news/bugfix-162.md new file mode 100644 index 0000000000..6b7c6a50d9 --- /dev/null +++ b/news/bugfix-162.md @@ -0,0 +1,3 @@ +macros: Fixed a bug which always set certain macros to string type + +The affected macros are `$PROGRAM`, `$HOST` and `$MESSAGE`.