This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by mstorsjo on Feb 17 2022, 2:09 PM.

Details

Summary

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.

Diff Detail

Event Timeline

mstorsjo created this revision.Feb 17 2022, 2:09 PM
mstorsjo requested review of this revision.Feb 17 2022, 2:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 17 2022, 2:09 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript

Looks like an improvement to me — but I'd like to see my comment explored a bit.

libcxx/src/locale.cpp
1527

I don't think the cast to int is doing anything anymore; what goes wrong if you remove it? Ditto below.

mstorsjo marked an inline comment as done.Feb 24 2022, 2:45 AM
mstorsjo added inline comments.
libcxx/src/locale.cpp
1527

Sure, it should work just fine to drop the cast.

mstorsjo updated this revision to Diff 411052.Feb 24 2022, 2:50 AM
mstorsjo marked an inline comment as done.

Removed the now unused static_cast<int>().

(The CI failure was spurious and went away when I retried that job, fwiw.)

Quuxplusone accepted this revision.Feb 25 2022, 10:34 AM

LGTM! One nit.

libcxx/src/locale.cpp
1527

Here and below, I'd now prefer parens
return (r != EOF) ? static_cast<char>(r) : dfault;
for clarity, although it's no big deal.

This revision is now accepted and ready to land.Feb 25 2022, 10:34 AM
mstorsjo added inline comments.Feb 25 2022, 1:45 PM
libcxx/src/locale.cpp
1527

Ok, sure, will do. Yeah I prefer that form for clarity, too.

This revision was landed with ongoing or failed builds.Feb 25 2022, 1:49 PM
This revision was automatically updated to reflect the committed changes.