[binutils-gdb] Fix ptid_t selftest crash

Tom Tromey via Gdb-cvs <[email protected]> Thu, 2 Jul 2026 13:01:04 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c6ed245a1999a1b424d71ca3d3f72ecab639f9f9

commit c6ed245a1999a1b424d71ca3d3f72ecab639f9f9
Author: Tom Tromey <[email protected]>
Date:   Thu Jul 2 06:51:36 2026 -0600

    Fix ptid_t selftest crash
    
    Sam James pointed out that my recent patch to add ptid_t::parse caused
    a selftest crash.
    
    At first I couldn't see why this did not crash for me, but then I
    realized that ptid_t::parse didn't unconditionally initialize '*obuf'.
    Initializing it to nullptr made this crash reliably.
    
    The underlying bug is that some code paths in ptid_t::parse don't set
    obuf on return.  This patch fixes the bug by passing 'obuf' down to
    hex_or_minus_one.
    
    Since this is straightforward and fixes a new crash, I'm checking it
    in.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34340

Diff:
---
 gdb/unittests/ptid-selftests.c | 2 +-
 gdbsupport/ptid.cc             | 5 +----
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/gdb/unittests/ptid-selftests.c b/gdb/unittests/ptid-selftests.c
index c02401db952..b52c98cdc54 100644
--- a/gdb/unittests/ptid-selftests.c
+++ b/gdb/unittests/ptid-selftests.c
@@ -154,7 +154,7 @@ static_assert (!ptid_t (2, 2, 2).matches (both),
 static ptid_t
 parse_one (const char *str, bool for_remote)
 {
-  const char *out;
+  const char *out = nullptr;
   ptid_t result = ptid_t::parse (str, &out, for_remote,
     [] ()
       {
diff --git a/gdbsupport/ptid.cc b/gdbsupport/ptid.cc
index 731a5bc3da3..5de1da3ba44 100644
--- a/gdbsupport/ptid.cc
+++ b/gdbsupport/ptid.cc
@@ -119,7 +119,7 @@ ptid_t::parse (const char *buf, const char **obuf, bool for_remote,
     }
 
   /* No multi-process.  Just a thread id.  */
-  hex = hex_or_minus_one (p, &pp, for_remote);
+  hex = hex_or_minus_one (p, obuf, for_remote);
 
   /* Handle special thread ids.  */
   if (hex == (ULONGEST) -1)
@@ -134,8 +134,5 @@ ptid_t::parse (const char *buf, const char **obuf, bool for_remote,
 
   pid = default_pid ();
 
-  if (obuf != nullptr)
-    *obuf = pp;
-
   return ptid_t (pid, lwp);
 }