diff options
author | Tom Tromey <tromey@adacore.com> | 2022-04-19 11:21:35 -0600 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2022-04-26 12:54:05 -0600 |
commit | bfdb52f83ca6ca3a0eb43ef2bd0f4f8193a06472 (patch) | |
tree | d272d3d9dca9e3864d38eaf4e7f0232a4e937b40 /gdb/nat/windows-nat.c | |
parent | f93c6e0a2ed1ad4f0a9bb8f38e859f3312c25282 (diff) | |
download | gdb-bfdb52f83ca6ca3a0eb43ef2bd0f4f8193a06472.zip gdb-bfdb52f83ca6ca3a0eb43ef2bd0f4f8193a06472.tar.gz gdb-bfdb52f83ca6ca3a0eb43ef2bd0f4f8193a06472.tar.bz2 |
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.
Diffstat (limited to 'gdb/nat/windows-nat.c')
-rw-r--r-- | gdb/nat/windows-nat.c | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c index bd1b945..c8db194 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); } |