wctomb() accepts out-of-range character in C-locale

Jun T takimoto-j@kba.biglobe.ne.jp
Mon Mar 25 07:45:33 GMT 2024


Dear newlib developers,
(this is the first time I post to this list)

On recent Cygwin, the following C code output '1' (i.e., wide character
0x80 can be converted into a valid single-byte character in C-locale):

---------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main() {
    char buf[MB_CUR_MAX];
    setlocale(LC_ALL, "C");
    printf("%d\n", wctomb(buf, 0x80));
    return 0;
}
---------------------------------------

On Linux it outputs '-1'.

It seems this is due to the following commit:

------------------------------------------------
commit 8a4318943875cd922601d34e54ce8a83ad2e733c
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Mon Jul 31 12:44:16 2023 +0200

    Revert "* libc/stdlib/mbtowc_r.c (__ascii_mbtowc): Disallow conversion of"

    This reverts commit 2b77087a48ea56e77fca5aeab478c922f6473d7c.

    For some reason lost in time, commit 2b77087a48ea5 introduced
    Cygwin-specific code treating single byte characters outside the
    portable character set as illegal chars.  However, Cygwin was
    always alone with this over-correct behaviour and it leads to
    stuff like gnulib replacing functions defined in Cygwin with
    their own implementation just due to that.
------------------------------------------------

Probably the function __ascii_wctomb() is used not only in C-locale
but also in some other locales, and the commit is for "fixing"
some problems in these locales?
But a wide character >= 0x80 can't be converted into a valid
character in C-loccale (7bit), I think.


More information about the Newlib mailing list