Repository navigation
fix(libc): map _Towctrans descriptors 1 and 2 to lower and upper case - #4095
Merged
oneandonlydean merged 1 commit intoOct 12, 2026
Merged
Conversation
_Towctrans passed the guest's descriptor to the host towctrans. Dinkumware's <wctype.h> (xwcc.h) defines towlower(wc) as _Towctrans(wc, 1) and towupper(wc) as _Towctrans(wc, 2), the values FreeBSD's wctrans.c also uses for "tolower" and "toupper". UCRT reads 1 as toupper and other nonzero values as tolower, so on Windows towlower(L'A') returned L'A' and towupper(L'a') returned L'a', while towlower(L'a') returned L'A'. The host wint_t is 16 bits there, which also turned WEOF into 0xffff. glibc's wctrans_t is a pointer to a table that towctrans reads, so on Linux descriptor 1 or 2 is dereferenced as an address. _Towctrans now takes the guest's 32-bit wint_t and size_t descriptor. 1 and 2 map 0..255 through the C locale tables behind _Getptolower and _Getptoupper and return other values unchanged. Any other descriptor sets errno to EINVAL and returns wc unchanged, as FreeBSD's towctrans_l does.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_Towctranspassed the guest's descriptor straight to the hosttowctrans, but the descriptor is Dinkumware's, not the host's:towlower(wc)into_Towctrans(wc, 1)andtowupper(wc)into_Towctrans(wc, 2): TI's C6000 libraryxwcc.h(V4.02), the C++ inlines at lines 73-81 and the C macros at lines 129-131, next totowctrans(wc, off)as_Towctrans(wc, off). The descriptor is asize_tthere (typedef _Sizet wctrans_t;, wctype.h line 21 of another Dinkumware copy)._Getpctype,_Iswctype,_Stouland_Towctransare in the PS4 name list (names.txt lines 456, 480, 691, 733) and the Vita one (vita_names.txt line 803), and the review of feat(libc): implement the C11 mtx and cnd functions #492 (point 6) found Sony's_WStoulcalling_Iswctype(c, 9)for white space, the indexxwcc.hgivesiswspace._WCT_TOLOWER = 1and_WCT_TOUPPER = 2, andtowctrans_lreturnswcunchanged witherrno = EINVALfor any other descriptor.The hosts read the value differently. On Windows, UCRT's
towctransreads 1 as toupper and other nonzero values as tolower, and itswint_tis 16 bits. A probe calling the export as a guest does:towlower(L'A')=_Towctrans(L'A', 1)L'A'L'a'towlower(L'a')=_Towctrans(L'a', 1)L'A'L'a'towupper(L'a')=_Towctrans(L'a', 2)L'a'L'A'_Towctrans(L'A', 3)L'a', errno 0L'A', errnoEINVAL_Towctrans(L'A', 0)L'A', errno 0L'A', errnoEINVAL_Towctrans(WEOF, 1)0xffff0xffffffffOn Linux, glibc's
wctrans_tisconst __int32_t *(wctype.h line 48) andtowctranspasses any nonzero descriptor towctrans_table_lookup, which reads the table at that address (towctrans.c lines 24-33, wchar-lookup.h lines 117-119), so descriptor 1 or 2 is dereferenced as an address._Towctransnow takes the guest's 32-bitwint_tand 64-bit descriptor. 1 and 2 map 0..255 through the C locale tables behind_Getptolowerand_Getptoupper(ASCII letters only, as in the rune locale's__maplowerand__mapupper) and return other values unchanged, without the host'stowlowerortowupper. Any other descriptor setserrnotoEINVALand returnswcunchanged, as FreeBSD does.towlower,towupperand the other wide classification functions are left to #1783.Tested
Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release,
-DBUILD_TESTING=ON, onmainat 325e0a8 with #4021 applied locally, sincemain's Windows build stops inSocket.cppwithout it:CheckWideCaseTranslationinguest_locale:_Towctrans(L'A', 1) == L'a'and_Towctrans(L'a', 2) == L'A'; descriptors 1 and 2 against_Getptolower/_Getptoupperfor 0..255 (digits unchanged) witherrnountouched; 0x100, 0x391, 0x3B1, 0xFFFF andWEOFunchanged; descriptors 0 and 3 returnL'A'andL'7'unchanged and seterrnoto 22. Without the library change the test fails at the first check (terminate called after throwing an instance of 'std::runtime_error',what(): Guest locale check failed); with it, it passes 20 runs in a row.core/libs/prx/libc/CMakeLists.txtandcore/libs/prx/libkernel/CMakeLists.txtthat build on Windows pass.tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)