Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved syntax error handling on tmpseek expressions ##shell #23361

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ static void recursive_help(RCore *core, int detail, const char *cmd_prefix) {
recursive_help (core, detail, "%");
recursive_help (core, detail, "(");
recursive_help (core, detail, "@");
recursive_help (core, detail, "'?'");
recursive_help (core, detail, "!");
recursive_help (core, detail, "=");
recursive_help (core, detail, "??");
Expand Down Expand Up @@ -4127,7 +4128,7 @@ static int r_core_cmd_subst(RCore *core, char *cmd) {
}
r_cons_break_push (NULL, NULL);
R_CRITICAL_ENTER (core);
bool ocur_enabled = core->print && core->print->cur_enabled;
const bool ocur_enabled = core->print && core->print->cur_enabled;
R_CRITICAL_LEAVE (core);
while (rep-- > 0 && *cmd) {
if (r_cons_was_breaked ()) {
Expand Down Expand Up @@ -4325,7 +4326,7 @@ static int r_core_cmd_subst_i(RCore *core, char *cmd, char *colon, bool *tmpseek
if (haveQuote) {
cmd++;
p = *cmd ? find_eoq (cmd) : NULL;
if (!p || !*p) {
if (R_STR_ISEMPTY (p)) {
if (!strcmp (cmd, "?")) {
r_core_cmd_help (core, help_msg_quote);
} else {
Expand Down Expand Up @@ -4901,10 +4902,8 @@ repeat:;
if (arroba) {
*arroba = 0;
}
ptr = (char *)r_str_trim_head_ro (ptr);

for (; *ptr == ' '; ptr++) {
//nothing to see here
}
if (*ptr && ptr[1] == ':') {
/* do nothing here */
} else {
Expand Down Expand Up @@ -4950,6 +4949,7 @@ repeat:;
R_LOG_TODO ("what do you expect for @. import offset from file maybe?");
}
} else if (ptr[0] && ptr[1] == ':' && ptr[2]) {
// TODO move into a separate function
switch (ptr[0]) {
case 'F': // "@F:" // temporary flag space
flgspc_changed = r_flag_space_push (core->flags, ptr + 2);
Expand Down Expand Up @@ -5235,7 +5235,25 @@ break;
r_str_trim_head (ptr + 1);
offstr = ptr + 1;

addr = (*offstr == '{')? core->offset: r_num_math (core->num, offstr);
switch (*offstr) {
case '{':
addr = core->offset;
break;
case '@':
case '?':
// nothing
break;
default:
{
ut64 n = r_num_math (core->num, offstr);
if (core->num->nc.errors) {
R_LOG_ERROR ("Invalid tmpseek address '%s'", offstr);
return 0;
}
addr = n;
}
break;
}
addr_is_set = true;

if (isalpha ((ut8)ptr[1]) && !addr) {
Expand Down Expand Up @@ -5269,10 +5287,7 @@ break;
}
if (ptr[1] == '@') { // "@@"
if (ptr[2] == '@') { // "@@@"
char *rule = ptr + 3;
while (*rule && *rule == ' ') {
rule++;
}
char *rule = (char *)r_str_trim_head_ro (ptr + 3);
ret = r_core_cmd_foreach3 (core, cmd, rule);
} else {
ret = r_core_cmd_foreach (core, cmd, ptr + 2);
Expand All @@ -5295,7 +5310,7 @@ break;
goto fail;
}
char *arg = p + 1;
int arg_len = strlen (arg);
const int arg_len = strlen (arg);
if (arg_len > 0) {
arg[arg_len - 1] = 0;
}
Expand Down Expand Up @@ -5825,7 +5840,7 @@ R_API int r_core_cmd_foreach3(RCore *core, const char *cmd, char *each) { // "@@
return 0;
}

static void foreachWord(RCore *core, const char *_cmd, const char *each) {
static void cmd_foreach_word(RCore *core, const char *_cmd, const char *each) {
char *cmd = strdup (_cmd);
char *nextLine = NULL;
/* foreach list of items */
Expand Down Expand Up @@ -5878,7 +5893,7 @@ static void foreachWord(RCore *core, const char *_cmd, const char *each) {
free (cmd);
}

static void foreachOffset(RCore *core, const char *_cmd, const char *each) {
static void cmd_foreach_offset(RCore *core, const char *_cmd, const char *each) {
char *cmd = strdup (_cmd);
char *nextLine = NULL;
ut64 addr;
Expand Down Expand Up @@ -6119,16 +6134,16 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) {
if (each[1] == ':') {
char *arg = r_core_cmd_str (core, each + 2);
if (arg) {
foreachOffset (core, cmd, arg);
cmd_foreach_offset (core, cmd, arg);
free (arg);
}
}
break;
case '=': // "@@="
if (each[1] == '=') {
foreachWord (core, cmd, r_str_trim_head_ro (str + 2));
cmd_foreach_word (core, cmd, r_str_trim_head_ro (str + 2));
} else {
foreachOffset (core, cmd, r_str_trim_head_ro (str + 1));
cmd_foreach_offset (core, cmd, r_str_trim_head_ro (str + 1));
}
break;
case 'd': // "@@d"
Expand Down
16 changes: 9 additions & 7 deletions libr/util/unum.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2023 - pancake */
/* radare - LGPL - Copyright 2007-2024 - pancake */

#define R_LOG_ORIGIN "util.num"

Expand Down Expand Up @@ -163,6 +163,7 @@ R_API char *r_num_units(char *buf, size_t len, ut64 num) {
}

R_API const char *r_num_get_name(RNum *num, ut64 n) {
R_RETURN_VAL_IF_FAIL (num, NULL);
if (num->cb_from_value) {
int ok = 0;
const char *msg = num->cb_from_value (num, n, &ok);
Expand All @@ -176,7 +177,7 @@ R_API const char *r_num_get_name(RNum *num, ut64 n) {
return NULL;
}

static void error(RNum *num, const char *err_str) {
static void error(R_NULLABLE RNum *num, const char *err_str) {
if (num) {
if (err_str) {
num->nc.errors++;
Expand Down Expand Up @@ -233,7 +234,7 @@ R_API ut64 r_num_from_ternary(const char *inp) {

// TODO: try to avoid the use of sscanf
/* old get_offset */
R_API ut64 r_num_get(RNum *num, const char *str) {
R_API ut64 r_num_get(R_NULLABLE RNum *num, const char *str) {
int i, j, ok;
char lch, len;
ut64 ret = 0LL;
Expand Down Expand Up @@ -545,7 +546,7 @@ R_API ut64 r_num_chs(int cylinder, int head, int sector, int sectorsize) {
return (ut64)cylinder * (ut64)head * (ut64)sector * (ut64)sectorsize;
}

R_API int r_num_conditional(RNum *num, const char *str) {
R_API int r_num_conditional(R_NULLABLE RNum *num, const char *str) {
char *lgt, *t, *p, *s = strdup (str);
int res = 0;
ut64 n, a, b;
Expand Down Expand Up @@ -623,7 +624,7 @@ R_API int r_num_is_valid_input(RNum *num, const char *input_value) {
return !(value == 0 && input_value && *input_value != '0') || !(value == 0 && input_value && *input_value != '@');
}

R_API ut64 r_num_get_input_value(RNum *num, const char *input_value) {
R_API ut64 r_num_get_input_value(R_NULLABLE RNum *num, const char *input_value) {
ut64 value = input_value ? r_num_math (num, input_value) : 0;
return value;
}
Expand All @@ -636,7 +637,8 @@ static int escape_char(char* dst, char byte) {
*(dst++) = escape_map [byte - 7];
*dst = 0;
return 2;
} else if (byte) {
}
if (byte) {
*(dst++) = '\\';
*(dst++) = 'x';
*(dst++) = NIBBLE_TO_HEX (byte >> 4);
Expand Down Expand Up @@ -677,7 +679,7 @@ R_API char* r_num_as_string(RNum *___, ut64 n, bool printable_only) {
return NULL;
}

R_API bool r_is_valid_input_num_value(RNum *num, const char *input_value) {
R_API bool r_is_valid_input_num_value(R_NULLABLE RNum *num, const char *input_value) {
if (!input_value) {
return false;
}
Expand Down
Loading