Skip to content

Commit

Permalink
Redirect stderr to stdout via -1; Unbreak dk 9 test (#4664)
Browse files Browse the repository at this point in the history
* Redirect stderr to stdout via `-1`; Unbreak `dk 9` test
* Use fully qualified names in 'overgeneral-exceptions' option
  • Loading branch information
kazarmy authored Oct 11, 2024
1 parent b96f355 commit 5ee08f7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -506,5 +506,5 @@ preferred-modules=

# Exceptions that will emit a warning when being caught. Defaults to
# "BaseException, Exception".
overgeneral-exceptions=BaseException,
Exception
overgeneral-exceptions=builtins.BaseException,
builtins.Exception
13 changes: 12 additions & 1 deletion librz/main/rizin.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ static int main_help(int line) {
"- ", "", "Read file from stdin",
"-=", "", "Perform R=! command to run all commands remotely",
"-0", "", "Print \\x00 after init and every command",
"-1", "", "Redirect stderr to stdout",
"-2", "", "Close stderr file descriptor (silent warning messages)",
"-a", "[arch]", "Set asm.arch",
"-A", "", "Run 'aaa' command to analyze all referenced code",
Expand Down Expand Up @@ -469,6 +470,7 @@ RZ_API int rz_main_rizin(int argc, const char **argv) {
rz_list_free(prefiles); \
}

bool stderr2stdout = false;
bool noStderr = false;

#ifdef __UNIX
Expand Down Expand Up @@ -534,7 +536,7 @@ RZ_API int rz_main_rizin(int argc, const char **argv) {
char *debugbackend = rz_str_dup("native");

RzGetopt opt;
rz_getopt_init(&opt, argc, argv, "=02AMCwxfF:H:hm:e:nk:NdqQs:p:b:B:a:Lui:I:l:R:r:c:D:vVSTzuXt");
rz_getopt_init(&opt, argc, argv, "=012AMCwxfF:H:hm:e:nk:NdqQs:p:b:B:a:Lui:I:l:R:r:c:D:vVSTzuXt");
while (argc >= 2 && (c = rz_getopt_next(&opt)) != -1) {
switch (c) {
case '-':
Expand All @@ -546,6 +548,9 @@ RZ_API int rz_main_rizin(int argc, const char **argv) {
RZ_FREE(r->cmdremote);
r->cmdremote = rz_str_dup("");
break;
case '1':
stderr2stdout = true;
break;
case '2':
noStderr = true;
break;
Expand Down Expand Up @@ -743,6 +748,12 @@ RZ_API int rz_main_rizin(int argc, const char **argv) {
help++;
}
}
if (stderr2stdout && -1 == dup2(1, 2)) {
RZ_LOG_ERROR("stderr2stdout: Failed to dup2 stderr\n");
LISTS_FREE();
RZ_FREE(debugbackend);
return 1;
}
if (noStderr) {
if (-1 == close(2)) {
RZ_LOG_ERROR("Failed to close stderr\n");
Expand Down
10 changes: 4 additions & 6 deletions test/db/archos/linux-x64/dbg
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ EOF
RUN

NAME=dk 9
BROKEN=1
FILE=bins/elf/analysis/calls_x64
ARGS=-a x86 -d
FILE=--
CMDS=<<EOF
dk 9
dc # binary may or may not have been terminated already
%= `env~?^ASAN_LD_PRELOAD=`
%+ env LD_PRELOAD=`env ASAN_LD_PRELOAD`
!python3 scripts/dk_9.py
EOF
EXPECT=<<EOF
EOF
REGEXP_FILTER_ERR=child.*signal [0-9]+.*
EXPECT_ERR=<<EOF
child received signal 9
EOF
Expand Down
12 changes: 12 additions & 0 deletions test/scripts/dk_9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys
import rzpipe

rzp = rzpipe.open("bins/elf/analysis/calls_x64", flags=["-a", "x86", "-d", "-1"])
expected = "child received signal 9"
actual = rzp.cmd("")
actual += rzp.cmd("wx ebfe; dk 9; dc") # ebfe is infinite loop
actual += rzp.cmd("")
if expected in actual or "Process exited with status=0x9" in actual:
sys.stderr.write(expected + "\n")
else:
sys.stderr.write(actual)

0 comments on commit 5ee08f7

Please sign in to comment.