diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-25 22:04:45 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-25 22:04:45 +0200 |
commit | d84a604830a7ee3f8fb5f3908ae0d54513393a20 (patch) | |
tree | 3e9dc05e082e41328a84c8eaa3d45f36c2ba5d44 | |
parent | 627821e42b06adfe6bbc6004d8eeb7c35f65120d (diff) | |
download | busybox-w32-d84a604830a7ee3f8fb5f3908ae0d54513393a20.tar.gz busybox-w32-d84a604830a7ee3f8fb5f3908ae0d54513393a20.tar.bz2 busybox-w32-d84a604830a7ee3f8fb5f3908ae0d54513393a20.zip |
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>
-rw-r--r-- | shell/ash_test/ash-arith/arith.right | 20 | ||||
-rwxr-xr-x | shell/ash_test/ash-arith/arith.tests | 10 | ||||
-rwxr-xr-x | shell/ash_test/ash-arith/arith1.sub | 4 | ||||
-rwxr-xr-x | shell/ash_test/ash-arith/arith2.sub | 10 | ||||
-rw-r--r-- | shell/hush_test/hush-arith/arith.right | 20 | ||||
-rwxr-xr-x | shell/hush_test/hush-arith/arith.tests | 10 | ||||
-rwxr-xr-x | shell/hush_test/hush-arith/arith1.sub | 4 | ||||
-rwxr-xr-x | shell/hush_test/hush-arith/arith2.sub | 10 | ||||
-rw-r--r-- | shell/math.c | 23 |
9 files changed, 65 insertions, 46 deletions
diff --git a/shell/ash_test/ash-arith/arith.right b/shell/ash_test/ash-arith/arith.right index 6936f1269..99ef825f5 100644 --- a/shell/ash_test/ash-arith/arith.right +++ b/shell/ash_test/ash-arith/arith.right | |||
@@ -97,6 +97,7 @@ ghi | |||
97 | 3 3 | 97 | 3 3 |
98 | 4 4 | 98 | 4 4 |
99 | 4 4 | 99 | 4 4 |
100 | 7 7 | ||
100 | ./arith.tests: line 257: arithmetic syntax error | 101 | ./arith.tests: line 257: arithmetic syntax error |
101 | ./arith.tests: line 259: arithmetic syntax error | 102 | ./arith.tests: line 259: arithmetic syntax error |
102 | ./arith.tests: line 260: arithmetic syntax error | 103 | ./arith.tests: line 260: arithmetic syntax error |
@@ -105,6 +106,8 @@ ghi | |||
105 | 4 4 | 106 | 4 4 |
106 | 7 7 | 107 | 7 7 |
107 | -7 -7 | 108 | -7 -7 |
109 | 7 | ||
110 | 7 | ||
108 | ./arith1.sub: line 2: arithmetic syntax error | 111 | ./arith1.sub: line 2: arithmetic syntax error |
109 | ./arith1.sub: line 3: arithmetic syntax error | 112 | ./arith1.sub: line 3: arithmetic syntax error |
110 | ./arith1.sub: line 4: arithmetic syntax error | 113 | ./arith1.sub: line 4: arithmetic syntax error |
@@ -119,11 +122,12 @@ ghi | |||
119 | 2 2 | 122 | 2 2 |
120 | -2 -2 | 123 | -2 -2 |
121 | 1 1 | 124 | 1 1 |
122 | ./arith1.sub: line 37: arithmetic syntax error | 125 | 7 |
123 | ./arith2.sub: line 2: arithmetic syntax error | 126 | 7 |
124 | ./arith2.sub: line 3: arithmetic syntax error | 127 | 7 |
125 | ./arith2.sub: line 4: arithmetic syntax error | 128 | 7 |
126 | ./arith2.sub: line 5: arithmetic syntax error | 129 | 7 |
130 | 7 | ||
127 | 5 5 | 131 | 5 5 |
128 | 1 1 | 132 | 1 1 |
129 | 6 6 | 133 | 6 6 |
@@ -132,8 +136,10 @@ ghi | |||
132 | 1 1 | 136 | 1 1 |
133 | 4 4 | 137 | 4 4 |
134 | 0 0 | 138 | 0 0 |
135 | ./arith2.sub: line 42: arithmetic syntax error | 139 | -7 |
136 | ./arith2.sub: line 47: arithmetic syntax error | 140 | -7 |
141 | 7 | ||
142 | 7 | ||
137 | 8 12 | 143 | 8 12 |
138 | ./arith.tests: line 290: arithmetic syntax error | 144 | ./arith.tests: line 290: arithmetic syntax error |
139 | 42 | 145 | 42 |
diff --git a/shell/ash_test/ash-arith/arith.tests b/shell/ash_test/ash-arith/arith.tests index d65758e7d..746ccab71 100755 --- a/shell/ash_test/ash-arith/arith.tests +++ b/shell/ash_test/ash-arith/arith.tests | |||
@@ -252,8 +252,8 @@ echo 3 $x | |||
252 | echo 4 $(( ++x )) | 252 | echo 4 $(( ++x )) |
253 | echo 4 $x | 253 | echo 4 $x |
254 | 254 | ||
255 | # bash 3.2 apparently thinks that ++7 is 7 | 255 | # ++ is not a inc operator on non-variable, it is the + + sequence |
256 | #ash# echo 7 $(( ++7 )) | 256 | echo 7 $(( ++7 )) |
257 | ( echo $(( 7-- )) ) | 257 | ( echo $(( 7-- )) ) |
258 | 258 | ||
259 | ( echo $(( --x=7 )) ) | 259 | ( echo $(( --x=7 )) ) |
@@ -267,9 +267,9 @@ echo 4 $x | |||
267 | echo 7 $(( +7 )) | 267 | echo 7 $(( +7 )) |
268 | echo -7 $(( -7 )) | 268 | echo -7 $(( -7 )) |
269 | 269 | ||
270 | # bash 3.2 apparently thinks that ++7 is 7 | 270 | # ++ is not a inc operator on non-variable, it is the + + sequence |
271 | #ash# echo $(( ++7 )) | 271 | echo $(( ++7 )) |
272 | #ash# echo $(( --7 )) | 272 | echo $(( --7 )) |
273 | 273 | ||
274 | ${THIS_SH} ./arith1.sub | 274 | ${THIS_SH} ./arith1.sub |
275 | ${THIS_SH} ./arith2.sub | 275 | ${THIS_SH} ./arith2.sub |
diff --git a/shell/ash_test/ash-arith/arith1.sub b/shell/ash_test/ash-arith/arith1.sub index 80aa99922..a36785c67 100755 --- a/shell/ash_test/ash-arith/arith1.sub +++ b/shell/ash_test/ash-arith/arith1.sub | |||
@@ -35,6 +35,6 @@ echo 1 $a | |||
35 | 35 | ||
36 | #ash# (( ++ )) | 36 | #ash# (( ++ )) |
37 | ( echo $(( +++7 )) ) | 37 | ( echo $(( +++7 )) ) |
38 | # bash 3.2 apparently thinks that ++ +7 is 7 | 38 | # ++ is not a inc operator on non-variable, it is the + + sequence |
39 | #ash# echo $(( ++ + 7 )) | 39 | echo $(( ++ + 7 )) |
40 | #ash# (( -- )) | 40 | #ash# (( -- )) |
diff --git a/shell/ash_test/ash-arith/arith2.sub b/shell/ash_test/ash-arith/arith2.sub index 9105059db..29f9471d6 100755 --- a/shell/ash_test/ash-arith/arith2.sub +++ b/shell/ash_test/ash-arith/arith2.sub | |||
@@ -1,4 +1,4 @@ | |||
1 | # bash 3.2 apparently thinks that ++7 is 7 etc | 1 | # ++ and -- are not inc/dec operators on non-variables, they are + + and - - sequences |
2 | ( echo $(( --7 )) ) | 2 | ( echo $(( --7 )) ) |
3 | ( echo $(( ++7 )) ) | 3 | ( echo $(( ++7 )) ) |
4 | ( echo $(( -- 7 )) ) | 4 | ( echo $(( -- 7 )) ) |
@@ -37,13 +37,13 @@ echo 4 $(( 4 - -- a )) | |||
37 | echo 0 $a | 37 | echo 0 $a |
38 | 38 | ||
39 | #ash# (( -- )) | 39 | #ash# (( -- )) |
40 | # bash 3.2 apparently thinks that ---7 is -7 | 40 | # -- is not a dec operator on non-variable, it is the - - sequence |
41 | #ash# echo $(( ---7 )) | 41 | echo $(( ---7 )) |
42 | ( echo $(( -- - 7 )) ) | 42 | ( echo $(( -- - 7 )) ) |
43 | 43 | ||
44 | #ash# (( ++ )) | 44 | #ash# (( ++ )) |
45 | # bash 3.2: 7 | 45 | # ++ is not a inc operator on non-variable, it is the + + sequence |
46 | #ash# echo 7 $(( ++7 )) | 46 | echo $(( ++7 )) |
47 | ( echo $(( ++ + 7 )) ) | 47 | ( echo $(( ++ + 7 )) ) |
48 | 48 | ||
49 | # bash 3.2: -7 | 49 | # bash 3.2: -7 |
diff --git a/shell/hush_test/hush-arith/arith.right b/shell/hush_test/hush-arith/arith.right index c48e468a5..2c389caea 100644 --- a/shell/hush_test/hush-arith/arith.right +++ b/shell/hush_test/hush-arith/arith.right | |||
@@ -106,6 +106,7 @@ hush: arithmetic syntax error | |||
106 | 3 3 | 106 | 3 3 |
107 | 4 4 | 107 | 4 4 |
108 | 4 4 | 108 | 4 4 |
109 | 7 7 | ||
109 | hush: arithmetic syntax error | 110 | hush: arithmetic syntax error |
110 | hush: arithmetic syntax error | 111 | hush: arithmetic syntax error |
111 | hush: arithmetic syntax error | 112 | hush: arithmetic syntax error |
@@ -114,6 +115,8 @@ hush: arithmetic syntax error | |||
114 | 4 4 | 115 | 4 4 |
115 | 7 7 | 116 | 7 7 |
116 | -7 -7 | 117 | -7 -7 |
118 | 7 | ||
119 | 7 | ||
117 | hush: arithmetic syntax error | 120 | hush: arithmetic syntax error |
118 | hush: arithmetic syntax error | 121 | hush: arithmetic syntax error |
119 | hush: arithmetic syntax error | 122 | hush: arithmetic syntax error |
@@ -128,11 +131,12 @@ hush: arithmetic syntax error | |||
128 | 2 2 | 131 | 2 2 |
129 | -2 -2 | 132 | -2 -2 |
130 | 1 1 | 133 | 1 1 |
131 | hush: arithmetic syntax error | 134 | 7 |
132 | hush: arithmetic syntax error | 135 | 7 |
133 | hush: arithmetic syntax error | 136 | 7 |
134 | hush: arithmetic syntax error | 137 | 7 |
135 | hush: arithmetic syntax error | 138 | 7 |
139 | 7 | ||
136 | 5 5 | 140 | 5 5 |
137 | 1 1 | 141 | 1 1 |
138 | 6 6 | 142 | 6 6 |
@@ -141,8 +145,10 @@ hush: arithmetic syntax error | |||
141 | 1 1 | 145 | 1 1 |
142 | 4 4 | 146 | 4 4 |
143 | 0 0 | 147 | 0 0 |
144 | hush: arithmetic syntax error | 148 | -7 |
145 | hush: arithmetic syntax error | 149 | -7 |
150 | 7 | ||
151 | 7 | ||
146 | 8 12 | 152 | 8 12 |
147 | hush: arithmetic syntax error | 153 | hush: arithmetic syntax error |
148 | 42 | 154 | 42 |
diff --git a/shell/hush_test/hush-arith/arith.tests b/shell/hush_test/hush-arith/arith.tests index bc6b341d1..a7aded17d 100755 --- a/shell/hush_test/hush-arith/arith.tests +++ b/shell/hush_test/hush-arith/arith.tests | |||
@@ -255,8 +255,8 @@ echo 3 $x | |||
255 | echo 4 $(( ++x )) | 255 | echo 4 $(( ++x )) |
256 | echo 4 $x | 256 | echo 4 $x |
257 | 257 | ||
258 | # bash 3.2 apparently thinks that ++7 is 7 | 258 | # ++ is not a inc operator on non-variable, it is the + + sequence |
259 | #ash# echo 7 $(( ++7 )) | 259 | echo 7 $(( ++7 )) |
260 | ( echo $(( 7-- )) ) | 260 | ( echo $(( 7-- )) ) |
261 | 261 | ||
262 | ( echo $(( --x=7 )) ) | 262 | ( echo $(( --x=7 )) ) |
@@ -270,9 +270,9 @@ echo 4 $x | |||
270 | echo 7 $(( +7 )) | 270 | echo 7 $(( +7 )) |
271 | echo -7 $(( -7 )) | 271 | echo -7 $(( -7 )) |
272 | 272 | ||
273 | # bash 3.2 apparently thinks that ++7 is 7 | 273 | # ++ is not a inc operator on non-variable, it is the + + sequence |
274 | #ash# echo $(( ++7 )) | 274 | echo $(( ++7 )) |
275 | #ash# echo $(( --7 )) | 275 | echo $(( --7 )) |
276 | 276 | ||
277 | ${THIS_SH} ./arith1.sub | 277 | ${THIS_SH} ./arith1.sub |
278 | ${THIS_SH} ./arith2.sub | 278 | ${THIS_SH} ./arith2.sub |
diff --git a/shell/hush_test/hush-arith/arith1.sub b/shell/hush_test/hush-arith/arith1.sub index 80aa99922..a36785c67 100755 --- a/shell/hush_test/hush-arith/arith1.sub +++ b/shell/hush_test/hush-arith/arith1.sub | |||
@@ -35,6 +35,6 @@ echo 1 $a | |||
35 | 35 | ||
36 | #ash# (( ++ )) | 36 | #ash# (( ++ )) |
37 | ( echo $(( +++7 )) ) | 37 | ( echo $(( +++7 )) ) |
38 | # bash 3.2 apparently thinks that ++ +7 is 7 | 38 | # ++ is not a inc operator on non-variable, it is the + + sequence |
39 | #ash# echo $(( ++ + 7 )) | 39 | echo $(( ++ + 7 )) |
40 | #ash# (( -- )) | 40 | #ash# (( -- )) |
diff --git a/shell/hush_test/hush-arith/arith2.sub b/shell/hush_test/hush-arith/arith2.sub index 9105059db..29f9471d6 100755 --- a/shell/hush_test/hush-arith/arith2.sub +++ b/shell/hush_test/hush-arith/arith2.sub | |||
@@ -1,4 +1,4 @@ | |||
1 | # bash 3.2 apparently thinks that ++7 is 7 etc | 1 | # ++ and -- are not inc/dec operators on non-variables, they are + + and - - sequences |
2 | ( echo $(( --7 )) ) | 2 | ( echo $(( --7 )) ) |
3 | ( echo $(( ++7 )) ) | 3 | ( echo $(( ++7 )) ) |
4 | ( echo $(( -- 7 )) ) | 4 | ( echo $(( -- 7 )) ) |
@@ -37,13 +37,13 @@ echo 4 $(( 4 - -- a )) | |||
37 | echo 0 $a | 37 | echo 0 $a |
38 | 38 | ||
39 | #ash# (( -- )) | 39 | #ash# (( -- )) |
40 | # bash 3.2 apparently thinks that ---7 is -7 | 40 | # -- is not a dec operator on non-variable, it is the - - sequence |
41 | #ash# echo $(( ---7 )) | 41 | echo $(( ---7 )) |
42 | ( echo $(( -- - 7 )) ) | 42 | ( echo $(( -- - 7 )) ) |
43 | 43 | ||
44 | #ash# (( ++ )) | 44 | #ash# (( ++ )) |
45 | # bash 3.2: 7 | 45 | # ++ is not a inc operator on non-variable, it is the + + sequence |
46 | #ash# echo 7 $(( ++7 )) | 46 | echo $(( ++7 )) |
47 | ( echo $(( ++ + 7 )) ) | 47 | ( echo $(( ++ + 7 )) ) |
48 | 48 | ||
49 | # bash 3.2: -7 | 49 | # bash 3.2: -7 |
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) | |||
668 | 668 | ||
669 | /* Should be an operator */ | 669 | /* Should be an operator */ |
670 | 670 | ||
671 | /* Special case: NUM-- and NUM++ are not recognized if NUM | 671 | /* Special case: XYZ--, XYZ++, --XYZ, ++XYZ are recognized |
672 | * is a literal number, not a variable. IOW: | 672 | * only if XYZ is a variable name, not a number or EXPR. IOW: |
673 | * "a+++v" is a++ + v. | 673 | * "a+++v" is a++ + v. |
674 | * "7+++v" is 7 + ++v, not 7++ + v. | 674 | * "7+++v" is 7 + ++v, not 7++ + v. |
675 | * "--7" is - - 7, not --7. | ||
676 | * "++++a" is + + ++a, not ++ ++ a. | ||
677 | * (we still mishandle "(a)+++7", should be treated as (a) + + + 7, but we do increment a) | ||
675 | */ | 678 | */ |
676 | if (lasttok == TOK_NUM && !numstackptr[-1].var /* number literal */ | 679 | if ((expr[0] == '+' || expr[0] == '-') |
677 | && (expr[0] == '+' || expr[0] == '-') | ||
678 | && (expr[1] == expr[0]) | 680 | && (expr[1] == expr[0]) |
679 | ) { | 681 | ) { |
680 | //bb_error_msg("special %c%c", expr[0], expr[0]); | 682 | if (numstackptr == numstack || !numstackptr[-1].var) { /* not a VAR++ */ |
681 | op = (expr[0] == '+' ? TOK_ADD : TOK_SUB); | 683 | char next = skip_whitespace(expr + 2)[0]; |
682 | expr += 1; | 684 | if (!(isalpha(next) || next == '_')) { /* not a ++VAR */ |
683 | goto tok_found1; | 685 | //bb_error_msg("special %c%c", expr[0], expr[0]); |
686 | op = (expr[0] == '+' ? TOK_ADD : TOK_SUB); | ||
687 | expr++; | ||
688 | goto tok_found1; | ||
689 | } | ||
690 | } | ||
684 | } | 691 | } |
685 | 692 | ||
686 | p = op_tokens; | 693 | p = op_tokens; |