Skip to content

Commit

Permalink
Minor cleanup in cmd_print (portion of #19713)
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae committed Feb 25, 2024
1 parent 1be3c54 commit 1b92c60
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
10 changes: 5 additions & 5 deletions libr/core/casm.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ static int is_hit_inrange(RCoreAsmHit *hit, ut64 start_range, ut64 end_range) {

R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
// if (n > core->blocksize) n = core->blocksize;
ut64 at;
ut32 idx = 0, hit_count;
int numinstr, asmlen, ii;
const int addrbytes = core->io->addrbytes;
Expand All @@ -496,8 +495,7 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
if (!hits) {
return NULL;
}

len = R_MIN (len - len % addrbytes, addrbytes * addr);
len = R_MIN (len - (len % addrbytes), addrbytes * addr);
if (len < 1) {
r_list_free (hits);
return NULL;
Expand All @@ -511,7 +509,7 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
free (buf);
return NULL;
}
if (!r_io_read_at (core->io, addr - len / addrbytes, buf, len)) {
if (!r_io_read_at (core->io, addr - (len / addrbytes), buf, len)) {
r_list_free (hits);
free (buf);
return NULL;
Expand All @@ -538,7 +536,9 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
break;
}
}
at = addr - idx / addrbytes;

ut64 at = addr - idx / addrbytes;

r_asm_set_pc (core->rasm, at);
for (hit_count = 0; hit_count < n; hit_count++) {
RAnalOp op;
Expand Down
35 changes: 18 additions & 17 deletions libr/core/cmd_print.inc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2023 - pancake */
/* radare - LGPL - Copyright 2009-2024 - pancake */

#if R_INCLUDE_BEGIN

Expand Down Expand Up @@ -5357,6 +5357,7 @@ static ut8 *decode_text(RCore *core, ut64 offset, size_t len, bool zeroend) {
}

static bool cmd_pi(RCore *core, const char *input, int len, int l, ut8 *block) {
// len is block_len
char ch = input[1];
if (ch == '+' || ch == '-' || IS_DIGIT (ch)) {
ch = ' ';
Expand Down Expand Up @@ -6282,12 +6283,9 @@ static int cmd_print(void *data, const char *input) {
return 0;
}

const char *sp = NULL;
if (input[1] == '.' || input[1] == '+') {
sp = input + 2;
} else {
sp = strchr (input + 1, ' ');
}
const char *sp = (input[1] == '.' || input[1] == '+')
? input + 2: strchr (input + 1, ' ');

if (IS_DIGIT (input[1])) {
sp = input + 1;
} else if (!sp && input[1] == '-') {
Expand All @@ -6314,12 +6312,15 @@ static int cmd_print(void *data, const char *input) {
l = use_blocksize;
}
// may be unnecessary, fixes 'pd 1;pdj 100;pd 1' bug
#if 0
core->offset = at; // "pd" doesnt know about the current offset for pd -X
#endif
r_core_block_read (core);

switch (input[1]) {
case 'C': // "pdC"
r_core_disasm_pdi (core, l, 0, 'C');
pd_result = 0;
pd_result = false;
processed_cmd = true;
break;
case 'v': // "pdv" // east decompiler
Expand All @@ -6340,7 +6341,7 @@ static int cmd_print(void *data, const char *input) {
break;
case 'c': // "pdc" // "pDc"
r_core_pseudo_code (core, input + 2);
pd_result = 0;
pd_result = false;
processed_cmd = true;
break;
case ',': // "pd,"
Expand Down Expand Up @@ -6489,7 +6490,7 @@ static int cmd_print(void *data, const char *input) {
r_core_return_value (core, dislen);
}
free (block);
pd_result = 0;
pd_result = false;
}
} else {
R_LOG_ERROR ("Cannot find function at 0x%08"PFMT64x, core->offset);
Expand Down Expand Up @@ -6566,7 +6567,7 @@ static int cmd_print(void *data, const char *input) {
pj_end (pj);
r_cons_printf ("%s\n", pj_string (pj));
pj_free (pj);
pd_result = 0;
pd_result = false;
r_config_set (core->config, "asm.bbmiddle", orig_bb_middle);
} else if (f) {
ut64 linearsz = r_anal_function_linear_size (f);
Expand All @@ -6585,7 +6586,7 @@ static int cmd_print(void *data, const char *input) {
// r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, f->_size > 0 ? f->_size: r_anal_function_realsize (f), f->addr);
}
}
pd_result = 0;
pd_result = false;
} else {
R_LOG_ERROR ("pdf: Cannot find function at 0x%08"PFMT64x, core->offset);
processed_cmd = true;
Expand Down Expand Up @@ -6627,17 +6628,17 @@ static int cmd_print(void *data, const char *input) {
}
}
r_cons_break_pop ();
pd_result = 0;
pd_result = false;
}
break;
case 'j': // pdj
case 'j': // "pdj"
processed_cmd = true;
if (*input == 'D') {
cmd_pDj (core, input + 2);
} else {
cmd_pdj (core, input + 2, block);
}
pd_result = 0;
pd_result = false;
break;
case 'J': // pdJ
formatted_json = true;
Expand All @@ -6651,7 +6652,7 @@ static int cmd_print(void *data, const char *input) {
case '?': // "pd?"
processed_cmd = true;
r_core_cmd_help (core, help_msg_pd);
pd_result = 0;
pd_result = false;
case '.':
case '-':
case '+':
Expand Down Expand Up @@ -8369,7 +8370,7 @@ static int cmd_print(void *data, const char *input) {
if (myblock) {
free (block);
}
if (tmpseek != UT64_MAX) {
if (tmpseek != UT64_MAX && tmpseek != core->offset) {
r_core_seek (core, tmpseek, SEEK_SET);
r_core_block_read (core);
}
Expand Down
3 changes: 1 addition & 2 deletions libr/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6917,7 +6917,6 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte
R_LOG_ERROR ("Too many backward instructions");
return false;
}

if (r_core_prevop_addr (core, core->offset, nb_opcodes, &addr)) {
nbytes = old_offset - addr;
} else if (!r_core_asm_bwdis_len (core, &nbytes, &addr, nb_opcodes)) {
Expand All @@ -6940,7 +6939,7 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte
r_io_read_at (core->io, addr + count, buf + count, nb_bytes - count);
} else {
if (nb_bytes > 0) {
memset (buf, 0xff, nb_bytes);
memset (buf, core->io->Oxff, nb_bytes);
}
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions libr/util/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,6 @@ R_API void r_print_hexii(RPrint *rp, ut64 addr, const ut8 *buf, int len, int ste
/* set screen_bounds to addr if the cursor is not visible on the screen anymore.
* Note: screen_bounds is set only the first time this happens. */
R_API void r_print_set_screenbounds(RPrint *p, ut64 addr) {
int r, rc;

r_return_if_fail (p);

if (!p->screen_bounds) {
Expand All @@ -808,6 +806,7 @@ R_API void r_print_set_screenbounds(RPrint *p, ut64 addr) {
}

if (p->screen_bounds == 1) {
int r, rc;
(void)p->consbind.get_size (&r);
(void)p->consbind.get_cursor (&rc);

Expand Down

0 comments on commit 1b92c60

Please sign in to comment.