From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by sourceware.org (Postfix) with ESMTPS id 679F03857369 for ; Thu, 21 Apr 2022 14:39:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 679F03857369 Received: by mail-io1-xd2e.google.com with SMTP id z19so889526iof.12 for ; Thu, 21 Apr 2022 07:39:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=kbpfth7070A7MOQhMRUFQ1GVMIihqlLKlQo5xCMGGNA=; b=DvUfIdKRGOFCiuhEfDjOy8GnDg3dRGArbHYRcQOM14YTJdxcOUULHrOAmPrRYNLodk iah12QBdRRo9H19fh30FLtb+omTIk9PiqTWiv5xX44gYnMobj+Ha/ALygRIalVwsrAxr eVxSBxVe9EXUQMjwlNn2BraRBSWz9w8ne7cWq5HFf7bRsXanQ3jyqesT11e+/I3egCvI VaKSRdrKRhfyaEP55A1Mb59DoZLojZPmq7Ve6CRe95j4iKAIuo1r4N8oZ0bpFMQNXPHY h5LIQfnQVKiAiG/4OnLvgWTpIcSPh/i5euN51t6LHTm25EGtHyHJ0N5+DLn1xBDxlBqQ npow== X-Gm-Message-State: AOAM532G9pEOH7wGpnxAYkp7UCPsKEsbmC/ID6CFEPgWg8oVsoDf0RNe /tx+Na0j40TQGeg8xHyKi5x7ynuTA8qCbw== X-Google-Smtp-Source: ABdhPJwrvluDYpnXFotJBMbL+bykoy2Hh0OkMNm6obIj4fdrupgreY5ZordzpxKqPCBYzSHiho02kQ== X-Received: by 2002:a05:6638:13d0:b0:328:6224:8dc8 with SMTP id i16-20020a05663813d000b0032862248dc8mr11922890jaj.144.1650551968707; Thu, 21 Apr 2022 07:39:28 -0700 (PDT) Received: from murgatroyd.Home (71-211-158-194.hlrn.qwest.net. [71.211.158.194]) by smtp.gmail.com with ESMTPSA id x8-20020a92d648000000b002ca2dc1a74esm12665104ilp.58.2022.04.21.07.39.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Apr 2022 07:39:28 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Handle encoding failures in Windows thread names Date: Thu, 21 Apr 2022 08:39:26 -0600 Message-Id: <20220421143926.2550856-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Apr 2022 14:39:31 -0000 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. --- gdb/nat/windows-nat.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c index bd1b9459145..7a4e804f891 100644 --- a/gdb/nat/windows-nat.c +++ b/gdb/nat/windows-nat.c @@ -119,12 +119,19 @@ 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 (); + BOOL used_default = FALSE; + gdb::unique_xmalloc_ptr 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); } -- 2.34.1