diff --git a/libr/io/p/io_debug.c b/libr/io/p/io_debug.c index eafa879878..745e227d19 100644 --- a/libr/io/p/io_debug.c +++ b/libr/io/p/io_debug.c @@ -417,6 +417,40 @@ static int fork_and_ptraceme_for_mac(RIO *io, int bits, const char *cmd) { } #endif +static char *get_and_escape_path (char *str) +{ + char *path_bin = strdup (str); + char *final = NULL; + + if (path_bin) { + char *p = r_str_lchr (str, '/'); + char *pp = r_str_tok (p, ' ', -1); + char *args; + + if (!pp) { + // There is nothing more to parse + return str; + } + + path_bin[pp - str] = '\0'; + if (strstr (path_bin, "\\ ")) { + path_bin = r_str_replace (path_bin, "\\ ", " ", true); + } + args = path_bin + (pp - str) + 1; + + char *path_bin_escaped = r_str_arg_escape (path_bin); + int len = strlen (path_bin_escaped); + + path_bin_escaped[len] = ' '; + path_bin_escaped[len + 1] = '\0'; + + final = r_str_append (path_bin_escaped, args); + free (path_bin); + } + + return final; +} + static int fork_and_ptraceme(RIO *io, int bits, const char *cmd) { #if __APPLE__ && !__POWERPC__ return fork_and_ptraceme_for_mac(io, bits, cmd); @@ -449,10 +483,14 @@ static int fork_and_ptraceme(RIO *io, int bits, const char *cmd) { char *_cmd = io->args ? r_str_appendf (strdup (cmd), " %s", io->args) : strdup (cmd); - + char *path_escaped = get_and_escape_path (_cmd); trace_me (); - argv = r_str_argv (_cmd, NULL); + argv = r_str_argv (path_escaped, NULL); + if (strstr (argv[0], "\\ ")) { + argv[0] = r_str_replace (argv[0], "\\ ", " ", true); + } if (!argv) { + free (path_escaped); free (_cmd); return -1; } @@ -469,6 +507,7 @@ static int fork_and_ptraceme(RIO *io, int bits, const char *cmd) { eprintf ("Invalid execvp\n"); } r_str_argv_free (argv); + free (path_escaped); free (_cmd); } perror ("fork_and_attach: execv");