diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index afbee0625a4..937ae202af1 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -7,7 +7,6 @@ #include #include #include -#include #include "../core_private.h" #include "rz_util/rz_strbuf.h" @@ -4447,8 +4446,12 @@ RZ_IPI RzCmdStatus rz_cmd_disassemble_recursively_no_function_handler(RzCore *co } RZ_IPI RzCmdStatus rz_cmd_disassemble_summarize_n_bytes_handler(RzCore *core, int argc, const char **argv) { - ut64 n_bytes = argc > 1 ? rz_num_math(core->num, argv[1]) : 0; + if (argc <= 1) { + RZ_LOG_ERROR("Invalid argument."); + return RZ_CMD_STATUS_ERROR; + } + ut64 n_bytes = rz_num_math(core->num, argv[1]); // small patch to reuse rz_core_print_disasm_strings which // needs to be rewritten entirely char *string = rz_core_print_disasm_strings(core, argc > 1 ? RZ_CORE_DISASM_STRINGS_MODE_BYTES : RZ_CORE_DISASM_STRINGS_MODE_INST, n_bytes, NULL); diff --git a/librz/core/cprint.c b/librz/core/cprint.c index eebceeb5db7..c5af25590c3 100644 --- a/librz/core/cprint.c +++ b/librz/core/cprint.c @@ -540,22 +540,43 @@ static void core_handle_call(RzCore *core, char *line, char **str) { } /** - * \brief Get the console output of disassembling \p byte_len bytes at \p addr + * \brief Get the console output of disassembling \p byte_len bytes + * or \p inst_len opcodes at \p addr. Restricted by \p byte_len + * and \p inst_len at the same time. Set one of them to zero to + * ignore its restriction. */ -static char *cons_dis_n_bytes(RzCore *core, ut64 addr, ut32 byte_len) { +static char *cons_disassembly(RzCore *core, ut64 addr, ut32 byte_len, ut32 inst_len) { + rz_return_val_if_fail(core && (byte_len || inst_len), NULL); + + bool cbytes; + + if (byte_len == 0) { + cbytes = false; + byte_len = inst_len; + } + + if (inst_len == 0) { + cbytes = true; + } + ut8 *block = malloc(byte_len + 1); if (!block) { RZ_LOG_ERROR("Cannot allocate buffer\n"); return NULL; } - rz_io_read_at(core->io, addr, block, byte_len); + if (rz_io_nread_at(core->io, addr, block, byte_len) == -1) { + RZ_LOG_ERROR("Fail to read from 0x%" PFMT64x ".", addr); + free(block); + return NULL; + } + RzCoreDisasmOptions disasm_options = { - .cbytes = true, + .cbytes = cbytes, }; rz_cons_push(); - rz_core_print_disasm(core, addr, block, byte_len, 9999, NULL, &disasm_options); + rz_core_print_disasm(core, addr, block, byte_len, inst_len, NULL, &disasm_options); rz_cons_filter(); const char *cons_str = rz_str_get(rz_cons_get_buffer()); char *ret = strdup(cons_str); @@ -616,7 +637,7 @@ RZ_API RZ_OWN char *rz_core_print_disasm_strings(RZ_NONNULL RzCore *core, RzCore case RZ_CORE_DISASM_STRINGS_MODE_BLOCK: { RzAnalysisBlock *bb = rz_analysis_find_most_relevant_block_in(core->analysis, core->offset); if (bb) { - dump_string = cons_dis_n_bytes(core, bb->addr, bb->size); + dump_string = cons_disassembly(core, bb->addr, bb->size, 0); if (!dump_string) { goto restore_conf; } @@ -637,12 +658,15 @@ RZ_API RZ_OWN char *rz_core_print_disasm_strings(RZ_NONNULL RzCore *core, RzCore break; } case RZ_CORE_DISASM_STRINGS_MODE_INST: { - dump_string = rz_core_cmd_strf(core, "pd %d", core->blocksize); + dump_string = cons_disassembly(core, core->offset, 0, core->blocksize); + if (!dump_string) { + goto restore_conf; + } break; } case RZ_CORE_DISASM_STRINGS_MODE_BYTES: default: { - dump_string = cons_dis_n_bytes(core, core->offset, n_bytes); + dump_string = cons_disassembly(core, core->offset, n_bytes, 0); if (!dump_string) { goto restore_conf; } diff --git a/test/db/cmd/cmd_pd b/test/db/cmd/cmd_pd index c0d5ad21a38..145dab656d1 100644 --- a/test/db/cmd/cmd_pd +++ b/test/db/cmd/cmd_pd @@ -642,7 +642,7 @@ s sym.func.100004401 af afn newname s entry0 -pds +pds 0x420 EOF EXPECT=<