From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id E1E203858D1E; Tue, 14 Feb 2023 16:05:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1E203858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676390737; bh=nP0l5MGu5UYxuaICGh7CfBuG6oh5msrIAfufJLVq0ow=; h=From:To:Subject:Date:From; b=Rz8DmPwcgspwNrjvrknnm4licfTONm9DipUZ0o9Wed9UpHhvv1SxEnIqGPJQzLvL7 gPkjj9KHpmIqL9zOi3+t7Mwlv/WjZDrNozJu9ynUDRLbyP60vTdzc1hf4NcF8Lvdl6 vmZ3+LeN05e6L0Lv27AxbqgQUbjE8a2Lwio/+3Jc= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Do not cast away const in agent_run_command X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 5bed9dc992a0136d403a7addb29a2ed822fd4fd2 X-Git-Newrev: 81aa19c303c94f549cb9ae343cfe4b635b4e888c Message-Id: <20230214160537.E1E203858D1E@sourceware.org> Date: Tue, 14 Feb 2023 16:05:37 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D81aa19c303c9= 4f549cb9ae343cfe4b635b4e888c commit 81aa19c303c94f549cb9ae343cfe4b635b4e888c Author: Tom Tromey Date: Tue Feb 14 07:03:11 2023 -0700 Do not cast away const in agent_run_command =20 While investigating something else, I noticed some weird code in agent_run_command (use of memcpy rather than strcpy). Then I noticed that 'cmd' is used as both an in and out parameter, despite being const. =20 Casting away const like this is bad. This patch removes the const and fixes the memcpy. I also added a static assert to assure myself that the code in gdbserver is correct -- gdbserver is passing its own buffer directly to agent_run_command. =20 Reviewed-By: Andrew Burgess Diff: --- gdb/linux-nat.c | 7 ++----- gdbserver/server.cc | 5 +++++ gdbserver/tracepoint.cc | 2 +- gdbsupport/agent.cc | 14 ++++++++------ gdbsupport/agent.h | 2 +- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 2b206a4ec1e..d6e69e61ef5 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -4114,9 +4114,7 @@ linux_nat_target::static_tracepoint_markers_by_strid = (const char *strid) /* Pause all */ target_stop (ptid); =20 - memcpy (s, "qTfSTM", sizeof ("qTfSTM")); - s[sizeof ("qTfSTM")] =3D 0; - + strcpy (s, "qTfSTM"); agent_run_command (pid, s, strlen (s) + 1); =20 /* Unpause all. */ @@ -4133,8 +4131,7 @@ linux_nat_target::static_tracepoint_markers_by_strid = (const char *strid) } while (*p++ =3D=3D ','); /* comma-separated list */ =20 - memcpy (s, "qTsSTM", sizeof ("qTsSTM")); - s[sizeof ("qTsSTM")] =3D 0; + strcpy (s, "qTsSTM"); agent_run_command (pid, s, strlen (s) + 1); p =3D s; } diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 21fb51a45d1..46dfe70838b 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -51,6 +51,11 @@ #include "gdbsupport/scoped_restore.h" #include "gdbsupport/search.h" =20 +/* PBUFSIZ must also be at least as big as IPA_CMD_BUF_SIZE, because + the client state data is passed directly to some agent + functions. */ +gdb_static_assert (PBUFSIZ >=3D IPA_CMD_BUF_SIZE); + #define require_running_or_return(BUF) \ if (!target_running ()) \ { \ diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc index 37a9a8c5b7c..b59077a3896 100644 --- a/gdbserver/tracepoint.cc +++ b/gdbserver/tracepoint.cc @@ -6820,7 +6820,7 @@ run_inferior_command (char *cmd, int len) target_pause_all (false); uninsert_all_breakpoints (); =20 - err =3D agent_run_command (pid, (const char *) cmd, len); + err =3D agent_run_command (pid, cmd, len); =20 reinsert_all_breakpoints (); target_unpause_all (false); diff --git a/gdbsupport/agent.cc b/gdbsupport/agent.cc index 531807be3d2..81c925dd99d 100644 --- a/gdbsupport/agent.cc +++ b/gdbsupport/agent.cc @@ -179,14 +179,16 @@ gdb_connect_sync_socket (int pid) #endif } =20 -/* Execute an agent command in the inferior. PID is the value of pid of t= he - inferior. CMD is the buffer for command. GDB or GDBserver will store = the - command into it and fetch the return result from CMD. The interaction - between GDB/GDBserver and the agent is synchronized by a synchronization - socket. Return zero if success, otherwise return non-zero. */ +/* Execute an agent command in the inferior. PID is the value of pid + of the inferior. CMD is the buffer for command. It is assumed to + be at least IPA_CMD_BUF_SIZE bytes long. GDB or GDBserver will + store the command into it and fetch the return result from CMD. + The interaction between GDB/GDBserver and the agent is synchronized + by a synchronization socket. Return zero if success, otherwise + return non-zero. */ =20 int -agent_run_command (int pid, const char *cmd, int len) +agent_run_command (int pid, char *cmd, int len) { int fd; int tid =3D agent_get_helper_thread_id (); diff --git a/gdbsupport/agent.h b/gdbsupport/agent.h index dceb33f6bd5..7a258e267a5 100644 --- a/gdbsupport/agent.h +++ b/gdbsupport/agent.h @@ -22,7 +22,7 @@ =20 #include "gdbsupport/preprocessor.h" =20 -int agent_run_command (int pid, const char *cmd, int len); +int agent_run_command (int pid, char *cmd, int len); =20 int agent_look_up_symbols (void *);