public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH][PR 31853] Fix printing strings on macOS Sonoma
@ 2024-06-10 16:28 Ciaran Woodward
  2024-06-10 20:05 ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Ciaran Woodward @ 2024-06-10 16:28 UTC (permalink / raw)
  To: gdb-patches; +Cc: Ciaran Woodward

On macOS sonoma, printing a string would only print the first
character. For instance, if there was a 'const char *s = "foobar"',
then the 'print s' command would print '$1 = "f"' rather than the
expected '$1 = "foobar"'.

It seems that this is due to Apple silently replacing the version
of libiconv they ship with the OS to one which silently fails to
handle the 'outbytesleft' parameter correctly when using 'wchar_t'
as a target encoding.

This specifically causes issues when using iterating through a
string as wchar_iterator does.

This bug is visible even if you build for an old version of macOS,
but then run on Sonoma. Therefore this fix in the code applies
generally to macOS, and not specific to building on Sonoma. Building
for an older version and expecting forwards compatibility is a
common situation on macOS.

The INTERMEDIATE_ENCODING which this patch modifies is only used as
an intermediate encoding. i.e. The characters which are converted to
it are always converted to a different encoding (such as the host
encoding) before actually being passed to any external API such
as printf, so this should be pretty low-impact.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31853
---
 gdb/gdb_wchar.h | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/gdb/gdb_wchar.h b/gdb/gdb_wchar.h
index 8df00e4679f..909a0e4ac71 100644
--- a/gdb/gdb_wchar.h
+++ b/gdb/gdb_wchar.h
@@ -76,8 +76,19 @@ typedef wint_t gdb_wint_t;
    We exploit this fact in the hope that there are hosts that define
    this but which do not support "wchar_t" as an encoding argument to
    iconv_open.  We put the endianness into the encoding name to avoid
-   hosts that emit a BOM when the unadorned name is used.  */
-#if defined (__STDC_ISO_10646__)
+   hosts that emit a BOM when the unadorned name is used.
+
+   Also, on version 14 macOS 'Sonoma', the implementation of iconv was
+   changed in such a way that breaks the way that gdb was using it.
+   Specifically, using wchar_t as an intermediate encoding silently
+   breaks when attempting to do character-by-character encoding.
+   By using the intermediate_encoding function to choose a suitable
+   encoding to put in the wchar_t, the iconv implementation behaves as
+   we expect it to. Strictly speaking, this seems to be a bug in
+   Sonoma specifically, but it is desirable for binaries built for
+   older versions of macOS to still work on newer ones such as Sonoma,
+   so there is no version check here for this workaround.  */
+#if defined (__STDC_ISO_10646__) || defined (__APPLE__)
 #define USE_INTERMEDIATE_ENCODING_FUNCTION
 #define INTERMEDIATE_ENCODING intermediate_encoding ()
 const char *intermediate_encoding (void);
-- 
2.25.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH][PR 31853] Fix printing strings on macOS Sonoma
  2024-06-10 16:28 [PATCH][PR 31853] Fix printing strings on macOS Sonoma Ciaran Woodward
@ 2024-06-10 20:05 ` Tom Tromey
  2024-06-11 10:32   ` Ciaran Woodward
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2024-06-10 20:05 UTC (permalink / raw)
  To: Ciaran Woodward; +Cc: gdb-patches

>>>>> "Ciaran" == Ciaran Woodward <ciaranwoodward@xmos.com> writes:

Ciaran> The INTERMEDIATE_ENCODING which this patch modifies is only used as
Ciaran> an intermediate encoding. i.e. The characters which are converted to
Ciaran> it are always converted to a different encoding (such as the host
Ciaran> encoding) before actually being passed to any external API such
Ciaran> as printf, so this should be pretty low-impact.

I think this isn't quite true as the wchar_t might be passed to
iswprint.

Ciaran> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31853

