Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix append #23075

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 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 @@ -4534,7 +4534,7 @@ repeat:;
}

char *nextgt = strchr (r_str_trim_head_ro (ptr + 1), '>');
if (nextgt) {
if (nextgt && nextgt[0] != '>') {
char *back = ptr + 1;
while (nextgt > back) {
if (!isdigit (*nextgt) && *nextgt != 'H') {
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 All @@ -4588,6 +4585,7 @@ repeat:;
// R_LOG_ERROR ("Cannot open pipe with fd %d", fdn);
// goto errorout;
}
*str = 0;
if (next_redirect) {
ptr = next_redirect;
*next_redirect = ' ';
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
2 changes: 1 addition & 1 deletion libr/util/token.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static void indent(RTokenizer *tok) {
if (data->incase) {
n++;
}
// R_LOG_DEBUG ("%s", r_str_pad (' ', n));
R_LOG_DEBUG ("%s", r_str_pad (' ', n));
}

bool callback(RTokenizer *tok) {
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
Loading