casaroli opened a new pull request, #21645:
URL: https://github.com/apache/nuttx/pull/21645

   ## Summary
   
   Two fixes in `libs/libc/locale`, one commit each:
   
   - **libs/libc/locale: Keep errno in gettext().** gettext() and the other 
functions of `<libintl.h>` changed errno. When there is no message catalog, the 
open() in `momap()` sets errno to ENOENT, and each call tries the open() again. 
A program often calls gettext() for the text of an error message between the 
failed call and the use of errno, for example `perror(_("setpgid"))`: the 
argument is evaluated before perror() reads errno. So the program reports "No 
such file or directory" for each error. The commit moves the body of 
dcngettext() into a static function `motranslate()`, and dcngettext(), which 
all the other functions call, now keeps errno around it. The save is at the 
top, not in `momap()`, because lib_malloc(), fstat() and mmap() can also change 
errno; the GNU C library does the same (intl/dcigettext.c saves errno at the 
start of DCIGETTEXT and restores it on each return). The diff moves the public 
functions below the new static function; their code does not change.
   - **libs/libc/locale: Accept C.UTF-8 in setlocale() and newlocale().** The C 
locale of NuttX uses UTF-8: MB_CUR_MAX is 4 and nl_langinfo(CODESET) gives 
"UTF-8". But setlocale() and newlocale() refused the names "C.UTF-8" and 
"C.utf8". The GNU C library accepts these names for its UTF-8 C locale, and 
many systems set one of them in LANG. So a program that selects one of these 
locales got an error, and printed a warning or stopped. Now setlocale() and 
newlocale() accept "C.UTF-8" and "C.utf8" as names of the C locale, and 
setlocale() returns "C.UTF-8" for them.
   
   ## Impact
   
   libc only. The same code in flat, protected and kernel builds; no arch code, 
no Kconfig option, no API change. No Documentation page describes these 
functions in detail, so no Documentation change.
   
   What a program sees:
   
   - With `CONFIG_LIBC_LOCALE_GETTEXT=y`: errno after gettext(), dgettext(), 
dcgettext(), ngettext(), dngettext() and dcngettext() is the value from before 
the call. Before, it was ENOENT when no catalog was found. textdomain(), 
bindtextdomain() and bind_textdomain_codeset() do not change.
   - With `CONFIG_LIBC_LOCALE=y`: setlocale(category, "C.UTF-8") and 
setlocale(category, "C.utf8") return "C.UTF-8" (before: NULL). newlocale() with 
these names returns a locale (before: NULL). The query setlocale(category, 
NULL) still returns "C", and "C", "POSIX", "" and all other names give the same 
results as before. The returned "C.UTF-8" is what glibc and macOS return for 
setlocale(LC_ALL, "C.UTF-8"), and setlocale() accepts it again, as POSIX 
requires for the returned string.
   
   ## Testing
   
   Test program: `testing/libc/locale` (`locale_test`) of 
apache/nuttx-apps#NNNN. It checks setlocale() and newlocale() with "C", 
"POSIX", "", "C.UTF-8", "C.utf8" and an unknown name, that the name setlocale() 
returns selects the locale again, nl_langinfo(CODESET) after 
setlocale(LC_CTYPE, "C.UTF-8"), and errno (EPERM before the call) and the text 
after the six gettext functions with no message catalog. All builds with 
`CONFIG_LIBC_LOCALE=y` and `CONFIG_LIBC_LOCALE_GETTEXT=y`.
   
   | Configuration | master 80a052ff9cc | this PR |
   |---|---|---|
   | qemu-armv8a:knsh (kernel build, arm64, HVF), make | `locale_test`: 11 
errors (C.UTF-8 and C.utf8 refused by setlocale() and newlocale() (4), so the 
CODESET check fails (1); errno 2 instead of 1 after each of the six gettext 
functions (6)); ostest status 0 | 0 errors; ostest status 0 |
   | qemu-armv8a:nsh (flat build, arm64, HVF), make | the same 11 errors; 
ostest status 0 | 0 errors; ostest status 0 |
   | mps2-an500:knsh (protected build, Cortex-M7, QEMU), make, 
arm-none-eabi-gcc 15.3, `EXTRAFLAGS=-Werror` | the same 11 errors; 0 warnings | 
0 errors; 0 warnings |
   | qemu-armv8a:nsh, CMake | - | 0 errors; 0 warnings |
   
   The same test on the host C library: glibc 2.43 (Fedora 44, x86_64): 0 
errors. macOS 27: 2 errors (it refuses "C.utf8"; it has no `<libintl.h>`, so no 
gettext cases).
   
   Size, stm32f4discovery:nsh (Cortex-M4), arm-none-eabi-gcc 15.3: `nuttx` is 
the same size before and after (text 94644 as it is, 94964 with `LIBC_LOCALE` 
and `LIBC_LOCALE_GETTEXT`; nsh does not call these functions). The objects 
grow: lib_gettext.o +32, lib_setlocale.o +51, lib_newlocale.o +47 bytes of text.
   
   `tools/checkpatch.sh -c -u -m -g origin/master..HEAD` passes.
   
   Not run: sim, RISC-V, x86_64, clang.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to