Skip to content

Commit

Permalink
Fix #16210 - Show error message and update help for we ##io (#16427)
Browse files Browse the repository at this point in the history
* Fix wen command for io.va=true, add two tests
  • Loading branch information
radare authored Apr 8, 2020
1 parent 72d1021 commit a52506a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 21 deletions.
21 changes: 13 additions & 8 deletions libr/core/cio.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,19 +421,24 @@ R_API bool r_core_write_at(RCore *core, ut64 addr, const ut8 *buf, int size) {
return ret;
}

R_API int r_core_extend_at(RCore *core, ut64 addr, int size) {
int ret;
R_API bool r_core_extend_at(RCore *core, ut64 addr, int size) {
if (!core->io || !core->file || size < 1) {
return false;
}
ret = r_io_use_fd (core->io, core->file->fd);
if (ret != -1) {
ret = r_io_extend_at (core->io, addr, size);
if (addr >= core->offset && addr <= core->offset+core->blocksize) {
r_core_block_read (core);
int io_va = r_config_get_i (core->config, "io.va");
if (io_va) {
RIOMap *map = r_io_map_get (core->io, core->offset);
if (map) {
addr = addr - map->itv.addr + map->delta;
}
r_config_set_i (core->config, "io.va", false);
}
return ret != -1;
int ret = r_io_extend_at (core->io, addr, size);
if (addr >= core->offset && addr <= core->offset+core->blocksize) {
r_core_block_read (core);
}
r_config_set_i (core->config, "io.va", io_va);
return ret;
}

R_API int r_core_shift_block(RCore *core, ut64 addr, ut64 b_size, st64 dist) {
Expand Down
38 changes: 26 additions & 12 deletions libr/core/cmd_write.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2019 - pancake */
/* radare - LGPL - Copyright 2009-2020 - pancake */

#include "r_crypto.h"
#include "r_config.h"
Expand Down Expand Up @@ -80,12 +80,12 @@ static const char *help_msg_wc[] = {
};

static const char *help_msg_we[] = {
"Usage", "", "write extend",
"wen", " <num>", "insert num null bytes at current offset",
"weN", " <addr> <len>", "insert bytes at address",
"Usage", "", "write extend # resize the file",
"wen", " <num>", "extend the underlying file inserting NUM null bytes at current offset",
"weN", " <addr> <len>", "extend current file and insert bytes at address",
"wes", " <addr> <dist> <block_size>", "shift a blocksize left or write in the editor",
"wex", " <hex_bytes>", "insert bytes at current offset",
"weX", " <addr> <hex_bytes>", "insert bytes at address",
"wex", " <hex_bytes>", "insert bytes at current offset by extending the file",
"weX", " <addr> <hex_bytes>", "insert bytes at address by extending the file",
NULL
};

Expand Down Expand Up @@ -944,13 +944,21 @@ static int cmd_write(void *data, const char *input) {
switch (input[1]) {
case 'n': // "wen"
if (input[2] == ' ') {
len = *input ? r_num_math (core->num, input+3) : 0;
len = *input ? r_num_math (core->num, input + 3) : 0;
if (len > 0) {
const ut64 cur_off = core->offset;
cmd_suc = r_core_extend_at (core, core->offset, len);
core->offset = cur_off;
r_core_block_read (core);
if (cmd_suc) {
core->offset = cur_off;
r_core_block_read (core);
} else {
eprintf ("r_io_extend failed\n");
cmd_suc = true;
}
}
} else {
eprintf ("Usage: wen [len]\n");
cmd_suc = true;
}
break;
case 'N': // "weN"
Expand All @@ -964,9 +972,13 @@ static int cmd_write(void *data, const char *input) {
if (len > 0){
ut64 cur_off = core->offset;
cmd_suc = r_core_extend_at (core, addr, len);
r_core_seek (core, cur_off, 1);
core->offset = addr;
r_core_block_read (core);
if (cmd_suc) {
r_core_seek (core, cur_off, 1);
core->offset = addr;
r_core_block_read (core);
} else {
eprintf ("r_io_extend failed\n");
}
}
cmd_suc = true;
}
Expand Down Expand Up @@ -1035,6 +1047,8 @@ static int cmd_write(void *data, const char *input) {
if (!r_core_write_at (core, addr, bytes, len)) {
cmd_write_fail (core);
}
} else {
eprintf ("r_io_extend failed\n");
}
core->offset = addr;
r_core_block_read (core);
Expand Down
2 changes: 1 addition & 1 deletion libr/include/r_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ R_API int r_core_file_list(RCore *core, int mode);
R_API int r_core_file_binlist(RCore *core);
R_API bool r_core_file_bin_raise(RCore *core, ut32 num);
R_API int r_core_seek_delta(RCore *core, st64 addr);
R_API int r_core_extend_at(RCore *core, ut64 addr, int size);
R_API bool r_core_extend_at(RCore *core, ut64 addr, int size);
R_API bool r_core_write_at(RCore *core, ut64 addr, const ut8 *buf, int size);
R_API int r_core_write_op(RCore *core, const char *arg, char op);
R_API ut8* r_core_transform_op(RCore *core, const char *arg, char op);
Expand Down
50 changes: 50 additions & 0 deletions test/new/db/cmd/write
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
NAME=wen 3
FILE=malloc://100
CMDS=<<EOF
r
wen 3
r
EOF
EXPECT=<<EOF
100
103
EOF
RUN

NAME=wen 3 writable bin
FILE=--
CMDS=<<EOF
mkdir .tmp
cp ../bins/mach0/mac-ls2 .tmp/ls-wen
o .tmp/ls-wen
r
wen 3
r
oo+
r
wen 3
r
rm .tmp/ls-wen
EOF
EXPECT=<<EOF
38704
38704
38704
38707
EOF
RUN

NAME=wen 3 in readonly bin
FILE=../bins/mach0/mac-ls2
CMDS=<<EOF
# cannot resize in va and non-write mode
r
wen 3
r
EOF
EXPECT=<<EOF
38704
38704
EOF
RUN

NAME=quoted comments bug
FILE=malloc://1024
CMDS=<<EOF
Expand Down

0 comments on commit a52506a

Please sign in to comment.