diff options
author | Mason Jones <20848827+smj-edison@users.noreply.github.com> | 2025-04-21 20:47:24 -0700 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2025-04-22 04:37:43 +0000 |
commit | 9eaf6be68bade1f74302b78cd8c2f68fddf20442 (patch) | |
tree | 0423426312f6c9372058e1eb697c0b43bf82c18c | |
parent | e0671d7c5396f6a5569792013e7e71121f42ce89 (diff) | |
download | jimtcl-9eaf6be68bade1f74302b78cd8c2f68fddf20442.zip jimtcl-9eaf6be68bade1f74302b78cd8c2f68fddf20442.tar.gz jimtcl-9eaf6be68bade1f74302b78cd8c2f68fddf20442.tar.bz2 |
Fix abort on boolean negation/plus unary operators
Currently jimtcl will abort if there is any unary operator besides ! in
front of a boolean. It is possible to construct an expression with -
or + before a boolean literal (e.g. expr {-true}) that currently aborts,
but should return an error. I implemented this error handling with
tcl's error message for this situation.
-rw-r--r-- | jim.c | 7 | ||||
-rw-r--r-- | tests/expr.test | 7 |
2 files changed, 14 insertions, 0 deletions
@@ -8452,6 +8452,13 @@ static int JimExprOpNumUnary(Jim_Interp *interp, struct JimExprNode *node) case JIM_EXPROP_NOT: wC = !bA; break; + case JIM_EXPROP_UNARYPLUS: + case JIM_EXPROP_UNARYMINUS: + rc = JIM_ERR; + Jim_SetResultFormatted(interp, + "can't use non-numeric string as operand of \"%s\"", + node->type == JIM_EXPROP_UNARYPLUS ? "+" : "-"); + break; default: abort(); } diff --git a/tests/expr.test b/tests/expr.test index 7e26c0a..bc52afd 100644 --- a/tests/expr.test +++ b/tests/expr.test @@ -154,5 +154,12 @@ test expr-5.3 {boolean in expression} { expr {true ? 4 : 5} } {4} +test expr-6.1 "Unary negation on boolean - should return error" -body { + expr {-true} +} -returnCodes error -result {can't use non-numeric string as operand of "-"} + +test expr-6.2 "Unary plus on boolean - should return error" -body { + expr {+true} +} -returnCodes error -result {can't use non-numeric string as operand of "+"} testreport |