Skip to content

Commit

Permalink
Fix wt, aht help messages and early catch badly formatted help messag…
Browse files Browse the repository at this point in the history
…es ##shell
  • Loading branch information
radare authored and trufae committed Jun 5, 2024
1 parent d3afaa4 commit 8f3644d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 127 deletions.
10 changes: 5 additions & 5 deletions libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -512,7 +513,6 @@ static void recursive_help(RCore *core, int detail, const char *cmd_prefix) {
key_found = true;
}
}

if (!key_found) {
free (k);
}
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cmd_anal.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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][?] <type>", "mark immediate as a type offset (deprecated, moved to \"aho\")",
"aht", "[?][s] <type>", "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
};
Expand Down
240 changes: 120 additions & 120 deletions libr/core/cmd_write.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libr/util/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8f3644d

Please sign in to comment.