Skip to content

Commit

Permalink
Fix appending redirection on internal alias files ##shell
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Jun 27, 2024
1 parent e3137f6 commit daea928
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
7 changes: 2 additions & 5 deletions libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2179,7 +2179,7 @@ static int cmd_table(void *data, const char *input) {
if (*file == '$' && !file[1]) {
R_LOG_ERROR ("No alias name given");
} else if (*file == '$') {
RCmdAliasVal *file_data = r_cmd_alias_get (core->rcmd, file+1);
RCmdAliasVal *file_data = r_cmd_alias_get (core->rcmd, file + 1);
if (file_data) {
char *file_data_str = r_cmd_alias_val_strdup (file_data);
load_table (core, core->table, strdup (file_data_str));
Expand Down Expand Up @@ -4568,11 +4568,8 @@ repeat:;
ut8 *alias_data = r_buf_read_all (cmd_out, &alias_len);
const char *arg = r_str_trim_head_ro (str + 1);
if (appendResult) {
if (r_cmd_alias_append_raw (core->rcmd, arg, alias_data, alias_len)) {
if (!r_cmd_alias_append_raw (core->rcmd, arg, alias_data, alias_len)) {
R_LOG_INFO ("Alias '$%s' is a command - will not attempt to append", arg);
} else {
/* No existing alias */
r_cmd_alias_set_raw (core->rcmd, arg, alias_data, alias_len);
}
} else {
r_cmd_alias_set_raw (core->rcmd, arg, alias_data, alias_len);
Expand Down
26 changes: 12 additions & 14 deletions libr/core/cmd_api.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 */

#define R_LOG_ORIGIN "cmdapi"
#include <r_core.h>
Expand Down Expand Up @@ -366,24 +366,21 @@ R_API RCmdAliasVal *r_cmd_alias_get(RCmd *cmd, const char *k) {
}

static ut8 *alias_append_internal(int *out_szp, const RCmdAliasVal *first, const ut8 *second, int second_sz) {
ut8* out;
int out_sz;

/* If appending to a string, always overwrite the trailing \0 */
int bytes_from_first = first->is_str
const int bytes_from_first = first->is_str
? first->sz - 1
: first->sz;

out_sz = bytes_from_first + second_sz;
out = malloc (out_sz);
const int out_sz = bytes_from_first + second_sz;
ut8 *out = malloc (out_sz);
if (!out) {
return NULL;
}

/* Copy full buffer if raw bytes. Stop before \0 if string. */
memcpy (out, first->data, bytes_from_first);
/* Always copy all bytes from second, including trailing \0 */
memcpy (out+bytes_from_first, second, second_sz);
memcpy (out + bytes_from_first, second, second_sz);

if (out_sz) {
*out_szp = out_sz;
Expand All @@ -392,6 +389,7 @@ static ut8 *alias_append_internal(int *out_szp, const RCmdAliasVal *first, const
}

R_API int r_cmd_alias_append_str(RCmd *cmd, const char *k, const char *a) {
R_RETURN_VAL_IF_FAIL (cmd && k && a, 1);
RCmdAliasVal *v_old = r_cmd_alias_get (cmd, k);
if (v_old) {
if (!v_old->is_data) {
Expand All @@ -410,23 +408,23 @@ R_API int r_cmd_alias_append_str(RCmd *cmd, const char *k, const char *a) {
return 0;
}

// R2_600 - return bool instead
R_API int r_cmd_alias_append_raw(RCmd *cmd, const char *k, const ut8 *a, int sz) {
RCmdAliasVal *v_old = r_cmd_alias_get (cmd, k);
if (v_old) {
if (!v_old->is_data) {
return 1;
return 0;
}
int new_len = 0;
ut8 *new = alias_append_internal (&new_len, v_old, a, sz);
if (!new) {
return 1;
if (new) {
r_cmd_alias_set_raw (cmd, k, new, new_len);
free (new);
}
r_cmd_alias_set_raw (cmd, k, new, new_len);
free (new);
} else {
r_cmd_alias_set_raw (cmd, k, a, sz);
}
return 0;
return true;
}

/* Returns a new copy of v->data. If !v->is_str, hex escaped */
Expand Down
25 changes: 25 additions & 0 deletions test/db/cmd/cmd_pipe
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,28 @@ Disassembly of entry0:

EOF
RUN

NAME=double redirect into an alias
FILE=--
CMDS=<<EOF
echo hello > $a
echo world > $a
cat $a
EOF
EXPECT=<<EOF
world
EOF
RUN

# XXX note that theres a missing newline here
NAME=double redirect into an alias append
FILE=--
CMDS=<<EOF
echo hello > $a
echo world >> $a
cat $a
EOF
EXPECT=<<EOF
helloworld
EOF
RUN

0 comments on commit daea928

Please sign in to comment.