From 8cba363d885f4486ba67a40ef77720445f3ff5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergi=20=C3=80lvarez=20i=20Capilla?= Date: Wed, 16 Feb 2022 12:13:25 +0100 Subject: [PATCH] Minor cleanup in cmd_print --- libr/core/casm.c | 10 +++++----- libr/core/cmd_print.c | 41 ++++++++++++++++++++--------------------- libr/core/disasm.c | 5 ++--- libr/util/print.c | 3 +-- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/libr/core/casm.c b/libr/core/casm.c index 0bdef909859c4..10423b5a88772 100644 --- a/libr/core/casm.c +++ b/libr/core/casm.c @@ -485,7 +485,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) { RAsmOp op; // if (n > core->blocksize) n = core->blocksize; - ut64 at; ut32 idx = 0, hit_count; int numinstr, asmlen, ii; const int addrbytes = core->io->addrbytes; @@ -494,8 +493,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; @@ -509,7 +507,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; @@ -536,7 +534,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++) { int instrlen = r_asm_disassemble (core->rasm, &op, diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index 1ec787b14ecd5..9ee203733289b 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -4934,6 +4934,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 == '-') { ch = ' '; @@ -5618,12 +5619,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 (!sp && input[1] == '-') { sp = input + 1; } @@ -5648,12 +5646,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 1 + core->offset = at; // "pd" doesnt know about the current offset for pd -X r_core_block_read (core); +#endif 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 'd': // "pdd" // r2dec @@ -5670,13 +5671,13 @@ 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," case 't': // "pdt" r_core_disasm_table (core, l, r_str_trim_head_ro (input + 2)); - pd_result = 0; + pd_result = false; processed_cmd = true; break; case 'k': // "pdk" -print class @@ -5692,7 +5693,7 @@ static int cmd_print(void *data, const char *input) { } else { r_core_disasm_pdi (core, l, 0, 0); } - pd_result = 0; + pd_result = false; break; case 'a': // "pda" processed_cmd = true; @@ -5785,7 +5786,7 @@ static int cmd_print(void *data, const char *input) { input[2] == 'J', NULL, NULL); } free (block); - pd_result = 0; + pd_result = false; } } else { eprintf ("Cannot find function at 0x%08"PFMT64x "\n", core->offset); @@ -5862,7 +5863,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); @@ -5880,7 +5881,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 { eprintf ("pdf: Cannot find function at 0x%08"PFMT64x "\n", core->offset); processed_cmd = true; @@ -5922,17 +5923,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; @@ -5946,7 +5947,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 '+': @@ -7378,10 +7379,8 @@ static int cmd_print(void *data, const char *input) { break; } beach: - if (myblock) { - free (block); - } - if (tmpseek != UT64_MAX) { + free (block); + if (tmpseek != UT64_MAX && tmpseek != core->offset) { r_core_seek (core, tmpseek, SEEK_SET); r_core_block_read (core); } diff --git a/libr/core/disasm.c b/libr/core/disasm.c index 8a928db494f2b..29db84199f37b 100644 --- a/libr/core/disasm.c +++ b/libr/core/disasm.c @@ -6271,7 +6271,6 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte eprintf ("Too many backward instructions\n"); 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)) { @@ -6291,10 +6290,10 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte count = R_MIN (nb_bytes, nbytes); if (count > 0) { r_io_read_at (core->io, addr, buf, count); - r_io_read_at (core->io, addr+count, buf+count, nb_bytes-count); + 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 { diff --git a/libr/util/print.c b/libr/util/print.c index 205f74111b26d..3055ec3ee8288 100644 --- a/libr/util/print.c +++ b/libr/util/print.c @@ -790,8 +790,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) { @@ -805,6 +803,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);