Skip to content

Commit

Permalink
shell: make ?x commands a parsing failure
Browse files Browse the repository at this point in the history
We have moved the `?x` commands to `%x`, thus now no command should
start with `?`. This patch makes the parser fail to parse `?x`
strings.
  • Loading branch information
ret2libc committed Sep 4, 2023
1 parent b71565b commit 32d3159
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 42 deletions.
6 changes: 4 additions & 2 deletions librz/core/cmd/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3972,11 +3972,13 @@ DEFINE_HANDLE_TS_FCN_AND_SYMBOL(help_stmt) {
if (node_str_len >= 2 && !strcmp(node_string + node_str_len - 2, "?*")) {
node_string[node_str_len - 2] = 0;
const char *argv[2] = { NULL, node_string };
return rz_cmd_help_search_handler(state->core, 2, argv, RZ_OUTPUT_MODE_STANDARD);
int argc = node_str_len > 2 ? 2 : 1;
return rz_cmd_help_search_handler(state->core, argc, argv, RZ_OUTPUT_MODE_STANDARD);
} else if (node_str_len >= 3 && !strcmp(node_string + node_str_len - 3, "?*j")) {
node_string[node_str_len - 3] = 0;
const char *argv[2] = { NULL, node_string };
return rz_cmd_help_search_handler(state->core, 2, argv, RZ_OUTPUT_MODE_JSON);
int argc = node_str_len > 2 ? 2 : 1;
return rz_cmd_help_search_handler(state->core, argc, argv, RZ_OUTPUT_MODE_JSON);
}

TSNode command = ts_node_child_by_field_name(node, "command", strlen("command"));
Expand Down
10 changes: 5 additions & 5 deletions subprojects/rizin-shell-parser/corpus/cmd_substitution.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Command substitution used as simple arg $(
=======================================

?e $(p8 10)
echo $(p8 10)

---

Expand All @@ -17,7 +17,7 @@ Command substitution used as simple arg $(
Command substitution with multiple statements
=======================================

?e $(p8 10; p8 4 @ 0xdeadbeef)
echo $(p8 10; p8 4 @ 0xdeadbeef)

---

Expand All @@ -38,7 +38,7 @@ Command substitution with multiple statements
Command substitution used as simple arg `
=======================================

?e `p8 10`
echo `p8 10`

---

Expand All @@ -54,7 +54,7 @@ Command substitution used as simple arg `
Nested command substitution
=======================================

?e $(p8 $(?e 10))
echo $(p8 $(echo 10))

---

Expand All @@ -71,7 +71,7 @@ Nested command substitution
Nested command substitution 2
=======================================

?e Hello$(?e Wor$(?e ld))
echo Hello$(echo Wor$(echo ld))

---

Expand Down
6 changes: 3 additions & 3 deletions subprojects/rizin-shell-parser/corpus/comments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ p8 4 # something
#! identified as comment if not command name
============================================

?e #!this is a comment
?e #this is also a comment
?e # and this too
echo #!this is a comment
echo #this is also a comment
echo # and this too

---

Expand Down
4 changes: 2 additions & 2 deletions subprojects/rizin-shell-parser/corpus/escape_args.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pd 10\@test\>name
Use newlines in echo
====================

?e "Hello\nWorld"
echo "Hello\nWorld"

---

Expand All @@ -30,7 +30,7 @@ Use newlines in echo
Escape hash
===========

?e Hello\#World
echo Hello\#World

---

Expand Down
6 changes: 3 additions & 3 deletions subprojects/rizin-shell-parser/corpus/grep_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ afl~$
Grep with cmd substitution as specifier
=======================================

pd 10~`?e mov`
pd 10~mo`?e v`
pd 10~mo$(?e v)
pd 10~`echo mov`
pd 10~mo`echo v`
pd 10~mo$(echo v)

---

Expand Down
4 changes: 2 additions & 2 deletions subprojects/rizin-shell-parser/corpus/iter_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ p8 @@/x 9090
Iter interpret cmd
==================

p8 @@c:?e hello
p8 @@c:echo hello

---

Expand All @@ -81,7 +81,7 @@ p8 @@c:?e hello
Iter interpret offsetssizes cmd
===============================

p8 @@@c:?e hello 10
p8 @@@c:echo hello 10

---

Expand Down
14 changes: 7 additions & 7 deletions subprojects/rizin-shell-parser/corpus/quoted_args.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Echo with (double) quoted args
==============================

?e "This;is.one@string"
echo "This;is.one@string"

---

Expand All @@ -14,7 +14,7 @@ Echo with (double) quoted args
Echo with (single) quoted args
==============================

?e 'This;is.one@string'
echo 'This;is.one@string'

---

Expand All @@ -27,7 +27,7 @@ Echo with (single) quoted args
Legacy command - all quoted
===========================

"?e This;is.one@string"
"echo This;is.one@string"

---

Expand All @@ -39,7 +39,7 @@ Legacy command - all quoted
Double quoted arg with cmd substitution
=======================================

?e "This is $(?e "a command")"
echo "This is $(echo "a command")"

---

Expand All @@ -56,9 +56,9 @@ Double quoted arg with cmd substitution
Quoted hash
==================

?e "#"
?e " #$(pdf)"
?e '#'
echo "#"
echo " #$(pdf)"
echo '#'

---

Expand Down
36 changes: 25 additions & 11 deletions subprojects/rizin-shell-parser/corpus/simple_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ af 0xdeadbeef
(args (arg (arg_identifier)))))


=========================
?v not valid
=========================

?v 3

---

(statements
(help_stmt
(cmd_identifier))
(ERROR))


======================
Percentage mark statements
======================
Expand Down Expand Up @@ -160,7 +174,7 @@ pf???
(help_stmt (cmd_identifier))
(help_stmt (cmd_identifier))
(help_stmt (cmd_identifier))
(arged_stmt (cmd_identifier))
(help_stmt (cmd_identifier))
(help_stmt (cmd_identifier))
(help_stmt (cmd_identifier))
(help_stmt (cmd_identifier))
Expand All @@ -173,7 +187,7 @@ pf???
Command with concatenation args
===============================

?e Hello" World"'!' And All
echo Hello" World"'!' And All

---

Expand All @@ -193,7 +207,7 @@ Command with concatenation args
Tasks command
=============

& ?e Hello
& echo Hello
&& 1
&=
&
Expand All @@ -215,7 +229,7 @@ Tasks command
Arg with (...)
==============

?e Hello(World)
echo Hello(World)

---

Expand All @@ -235,7 +249,7 @@ Arg with (...)
Expressions with (...)
============================

?v (100 + (2 - 3)) * 2
%v (100 + (2 - 3)) * 2

---

Expand Down Expand Up @@ -263,13 +277,13 @@ Expressions with (...)
Arg with $
==========

?v $$
%v $$
s $
?v $*
?v $$$ test
?v $alias
?e hello$alias
?e test
%v $*
%v $$$ test
%v $alias
echo hello$alias
echo test

---

Expand Down
2 changes: 1 addition & 1 deletion subprojects/rizin-shell-parser/corpus/special_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ Interpreter statements
Pointer type statements with substitution
=======================================

*entr$(?e y0)=$(?v $$)
*entr$(echo y0)=$(%v $$)

---

Expand Down
11 changes: 5 additions & 6 deletions subprojects/rizin-shell-parser/src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ static bool is_interpret_cmd(const char *s) {

static bool is_special_start(const int32_t ch) {
return ch == '*' || ch == '(' || ch == '@' || ch == '|' || ch == '>' ||
ch == '.' || ch == '|' || ch == '~' || ch == '!';
ch == '.' || ch == '|' || ch == '~' || ch == '!' || ch == '?';
}

static bool is_start_of_command(const int32_t ch) {
return iswalpha (ch) || ch == '$' || ch == '?' || ch == ':' || ch == '+' ||
return iswalpha (ch) || ch == '$' || ch == ':' || ch == '+' ||
ch == '=' || ch == '/' || ch == '_' || ch == '#' || ch == '\\' ||
ch == '-' || ch == '<' || ch == '&' || ch == '%' || is_special_start (ch);
}
Expand Down Expand Up @@ -180,10 +180,9 @@ bool tree_sitter_rzcmd_external_scanner_scan(void *payload, TSLexer *lexer, cons
if (is_comment (res)) {
return false;
}
// ?? is not considered an help command, just a regular one
if ((res[i_res - 1] == '?' && strcmp (res, "??") != 0) ||
(i_res > 2 && is_recursive_help (res[i_res - 2], res[i_res - 1])) ||
(i_res > 3 && is_recursive_help_json (res[i_res - 3], res[i_res - 2], res[i_res - 1]))) {
if ((res[i_res - 1] == '?') ||
(i_res >= 2 && is_recursive_help(res[i_res - 2], res[i_res - 1])) ||
(i_res >= 3 && is_recursive_help_json(res[i_res - 3], res[i_res - 2], res[i_res - 1]))) {
if (i_res == 1) {
return false;
}
Expand Down

0 comments on commit 32d3159

Please sign in to comment.