[libcxx-commits] [libcxx] 0f5d0d4 - [libcxx] Fix the error checking for wctob_l, fixing locale narrow function on Windows

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Fri Feb 25 13:48:49 PST 2022


Author: Martin Storsjö
Date: 2022-02-25T23:44:23+02:00
New Revision: 0f5d0d4e820ea50e251fb4b6592c02635c49081e

URL: https://github.com/llvm/llvm-project/commit/0f5d0d4e820ea50e251fb4b6592c02635c49081e
DIFF: https://github.com/llvm/llvm-project/commit/0f5d0d4e820ea50e251fb4b6592c02635c49081e.diff

LOG: [libcxx] Fix the error checking for wctob_l, fixing locale narrow function on Windows

According to POSIX.1 (and Glibc docs, and Microsoft docs), the wctob
function returns EOF on error, not WEOF. (And wctob_l should consequently
do the same.)

The previous misconception about what this function returns on errors
seems to stem from incorrect documentation in macOS, stemming from BSD
docs with the same issue. The corresponding documentation bug in FreeBSD
was fixed in 2012 in
https://github.com/freebsd/freebsd-src/commit/945aab90991bdaeabeb6ef25112975a96c01dd4e,
but it hasn't been fixed for macOS yet.

The issue seems to only be a documentation issue; the implementation
on macOS actually does use EOF, not WEOF:
https://opensource.apple.com/source/Libc/Libc-1439.40.11/locale/FreeBSD/wctob.c.auto.html

On most Unices, EOF and WEOF are the same value, but on Windows,
EOF is -1, while WEOF is (unsigned short)0xFFFF. By fixing this,
two tests start passing on Windows.

Differential Revision: https://reviews.llvm.org/D120088

Added: 
    

Modified: 
    libcxx/src/locale.cpp
    libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_1.pass.cpp
    libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_many.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp
index bd3f8733f57c4..fd0e17a7007b4 100644
--- a/libcxx/src/locale.cpp
+++ b/libcxx/src/locale.cpp
@@ -1524,7 +1524,7 @@ char
 ctype_byname<wchar_t>::do_narrow(char_type c, char dfault) const
 {
     int r = __libcpp_wctob_l(c, __l);
-    return r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault;
+    return (r != EOF) ? static_cast<char>(r) : dfault;
 }
 
 const wchar_t*
@@ -1533,7 +1533,7 @@ ctype_byname<wchar_t>::do_narrow(const char_type* low, const char_type* high, ch
     for (; low != high; ++low, ++dest)
     {
         int r = __libcpp_wctob_l(*low, __l);
-        *dest = r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault;
+        *dest = (r != EOF) ? static_cast<char>(r) : dfault;
     }
     return low;
 }

diff  --git a/libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_1.pass.cpp b/libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_1.pass.cpp
index 2c2f9b676a076..c4cb4f7728af0 100644
--- a/libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_1.pass.cpp
+++ b/libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_1.pass.cpp
@@ -8,7 +8,6 @@
 
 // REQUIRES: locale.en_US.UTF-8
 // REQUIRES: locale.fr_CA.ISO8859-1
-// XFAIL: LIBCXX-WINDOWS-FIXME
 // XFAIL: libcpp-has-no-wide-characters
 
 // <locale>

diff  --git a/libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_many.pass.cpp b/libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_many.pass.cpp
index 8cb1e22a52afe..1442f1a9e0cad 100644
--- a/libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_many.pass.cpp
+++ b/libcxx/test/std/localization/locale.categories/category.ctype/locale.ctype.byname/narrow_many.pass.cpp
@@ -8,7 +8,6 @@
 
 // REQUIRES: locale.en_US.UTF-8
 // REQUIRES: locale.fr_CA.ISO8859-1
-// XFAIL: LIBCXX-WINDOWS-FIXME
 // XFAIL: libcpp-has-no-wide-characters
 
 // <locale>


        


More information about the libcxx-commits mailing list