public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom Tromey <tromey@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] Handle encoding failures in Windows thread names
Date: Tue, 26 Apr 2022 18:54:14 +0000 (GMT)	[thread overview]
Message-ID: <20220426185414.537103857C43@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bfdb52f83ca6ca3a0eb43ef2bd0f4f8193a06472

commit bfdb52f83ca6ca3a0eb43ef2bd0f4f8193a06472
Author: Tom Tromey <tromey@adacore.com>
Date:   Tue Apr 19 11:21:35 2022 -0600

    Handle encoding failures in Windows thread names
    
    Internally at AdaCore, we noticed that the new Windows thread name
    code could fail.  First, it might return a zero-length string, but in
    gdb conventions it should return nullptr instead.  Second, an encoding
    failure could wind up showing replacement characters to the user; this
    is confusing and not useful; it's better to recognize such errors and
    simply discard the name.  This patch makes both of these changes.

Diff:
---
 gdb/nat/windows-nat.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
index bd1b9459145..c8db19439f3 100644
--- a/gdb/nat/windows-nat.c
+++ b/gdb/nat/windows-nat.c
@@ -119,12 +119,23 @@ windows_thread_info::thread_name ()
       HRESULT result = GetThreadDescription (h, &value);
       if (SUCCEEDED (result))
 	{
-	  size_t needed = wcstombs (nullptr, value, 0);
-	  if (needed != (size_t) -1)
+	  int needed = WideCharToMultiByte (CP_ACP, 0, value, -1, nullptr, 0,
+					    nullptr, nullptr);
+	  if (needed != 0)
 	    {
-	      name.reset ((char *) xmalloc (needed));
-	      if (wcstombs (name.get (), value, needed) == (size_t) -1)
-		name.reset ();
+	      /* USED_DEFAULT is how we detect that the encoding
+		 conversion had to fall back to the substitution
+		 character.  It seems better to just reject bad
+		 conversions here.  */
+	      BOOL used_default = FALSE;
+	      gdb::unique_xmalloc_ptr<char> new_name
+		((char *) xmalloc (needed));
+	      if (WideCharToMultiByte (CP_ACP, 0, value, -1,
+				       new_name.get (), needed,
+				       nullptr, &used_default) == needed
+		  && !used_default
+		  && strlen (new_name.get ()) > 0)
+		name = std::move (new_name);
 	    }
 	  LocalFree (value);
 	}


                 reply	other threads:[~2022-04-26 18:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220426185414.537103857C43@sourceware.org \
    --to=tromey@sourceware.org \
    --cc=gdb-cvs@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).