Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Do not write negative PID or TID in remote protocol
Date: Wed, 11 Mar 2026 14:21:52 -0600	[thread overview]
Message-ID: <20260311202152.2704410-1-tromey@adacore.com> (raw)

Currently both gdb and gdbserver can write a negative number for the
PID or TID.  However, the only negative value that really makes sense
is the special case of "-1" -- in other cases if the PID or TID has
the high bit set, it should still be written as a positive number.

This patch attempts to fix the bug.  I am not really sure how to test
it.

As an aside there seems to be more low-level RSP code that could be
shared here.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25111
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33979
---
 gdb/remote.c              | 19 ++++++++-----------
 gdbserver/remote-utils.cc | 12 ++++++------
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index 88b06e688bc..54aeaba0ce5 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -3684,22 +3684,19 @@ static int remote_newthread_step (threadref *ref, void *context);
 char *
 remote_target::write_ptid (char *buf, const char *endbuf, ptid_t ptid)
 {
-  ptid_t::pid_type pid;
-  ptid_t::lwp_type lwp;
-
   if (m_features.remote_multi_process_p ())
     {
-      pid = ptid.pid ();
-      if (pid < 0)
-	buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
+      ptid_t::pid_type pid = ptid.pid ();
+      if (pid == -1)
+	buf += xsnprintf (buf, endbuf - buf, "p-1.");
       else
-	buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
+	buf += xsnprintf (buf, endbuf - buf, "p%x.", (unsigned) pid);
     }
-  lwp = ptid.lwp ();
-  if (lwp < 0)
-    buf += xsnprintf (buf, endbuf - buf, "-%lx", -lwp);
+  ptid_t::lwp_type lwp = ptid.lwp ();
+  if (lwp == -1)
+    buf += xsnprintf (buf, endbuf - buf, "-1");
   else
-    buf += xsnprintf (buf, endbuf - buf, "%lx", lwp);
+    buf += xsnprintf (buf, endbuf - buf, "%lx", (unsigned long) lwp);
 
   return buf;
 }
diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc
index 7671206ebc1..a0ae04bd632 100644
--- a/gdbserver/remote-utils.cc
+++ b/gdbserver/remote-utils.cc
@@ -525,16 +525,16 @@ write_ptid (char *buf, ptid_t ptid)
   if (cs.multi_process)
     {
       pid = ptid.pid ();
-      if (pid < 0)
-	buf += sprintf (buf, "p-%x.", -pid);
+      if (pid == -1)
+	buf += sprintf (buf, "p-1.");
       else
-	buf += sprintf (buf, "p%x.", pid);
+	buf += sprintf (buf, "p%x.", (unsigned) pid);
     }
   lwp = ptid.lwp ();
-  if (lwp < 0)
-    buf += sprintf (buf, "-%lx", -lwp);
+  if (lwp == -1)
+    buf += sprintf (buf, "-1");
   else
-    buf += sprintf (buf, "%lx", lwp);
+    buf += sprintf (buf, "%lx", (unsigned long) lwp);
 
   return buf;
 }

base-commit: b18149c8e933d8539b8a49c5d200b1ace513e339
-- 
2.53.0


             reply	other threads:[~2026-03-11 20:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-11 20:21 Tom Tromey [this message]
2026-03-12 19:42 ` Kevin Buettner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260311202152.2704410-1-tromey@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox