diff options
author | Andrew Burgess <aburgess@redhat.com> | 2023-09-27 17:18:01 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2023-10-06 13:02:36 +0100 |
commit | 7663126c0b8246754990ce57a2c8432c2d0e4cc9 (patch) | |
tree | 6fd62e0c6bbf1e4797f67e18fd71f0490db1789f /gdbserver | |
parent | f1f0a06d5b34231edd75fbd71a3be79097437f62 (diff) | |
download | gdb-7663126c0b8246754990ce57a2c8432c2d0e4cc9.zip gdb-7663126c0b8246754990ce57a2c8432c2d0e4cc9.tar.gz gdb-7663126c0b8246754990ce57a2c8432c2d0e4cc9.tar.bz2 |
gdbserver: fix handling of trailing empty argument
When I posted the previous patch for review Andreas Schwab pointed out
that passing a trailing empty argument also doesn't work.
The fix for this is in the same area of code as the previous patch,
but is sufficiently different that I felt it deserved a patch of its
own.
I noticed that passing arguments containing single quotes to gdbserver
didn't work correctly:
gdb -ex 'set sysroot' --args /tmp/show-args
Reading symbols from /tmp/show-args...
(gdb) target extended-remote | gdbserver --once --multi - /tmp/show-args
Remote debugging using | gdbserver --once --multi - /tmp/show-args
stdin/stdout redirected
Process /tmp/show-args created; pid = 176054
Remote debugging using stdio
Reading symbols from /lib64/ld-linux-x86-64.so.2...
(No debugging symbols found in /lib64/ld-linux-x86-64.so.2)
0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2
(gdb) set args abc ""
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /tmp/show-args \'
stdin/stdout redirected
Process /tmp/show-args created; pid = 176088
2 args are:
/tmp/show-args
abc
Done.
[Inferior 1 (process 176088) exited normally]
(gdb) target native
Done. Use the "run" command to start a process.
(gdb) run
Starting program: /tmp/show-args \'
2 args are:
/tmp/show-args
abc
Done.
[Inferior 1 (process 176095) exited normally]
(gdb) q
The 'shows-args' program used here just prints the arguments passed to
the inferior.
Notice that when starting the inferior using the extended-remote
target there is only a single argument 'abc', while when using the
native target there is a second argument, the blank line, representing
the empty argument.
The problem here is that the vRun packet coming from GDB looks like
this (I've removing the trailing checksum):
$vRun;PROGRAM_NAME;616263;
If we compare this to a packet with only a single argument and no
trailing empty argument:
$vRun;PROGRAM_NAME;616263
Notice the lack of the trailing ';' character here.
The problem is that gdbserver processes this string in a loop. At
each point we maintain a pointer to the character just after a ';',
and then we process everything up to either the next ';' character, or
to the end of the string.
We break out of this loop when the character we start with (in that
loop iteration) is the null-character. This means in the trailing
empty argument case, we abort the loop before doing anything with the
empty argument.
In this commit I've updated the loop, we now break out using a 'break'
statement at the end of the loop if the (sub-)string we just processed
was empty, with this change we now notice the trailing empty
argument.
I've updated the test case to cover this issue.
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdbserver')
-rw-r--r-- | gdbserver/server.cc | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 496b9be..d78eb5a 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -2969,7 +2969,9 @@ handle_v_run (char *own_buf) char *new_program_name = NULL; int i; - for (i = 0, p = own_buf + strlen ("vRun;"); *p; p = next_p, ++i) + for (i = 0, p = own_buf + strlen ("vRun;"); + /* Exit condition is at the end of the loop. */; + p = next_p + 1, ++i) { next_p = strchr (p, ';'); if (next_p == NULL) @@ -3032,8 +3034,8 @@ handle_v_run (char *own_buf) new_argv.push_back (full_arg); xfree (arg); } - if (*next_p) - next_p++; + if (*next_p == '\0') + break; } if (new_program_name == NULL) |