]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix two bugs in gdbserver thread name handling
authorTom Tromey <tromey@adacore.com>
Fri, 15 Dec 2023 14:56:45 +0000 (07:56 -0700)
committerTom Tromey <tromey@adacore.com>
Tue, 9 Jan 2024 14:07:41 +0000 (07:07 -0700)
Simon pointed out that my earlier patch to gdbserver's thread name
code:

    commit 07b3255c3bae7126a0d679f957788560351eb236
    Author: Tom Tromey <tom@tromey.com>
    Date:   Thu Jul 13 17:28:48 2023 -0600

Filter invalid encodings from Linux thread names

... introduced a regression.  This bug was that the iconv output was
not \0-terminated.

Looking at it, I found another bug as well -- replace_non_ascii would
not \0-terminate, and also would return the wrong pointer

This patch fixes both of them.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31153

gdbserver/linux-low.cc

index 4aa011c14ecf9573564cb2383ad6e9fea776d7aa..8cbc7833e532ad4e22eaee816374c3071551f910 100644 (file)
@@ -7013,11 +7013,13 @@ current_lwp_ptid (void)
 }
 
 /* A helper function that copies NAME to DEST, replacing non-printable
-   characters with '?'.  Returns DEST as a convenience.  */
+   characters with '?'.  Returns the original DEST as a
+   convenience.  */
 
 static const char *
 replace_non_ascii (char *dest, const char *name)
 {
+  const char *result = dest;
   while (*name != '\0')
     {
       if (!ISPRINT (*name))
@@ -7026,7 +7028,8 @@ replace_non_ascii (char *dest, const char *name)
        *dest++ = *name;
       ++name;
     }
-  return dest;
+  *dest = '\0';
+  return result;
 }
 
 const char *
@@ -7064,8 +7067,8 @@ linux_process_target::thread_name (ptid_t thread)
       else if ((errno == EILSEQ || errno == EINVAL)
               && outbuf < &dest[sizeof (dest) - 2])
        *outbuf++ = '?';
-      *outbuf = '\0';
     }
+  *outbuf = '\0';
 
   iconv_close (handle);
   return *dest == '\0' ? nullptr : dest;