[PATCH] fix wcwidth to work with gcc 16 sign extension (Re: wchar.h: tweak wcwidth prototype parameter wchar_t -> wint_t)

Thomas Wolff <[email protected]> Wed, 3 Jun 2026 02:43:09 +0200
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------IwOxJTYbezHKVuQrEWWYSkxv
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

As discussed in the cygwin thread, I withdraw my previous patch and=20
provide the attached one to fix wcwidth for gcc 16.
Thomas

Am 01.06.2026 um 18:02 schrieb Thomas Wolff:
>
> Am 31.05.2026 um 13:57 schrieb Takashi Yano:
>> Hi Thomas,
>>
>> On Sun, 31 May 2026 10:06:12 +0200
>> Thomas Wolff wrote:
>>> Hi Brian,
>>>
>>> Am 31.05.2026 um 05:50 schrieb Brian Inglis via Cygwin:
>>>> On 2026-05-28 22:58, Thomas Wolff wrote:
>>>>> to make it compliant with newlib and the manual page;
>>>>> fixes cases of wrong width calculation:
>>>>> https://cygwin.com/pipermail/cygwin/2026-April/259597.html
>>>>> as mentioned in
>>>>> https://cygwin.com/pipermail/cygwin/2026-May/259734.html
>>>>> as described in
>>>>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D125451#c14
>>>>> attachment:
>>>> 0001-wchar.h-tweak-wcwidth-prototype-parameter-wchar_t-wi.patch
>>>>
>>>> The existing wcwidth declaration in newlib/libc/include/wchar.h agree=
s
>>>> with
>>>> POSIX 8 SUS V5.
>>>>
>>>> It is the man doc, definition, and implementation in
>>>> newlib/libc/string/wcwidth.c which need changed to match the
>>>> specification and return codes in:
>>>>
>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0https://pubs.opengroup.org/onlinepubs/9=
799919799/functions/wcwidth.html=20
>>>>
>>> Your argument overlooks one significant deviation: in POSIX, wchar_t=
=20
>>> has
>>> 32 bits, in cygwin only 16.
>>> So to make wcwidth work for *all* Unicode character code points, the 3=
2
>>> bit version must be used.
>>> I tested positively that this fixes the broken test case with gcc 16 I
>>> had reported to the cygwin list.
>> However, newlib is not used only by Cygwin, so I think newlib itself=20
>> should
>> follow POSIX. Shouldn't we have our own wcwidth() implementation for=20
>> Cygwin?
>>
>> On second thought, since a 16=E2=80=91bit wchar_t needs to be converted=
 to a=20
>> 32=E2=80=91bit
>> Unicode code point especially for surrogate pair, we cannot use=20
>> wcwidth in
>> the same way as Linux does. I wonder what the correct approach would be=
.
> I don't there is a "correct" approach as POSIX probably did not=20
> consider this problem.
> But I just responded to a cute idea on the cygwin mailing list, which=20
> was unfeasible but I modified it with a proposal to return width 1 for=
=20
> a high surrogate, remember it, and then return 1 or 0 for the low=20
> surrogate, respectively.

--------------IwOxJTYbezHKVuQrEWWYSkxv
Content-Type: text/plain; charset=UTF-8;
 name="0001-sync-wcwidth-parameter-width-with-prototype-in-wchar.patch"
Content-Disposition: attachment;
 filename*0="0001-sync-wcwidth-parameter-width-with-prototype-in-wchar.pa";
 filename*1="tch"
Content-Transfer-Encoding: base64

RnJvbSAwOGYwMGE2NTk5ODEwZThhYTVmYzRiNDU2YTViMTgzNjM1Y2U5MWZjIE1vbiBTZXAg
MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBUaG9tYXMgV29sZmYgPHRvd29AdG93by5uZXQ+CkRh
dGU6IFdlZCwgMyBKdW4gMjAyNiAwMDowMDowMCArMDAwMApTdWJqZWN0OiBbUEFUQ0hdIHN5
bmMgd2N3aWR0aCBwYXJhbWV0ZXIgd2lkdGggd2l0aCBwcm90b3R5cGUgaW4gd2NoYXIuaAoK
aW4gb3JkZXIgdG8gZXhjbHVkZSB1bmRlZmluZWQgYmVoYXZpb3VyIG9uIHBhcmFtZXRlciB3
aWR0aCBleHRlbnNpb24KYXMgYXJpc2VkIGF0IGdjYyAxNiwgc2VlIGh0dHBzOi8vZ2NjLmdu
dS5vcmcvYnVnemlsbGEvc2hvd19idWcuY2dpP2lkPTEyNTQ1MSwKZml4ZXMgaHR0cHM6Ly9j
eWd3a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