From 82697714e1b51968dc852c9194deb2e0bf159596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Mon, 8 Oct 2018 22:51:08 +0200 Subject: [PATCH] core: fix evaluation of nested ternary operators (closes #1263) --- ChangeLog.adoc | 1 + src/core/wee-eval.c | 10 +++++++--- tests/unit/core/test-eval.cpp | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ChangeLog.adoc b/ChangeLog.adoc index e16289623..1f152e8ab 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -33,6 +33,7 @@ New features:: Bug fixes:: + * core: fix evaluation of nested ternary operators (issue #1263) * core: fix evaluation of condition when the left operand is an empty string * core: fix string evaluation with regex replacement when the string is empty * core: fix check of tags in lines (command /filter and hook_print) diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index b9a092a65..9ba2dd9d2 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -317,7 +317,8 @@ eval_replace_vars_cb (void *data, const char *text) struct t_config_option *ptr_option; struct t_gui_buffer *ptr_buffer; char str_value[512], *value, *pos, *pos1, *pos2, *hdata_name, *list_name; - char *tmp, *info_name, *hide_char, *hidden_string, *error, *condition; + char *tmp, *tmp2, *info_name, *hide_char, *hidden_string, *error; + char *condition; const char *ptr_value, *ptr_arguments, *ptr_string; struct t_hdata *hdata; void *pointer; @@ -556,10 +557,13 @@ eval_replace_vars_cb (void *data, const char *text) strndup (text + 3, pos - (text + 3)) : strdup (text + 3); if (!condition) return strdup (""); - tmp = eval_expression_condition (condition, eval_context); - rc = eval_is_true (tmp); + tmp = eval_replace_vars (condition, eval_context); + tmp2 = eval_expression_condition ((tmp) ? tmp : "", eval_context); + rc = eval_is_true (tmp2); if (tmp) free (tmp); + if (tmp2) + free (tmp2); if (rc) { /* diff --git a/tests/unit/core/test-eval.cpp b/tests/unit/core/test-eval.cpp index 3a34f9b7b..5afa180ac 100644 --- a/tests/unit/core/test-eval.cpp +++ b/tests/unit/core/test-eval.cpp @@ -391,6 +391,13 @@ TEST(CoreEval, EvalExpression) WEE_CHECK_EVAL("yes-no", "${if:5>2?${if:1>7?yes-yes:yes-no}:${if:9>4?no-yes:no-no}}"); WEE_CHECK_EVAL("no-yes", "${if:1>7?${if:6>3?yes-yes:yes-no}:${if:9>4?no-yes:no-no}}"); WEE_CHECK_EVAL("no-no", "${if:1>7?${if:1>7?yes-yes:yes-no}:${if:1>7?no-yes:no-no}}"); + WEE_CHECK_EVAL("0", "${if:0}"); + WEE_CHECK_EVAL("1", "${if:1}"); + WEE_CHECK_EVAL("0", "${if:abc!=abc}"); + WEE_CHECK_EVAL("1", "${if:abc==abc}"); + WEE_CHECK_EVAL("1", "${if:${if:abc==abc}}"); + WEE_CHECK_EVAL("0", "${if:${rev:${if:42==42?hello:bye}}==eyb}"); + WEE_CHECK_EVAL("1", "${if:${rev:${if:42==42?hello:bye}}==olleh}"); /* test option */ snprintf (str_value, sizeof (str_value),