I think this is ok.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH][PR 31853] Fix printing strings on macOS Sonoma
  2024-06-10 20:05 ` Tom Tromey
@ 2024-06-11 10:32   ` Ciaran Woodward
  2024-06-11 15:08     ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Ciaran Woodward @ 2024-06-11 10:32 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

 
> Ciaran> The INTERMEDIATE_ENCODING which this patch modifies is only used
> as
> Ciaran> an intermediate encoding. i.e. The characters which are converted
> to
> Ciaran> it are always converted to a different encoding (such as the host
> Ciaran> encoding) before actually being passed to any external API such
> Ciaran> as printf, so this should be pretty low-impact.
> 
> I think this isn't quite true as the wchar_t might be passed to
> iswprint.

Ah yes, you are correct, I removed the problem text from the commit message and pushed.

My apologies, I forgot to add your approved-by tag.

Thanks,
Ciaran

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH][PR 31853] Fix printing strings on macOS Sonoma
  2024-06-11 10:32   ` Ciaran Woodward
@ 2024-06-11 15:08     ` Tom Tromey
  2024-06-11 16:19       ` Ciaran Woodward
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2024-06-11 15:08 UTC (permalink / raw)
  To: Ciaran Woodward; +Cc: Tom Tromey, gdb-patches

>>>>> "Ciaran" == Ciaran Woodward <ciaranwoodward@xmos.com> writes:

Ciaran> Ah yes, you are correct, I removed the problem text from the
Ciaran> commit message and pushed.

I think it would be worth applying this patch to the gdb 15 branch as
well.  WDYT?

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH][PR 31853] Fix printing strings on macOS Sonoma
  2024-06-11 15:08     ` Tom Tromey
@ 2024-06-11 16:19       ` Ciaran Woodward
  2024-06-11 17:58         ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Ciaran Woodward @ 2024-06-11 16:19 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> 
> I think it would be worth applying this patch to the gdb 15 branch as
> well.  WDYT?

I would agree. Otherwise it wouldn't be possible for Mac users to print
strings with gdb. I don't think any potential obscure missed consequences
would be worse than that.

What do I do, just rebase my patch onto gdb-15-branch and push it?

Cheers,
Ciaran


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH][PR 31853] Fix printing strings on macOS Sonoma
  2024-06-11 16:19       ` Ciaran Woodward
@ 2024-06-11 17:58         ` Tom Tromey
  2024-06-12 10:21           ` Ciaran Woodward
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2024-06-11 17:58 UTC (permalink / raw)
  To: Ciaran Woodward; +Cc: Tom Tromey, gdb-patches

>>>>> "Ciaran" == Ciaran Woodward <ciaranwoodward@xmos.com> writes:

>> 
>> I think it would be worth applying this patch to the gdb 15 branch as
>> well.  WDYT?

Ciaran> I would agree. Otherwise it wouldn't be possible for Mac users to print
Ciaran> strings with gdb. I don't think any potential obscure missed consequences
Ciaran> would be worse than that.

Ciaran> What do I do, just rebase my patch onto gdb-15-branch and push it?

I'd probably use "git cherry-pick -x" but yeah.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH][PR 31853] Fix printing strings on macOS Sonoma
  2024-06-11 17:58         ` Tom Tromey
@ 2024-06-12 10:21           ` Ciaran Woodward
  0 siblings, 0 replies; 7+ messages in thread
From: Ciaran Woodward @ 2024-06-12 10:21 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches


> Ciaran> What do I do, just rebase my patch onto gdb-15-branch and push it?
> 
> I'd probably use "git cherry-pick -x" but yeah.
> 
> Tom

Thanks, pushed

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-06-12 10:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-10 16:28 [PATCH][PR 31853] Fix printing strings on macOS Sonoma Ciaran Woodward
2024-06-10 20:05 ` Tom Tromey
2024-06-11 10:32   ` Ciaran Woodward
2024-06-11 15:08     ` Tom Tromey
2024-06-11 16:19       ` Ciaran Woodward
2024-06-11 17:58         ` Tom Tromey
2024-06-12 10:21           ` Ciaran Woodward

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).