[PATCH (3.5) 3/3] template escaping changed
template_escaping: template and correlation index escaping changed. correlation index escaping introduced, because it's a correct use-case, that somebody want to use @ character after template, in this case use '@@' after the template e.g.: ${USERNAME}@gmail.com caused bad behaviour and there wasn't any possible solution to reach the correct behaviour from now the correct usage is: ${USERNAME}@@gmail.com The template $ escaping changed from '\$' to '$$'\ because it was very confusing that the lexer could process '\' and the template_compile processed '\' too.
From now, the correct usage if you want to use $ in the output string is: $$HELLO instead of \$HELLO
Signed-off-by: Juhasz Viktor <jviktor@balabit.hu> --- lib/templates.c | 38 +++++++++++++++++++++++++++--------- tests/unit/test_template_compile.c | 38 +++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/lib/templates.c b/lib/templates.c index 6cd90ee..a34667a 100644 --- a/lib/templates.c +++ b/lib/templates.c @@ -871,19 +871,33 @@ log_template_add_func_elem(LogTemplate *self, GString *text, gint argc, gchar *a } static void -parse_msg_ref(gchar **p, gint *msg_ref) +parse_msg_ref(gchar *template_string, gchar **p, gint *msg_ref) { *msg_ref = 0; if ((**p) == '@') { (*p)++; - /* syntax: ${name}@1 to denote the log message index in the correllation state */ - while ((**p) >= '0' && (**p) <= '9') + if ((**p) >= '0' && (**p) <= '9') { - (*msg_ref) += (*msg_ref) * 10 + ((**p) - '0'); - (*p)++; + /* syntax: ${name}@1 to denote the log message index in the correllation state */ + while ((**p) >= '0' && (**p) <= '9') + { + (*msg_ref) += (*msg_ref) * 10 + ((**p) - '0'); + (*p)++; + } + *msg_ref += 1; + } + else + { + if ((**p) != '@') + { + msg_warning("Non-numeric correlation state ID found, assuming a literal '@' character. To avoid confusion when using a literal '@' after a macro or template function, write '@@' in the templ + evt_tag_str("Template", template_string), + NULL); + (*p)--; + } + *msg_ref = 0; } - *msg_ref += 1; } } @@ -978,7 +992,7 @@ log_template_process_braced_template(LogTemplate *self, gchar **p, GString *text return FALSE; } } - parse_msg_ref(p, &msg_ref); + parse_msg_ref(self->template, p, &msg_ref); log_template_add_elem(self, start, macro_len, default_value, text, msg_ref); return TRUE; } @@ -1091,7 +1105,7 @@ log_template_process_template_function(LogTemplate *self, gchar **p, GString *te goto error; } (*p)++; - parse_msg_ref(p, &msg_ref); + parse_msg_ref(self->template, p, &msg_ref); if (!log_template_add_func_elem(self, text, strv->len - 1, (gchar **) strv->pdata, msg_ref, error)) { goto error; @@ -1180,7 +1194,13 @@ log_template_process_character(LogTemplate *self, gchar **p, GString **text, GEr } if (**p == '\\') { - (*p)++; + if (cfg_is_config_version_older(self->cfg, 0x0305)) + { + msg_warning("Template escaping changed in syslog-ng-ose 3.5. Use '$$' to specify a literal dollar sign instead of '\\$' and remove the escaping of the backslash character when you upgrade your c + evt_tag_str("Template", self->template), + NULL); + (*p)++; + } } if (**p) { diff --git a/tests/unit/test_template_compile.c b/tests/unit/test_template_compile.c index 23fed4f..f73f653 100644 --- a/tests/unit/test_template_compile.c +++ b/tests/unit/test_template_compile.c @@ -10,6 +10,7 @@ #define SIMPLE_MACRO_UNBRACE_ADD_TEXT "$MESSAGE test value" #define SIMPLE_MACRO_MSGREF "${MESSAGE}@1" #define SIMPLE_MACRO_INVALID_REF "${MESSAGE}@gmail.com" +#define SIMPLE_MACRO_ESCAPED_REF "${MESSAGE}@@12" #define SIMPLE_MACRO_ADD_TEXT "${MESSAGE}test value" #define ESCAPED_CHAR "Test \\$STRING" #define VALID_SUBST "${MESSAGE:-default value}" @@ -20,6 +21,7 @@ #define TRICKY_VALUE_5 "$" #define TRICKY_VALUE_6 "${MESSAGE:-}" #define TRICKY_VALUE_7 "${}" +#define TRICKY_VALUE_8 "\\tsomething ${MESSAGE}" #define INVALID_MACRO "${MESSAGE" #define INVALID_SUBST "${MESSAGE:1}" @@ -36,6 +38,9 @@ #define SIMPLE_UNKNOWN_FUNCTION "$(unknown function)" +#define CURRENT_VERSION 0x0305 +#define PREVIOUS_VERSION 0x0304 + static void hello(LogMessage *msg, int argc, GString *argv[], GString *result) { @@ -147,17 +152,32 @@ test_template_compile_macro() assert_template_compile(SIMPLE_MACRO_INVALID_REF); element = (LogTemplateElem *)template->compiled_template->data; - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 1); + fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); + assert_macro_element(element[0], expected_element); + element = (LogTemplateElem *)template->compiled_template->next->data; + fill_expected_template_element(expected_element, text = "@gmail.com", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); + assert_macro_element(element[0], expected_element); + + assert_template_compile(SIMPLE_MACRO_ESCAPED_REF); + element = (LogTemplateElem *)template->compiled_template->data; + fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); assert_macro_element(element[0], expected_element); element = (LogTemplateElem *)template->compiled_template->next->data; - fill_expected_template_element(expected_element, text = "gmail.com", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); + fill_expected_template_element(expected_element, text = "@12", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); assert_macro_element(element[0], expected_element); + cfg_set_version(template->cfg, PREVIOUS_VERSION); assert_template_compile(ESCAPED_CHAR); element = (LogTemplateElem *)template->compiled_template->data; fill_expected_template_element(expected_element, text = "Test $STRING", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); assert_macro_element(element[0], expected_element); + cfg_set_version(template->cfg, CURRENT_VERSION); + assert_template_compile(ESCAPED_CHAR); + element = (LogTemplateElem *)template->compiled_template->data; + fill_expected_template_element(expected_element, text = "Test \\", default_value = NULL, value_handle=log_msg_get_value_handle("STRING"), type = LTE_VALUE, msg_ref = 0); + assert_value_element(element[0], expected_element); + assert_template_compile(VALID_SUBST); element = (LogTemplateElem *)template->compiled_template->data; fill_expected_template_element(expected_element, text = "", default_value = "default value", macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); @@ -178,11 +198,18 @@ test_template_compile_macro() fill_expected_template_element(expected_element, text = "${VALUE_NAME}", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); assert_macro_element(element[0], expected_element); + cfg_set_version(template->cfg, PREVIOUS_VERSION); assert_template_compile(TRICKY_VALUE_4); element = (LogTemplateElem *)template->compiled_template->data; fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); assert_macro_element(element[0], expected_element); + cfg_set_version(template->cfg, CURRENT_VERSION); + assert_template_compile(TRICKY_VALUE_4); + element = (LogTemplateElem *)template->compiled_template->data; + fill_expected_template_element(expected_element, text = "\\", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); + assert_macro_element(element[0], expected_element); + assert_template_compile(TRICKY_VALUE_5); element = (LogTemplateElem *)template->compiled_template->data; fill_expected_template_element(expected_element, text = "$", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); @@ -193,6 +220,11 @@ test_template_compile_macro() fill_expected_template_element(expected_element, text = "", default_value = "", macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); assert_macro_element(element[0], expected_element); + assert_template_compile(TRICKY_VALUE_8); + element = (LogTemplateElem *)template->compiled_template->data; + fill_expected_template_element(expected_element, text = "\\tsomething ", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); + assert_macro_element(element[0], expected_element); + log_template_unref(template); } @@ -303,7 +335,7 @@ test_template_compile_negativ_tests() int main(int argc, char **argv) { msg_init(FALSE); - configuration = cfg_new(0x305); + configuration = cfg_new(CURRENT_VERSION); log_msg_registry_init(); log_template_global_init(); --
participants (1)
-
Juhász Viktor