From 8f3644d6ff2c9815a5c99316ef547c07e8e4d3ef Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 5 Jun 2024 18:29:44 +0200 Subject: [PATCH] Fix wt, aht help messages and early catch badly formatted help messages ##shell --- libr/core/cmd.c | 10 +- libr/core/cmd_anal.inc.c | 2 +- libr/core/cmd_write.inc.c | 240 +++++++++++++++++++------------------- libr/util/sys.c | 2 +- 4 files changed, 127 insertions(+), 127 deletions(-) diff --git a/libr/core/cmd.c b/libr/core/cmd.c index b928239e2d9de..dfcf652dbb040 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -479,12 +479,13 @@ static void recursive_help(RCore *core, int detail, const char *cmd_prefix) { recursive_help (core, detail, "??"); recursive_help (core, detail, "~"); } - - char *s = r_core_cmd_strf (core, "%s?", cmd_prefix); - if (!s) { + if (strchr (cmd_prefix, '[')) { + R_LOG_WARN ("Invalid char in command, help message must be fixed: %s", cmd_prefix); return; } - if (!*s) { + R_LOG_DEBUG ("[recursive help] %s", cmd_prefix); + char *s = r_core_cmd_strf (core, "%s?", cmd_prefix); + if (R_STR_ISEMPTY (s)) { free (s); return; } @@ -512,7 +513,6 @@ static void recursive_help(RCore *core, int detail, const char *cmd_prefix) { key_found = true; } } - if (!key_found) { free (k); } diff --git a/libr/core/cmd_anal.inc.c b/libr/core/cmd_anal.inc.c index dfecf618c61de..69d26958a71f2 100644 --- a/libr/core/cmd_anal.inc.c +++ b/libr/core/cmd_anal.inc.c @@ -875,7 +875,7 @@ static RCoreHelpMessage help_msg_ah = { "ahr", " val", "set hint for return value of a function", "ahs", " 4", "set opcode size=4", "ahS", " jz", "set asm.syntax=jz for this opcode", - "aht", "[s][?] ", "mark immediate as a type offset (deprecated, moved to \"aho\")", + "aht", "[?][s] ", "mark immediate as a type offset (deprecated, moved to \"aho\")", "ahv", " val", "change opcode's val field (useful to set jmptbl sizes in jmp rax)", NULL }; diff --git a/libr/core/cmd_write.inc.c b/libr/core/cmd_write.inc.c index 67085e065023b..3603119396f88 100644 --- a/libr/core/cmd_write.inc.c +++ b/libr/core/cmd_write.inc.c @@ -27,7 +27,7 @@ static RCoreHelpMessage help_msg_w = { "wp", "[?] -|file", "apply radare patch file. See wp? fmi", "wr", " 10", "write 10 random bytes", "ws", "[?] pstring", "write pascal string: 1 byte for length + N for the string", - "wt", "[afs][?] [filename] [size]", "write to file (from current seek, blocksize or sz bytes)", + "wt", "[?][afs] [filename] [size]", "write to file (from current seek, blocksize or sz bytes)", "ww", " foobar", "write wide string 'f\\x00o\\x00o\\x00b\\x00a\\x00r\\x00'", "wx", "[?][fs] 9090", "write two intel nops (from wxfile or wxseek)", "wX", " 1b2c3d", "fill current block with cyclic hexpairs", @@ -1653,157 +1653,157 @@ static int cmd_wt(RCore *core, const char *input) { input++; switch (*input) { - case 's': { // "wts" - ut64 addr = 0; - char *host_port; - R_BORROW char *host; - R_BORROW char *port; - ut8 *buf; - RSocket *sock; - - if (argc < 2) { - r_core_cmd_help_match (core, help_msg_wt, "wts"); - ret = 1; - goto leave; - } + case 's': { // "wts" + ut64 addr = 0; + char *host_port; + R_BORROW char *host; + R_BORROW char *port; + ut8 *buf; + RSocket *sock; + + if (argc < 2) { + r_core_cmd_help_match (core, help_msg_wt, "wts"); + ret = 1; + goto leave; + } - sz = r_io_size (core->io); - if (sz < 0) { - R_LOG_ERROR ("Unknown file size"); - ret = 1; - goto leave; - } + sz = r_io_size (core->io); + if (sz < 0) { + R_LOG_ERROR ("Unknown file size"); + ret = 1; + goto leave; + } - host_port = strdup (argv[1]); + host_port = strdup (argv[1]); - host = host_port; - port = strchr (host_port, ':'); - if (!port) { - r_core_cmd_help_match (core, help_msg_wt, "wts"); + host = host_port; + port = strchr (host_port, ':'); + if (!port) { + r_core_cmd_help_match (core, help_msg_wt, "wts"); + free (host_port); + ret = 1; + goto leave; + } + + *port++ = 0; + + if (argc > 2) { + sz = r_num_math (core->num, argv[2]); + if (sz < 0) { + R_LOG_ERROR ("%s is not a valid size", argv[2]); free (host_port); ret = 1; goto leave; } + addr = core->offset; + } - *port++ = 0; + buf = malloc (sz); + r_io_read_at (core->io, addr, buf, sz); - if (argc > 2) { - sz = r_num_math (core->num, argv[2]); - if (sz < 0) { - R_LOG_ERROR ("%s is not a valid size", argv[2]); - free (host_port); + sock = r_socket_new (false); + if (r_socket_connect (sock, host, port, R_SOCKET_PROTO_TCP, 0)) { + ut64 sent = 0; + R_LOG_INFO ("Connection created. Sending data to TCP socket"); + while (sent < sz) { + bool sockret = r_socket_write (sock, buf + sent, sz - sent); + if (!sockret) { + R_LOG_ERROR ("Socket write error"); ret = 1; - goto leave; + break; } - addr = core->offset; } + } else { + R_LOG_ERROR ("Connection to %s failed", host_port); + ret = 1; + } - buf = malloc (sz); - r_io_read_at (core->io, addr, buf, sz); - - sock = r_socket_new (false); - if (r_socket_connect (sock, host, port, R_SOCKET_PROTO_TCP, 0)) { - ut64 sent = 0; - R_LOG_INFO ("Connection created. Sending data to TCP socket"); - while (sent < sz) { - bool sockret = r_socket_write (sock, buf + sent, sz - sent); - if (!sockret) { - R_LOG_ERROR ("Socket write error"); - ret = 1; - break; - } - } - } else { - R_LOG_ERROR ("Connection to %s failed", host_port); + free (host_port); + free (buf); + r_socket_free (sock); + goto leave; + } + case 'f': // "wtf" + switch (input[1]) { + case '\0': + case '?': // "wtf?" + r_core_cmd_help_match (core, help_msg_wt, "wtf"); + ret = 1; + goto leave; + case '!': { // "wtf!" + RIOMap *map; + if (input[2] == '?') { + r_core_cmd_help_match (core, help_msg_wt, "wtf!"); ret = 1; + goto leave; } - free (host_port); - free (buf); - r_socket_free (sock); - goto leave; + map = r_io_map_get_at (core->io, poff); + if (map) { + // convert vaddr to paddr + poff = poff - r_io_map_begin (map) + map->delta; + } + + sz = r_io_fd_size (core->io, core->io->desc->fd) - core->offset; + + // ignore given size + if (argc > 2) { + argc = 2; + } + break; } - case 'f': // "wtf" - switch (input[1]) { - case '\0': - case '?': // "wtf?" - r_core_cmd_help_match (core, help_msg_wt, "wtf"); + case 'f': // "wtff" + if (input[2] == '?') { + r_core_cmd_help_match (core, help_msg_wt, "wtff"); ret = 1; goto leave; - case '!': { // "wtf!" - RIOMap *map; - if (input[2] == '?') { - r_core_cmd_help_match (core, help_msg_wt, "wtf!"); - ret = 1; - goto leave; - } - - map = r_io_map_get_at (core->io, poff); - if (map) { - // convert vaddr to paddr - poff = poff - r_io_map_begin (map) + map->delta; - } - - sz = r_io_fd_size (core->io, core->io->desc->fd) - core->offset; + } - // ignore given size - if (argc > 2) { - argc = 2; - } - break; + if (argc > 1) { + prefix = argv[1]; } - case 'f': // "wtff" - if (input[2] == '?') { - r_core_cmd_help_match (core, help_msg_wt, "wtff"); - ret = 1; - goto leave; - } - if (argc > 1) { - prefix = argv[1]; - } + default_filename_sep = '-'; + break; + default: // "wtf" + if (input[2] == '?') { + r_core_cmd_help_match (core, help_msg_wt, "wtf"); + ret = 1; + goto leave; + } - default_filename_sep = '-'; - break; - default: // "wtf" - if (input[2] == '?') { - r_core_cmd_help_match (core, help_msg_wt, "wtf"); + if (r_str_startswith (filename, "base64:")) { + const char *encoded = filename + 7; + int len; + if (strlen (encoded) > 31) { + R_LOG_ERROR ("Base64 blob must be fewer than 32 characters"); ret = 1; goto leave; } - if (r_str_startswith (filename, "base64:")) { - const char *encoded = filename + 7; - int len; - if (strlen (encoded) > 31) { - R_LOG_ERROR ("Base64 blob must be fewer than 32 characters"); - ret = 1; - goto leave; - } + len = r_base64_decode ((ut8 *)fn_local, encoded, -1); - len = r_base64_decode ((ut8 *)fn_local, encoded, -1); + filename = fn_local; - filename = fn_local; - - if (len < 0) { - R_LOG_ERROR ("Couldn't decode b64 filename"); - ret = 1; - goto leave; - } + if (len < 0) { + R_LOG_ERROR ("Couldn't decode b64 filename"); + ret = 1; + goto leave; } - break; } break; - case 'a': - append = true; - break; - case '\0': // "wt" - case ' ': // "wt " - break; - case '?': // "wt?" - default: - r_core_cmd_help (core, help_msg_wt); - goto leave; + } + break; + case 'a': + append = true; + break; + case '\0': // "wt" + case ' ': // "wt " + break; + case '?': // "wt?" + default: + r_core_cmd_help (core, help_msg_wt); + goto leave; } // default filename is prefix.addr diff --git a/libr/util/sys.c b/libr/util/sys.c index 70029b05bbc8e..de5e46bbcfa90 100644 --- a/libr/util/sys.c +++ b/libr/util/sys.c @@ -834,7 +834,7 @@ R_API int r_sys_cmdf(const char *fmt, ...) { int ret; char cmd[4096]; va_list ap; - va_start(ap, fmt); + va_start (ap, fmt); vsnprintf (cmd, sizeof (cmd), fmt, ap); ret = r_sys_cmd (cmd); va_end (ap);