Skip to content

Commit dbb29ec

Browse files
authored
Merge pull request #5566 from Luv-Ray/fix-expr-issue5560
`expr`: check prefix operation
2 parents a0ac3dd + 4470294 commit dbb29ec

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/uu/expr/src/syntax_tree.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,12 @@ fn push_token_to_either_stack(
332332
}
333333

334334
Token::PrefixOp { .. } | Token::ParOpen => {
335-
op_stack.push((token_idx, token.clone()));
336-
Ok(())
335+
if out_stack.is_empty() {
336+
op_stack.push((token_idx, token.clone()));
337+
Ok(())
338+
} else {
339+
Err(String::from("syntax error (operation should be prefix)"))
340+
}
337341
}
338342

339343
Token::ParClose => move_till_match_paren(out_stack, op_stack),

tests/by-util/test_expr.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ fn test_parenthesis() {
100100
.args(&["(", "1", "+", "1", ")", "*", "2"])
101101
.succeeds()
102102
.stdout_only("4\n");
103+
104+
new_ucmd!()
105+
.args(&["1", "(", ")"])
106+
.fails()
107+
.stderr_only("expr: syntax error (operation should be prefix)\n");
103108
}
104109

105110
#[test]
@@ -221,6 +226,11 @@ fn test_index() {
221226
.args(&["index", "αbcdef_f", "f"])
222227
.succeeds()
223228
.stdout_only("6\n");
229+
230+
new_ucmd!()
231+
.args(&["αbcdef", "index", "α"])
232+
.fails()
233+
.stderr_only("expr: syntax error (operation should be prefix)\n");
224234
}
225235

226236
#[test]
@@ -234,6 +244,11 @@ fn test_length() {
234244
.args(&["length", "abcdef"])
235245
.succeeds()
236246
.stdout_only("6\n");
247+
248+
new_ucmd!()
249+
.args(&["abcdef", "length"])
250+
.fails()
251+
.stderr_only("expr: syntax error (operation should be prefix)\n");
237252
}
238253

239254
#[test]
@@ -271,6 +286,11 @@ fn test_substr() {
271286
.args(&["substr", "abc", "1", "1"])
272287
.succeeds()
273288
.stdout_only("a\n");
289+
290+
new_ucmd!()
291+
.args(&["abc", "substr", "1", "1"])
292+
.fails()
293+
.stderr_only("expr: syntax error (operation should be prefix)\n");
274294
}
275295

276296
#[test]

0 commit comments

Comments
 (0)