Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.busybox.net/busybox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-09-25 23:04:45 +0300
committerDenys Vlasenko <vda.linux@googlemail.com>2021-09-25 23:04:45 +0300
commitd84a604830a7ee3f8fb5f3908ae0d54513393a20 (patch)
tree3e9dc05e082e41328a84c8eaa3d45f36c2ba5d44 /shell/math.c
parent627821e42b06adfe6bbc6004d8eeb7c35f65120d (diff)
shell: fix arithmentic evaluation of "++7" and such (it is + + 7, i.e. 7)
function old new delta evaluate_string 945 988 +43 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/math.c')
-rw-r--r--shell/math.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/shell/math.c b/shell/math.c
index 2942cdd26..049d5703b 100644
--- a/shell/math.c
+++ b/shell/math.c
@@ -668,19 +668,26 @@ evaluate_string(arith_state_t *math_state, const char *expr)
/* Should be an operator */
- /* Special case: NUM-- and NUM++ are not recognized if NUM
- * is a literal number, not a variable. IOW:
+ /* Special case: XYZ--, XYZ++, --XYZ, ++XYZ are recognized
+ * only if XYZ is a variable name, not a number or EXPR. IOW:
* "a+++v" is a++ + v.
* "7+++v" is 7 + ++v, not 7++ + v.
+ * "--7" is - - 7, not --7.
+ * "++++a" is + + ++a, not ++ ++ a.
+ * (we still mishandle "(a)+++7", should be treated as (a) + + + 7, but we do increment a)
*/
- if (lasttok == TOK_NUM && !numstackptr[-1].var /* number literal */
- && (expr[0] == '+' || expr[0] == '-')
+ if ((expr[0] == '+' || expr[0] == '-')
&& (expr[1] == expr[0])
) {
- //bb_error_msg("special %c%c", expr[0], expr[0]);
- op = (expr[0] == '+' ? TOK_ADD : TOK_SUB);
- expr += 1;
- goto tok_found1;
+ if (numstackptr == numstack || !numstackptr[-1].var) { /* not a VAR++ */
+ char next = skip_whitespace(expr + 2)[0];
+ if (!(isalpha(next) || next == '_')) { /* not a ++VAR */
+ //bb_error_msg("special %c%c", expr[0], expr[0]);
+ op = (expr[0] == '+' ? TOK_ADD : TOK_SUB);
+ expr++;
+ goto tok_found1;
+ }
+ }
}
p = op_tokens;