From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 537103857C43; Tue, 26 Apr 2022 18:54:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 537103857C43 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] Handle encoding failures in Windows thread names X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: f93c6e0a2ed1ad4f0a9bb8f38e859f3312c25282 X-Git-Newrev: bfdb52f83ca6ca3a0eb43ef2bd0f4f8193a06472 Message-Id: <20220426185414.537103857C43@sourceware.org> Date: Tue, 26 Apr 2022 18:54:14 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Apr 2022 18:54:14 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dbfdb52f83ca6= ca3a0eb43ef2bd0f4f8193a06472 commit bfdb52f83ca6ca3a0eb43ef2bd0f4f8193a06472 Author: Tom Tromey Date: Tue Apr 19 11:21:35 2022 -0600 Handle encoding failures in Windows thread names =20 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 =3D GetThreadDescription (h, &value); if (SUCCEEDED (result)) { - size_t needed =3D wcstombs (nullptr, value, 0); - if (needed !=3D (size_t) -1) + int needed =3D WideCharToMultiByte (CP_ACP, 0, value, -1, nullptr, 0, + nullptr, nullptr); + if (needed !=3D 0) { - name.reset ((char *) xmalloc (needed)); - if (wcstombs (name.get (), value, needed) =3D=3D (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 =3D FALSE; + gdb::unique_xmalloc_ptr new_name + ((char *) xmalloc (needed)); + if (WideCharToMultiByte (CP_ACP, 0, value, -1, + new_name.get (), needed, + nullptr, &used_default) =3D=3D needed + && !used_default + && strlen (new_name.get ()) > 0) + name =3D std::move (new_name); } LocalFree (value); }