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

   ## Summary
   
   C99 specifies `%lc` for a wide character (`wint_t`) and `%ls` for a wide 
string (`wchar_t *`), written as their multibyte sequences. `printf()` ignored 
the `l` and handled them as `%c` and `%s`: `%lc` wrote only the low byte of the 
character, and `%ls` read the wide string as bytes and stopped at the first 
zero byte, which for ASCII text is after the first character.
   
   For example, bash's `help` builds each line of its command list as a wide 
string and prints it with `%ls`, so the list came out as lines of spaces.
   
   With `CONFIG_LIBC_LOCALE`, this PR converts them with `wcrtomb()`. For `%ls` 
the precision limits the number of bytes, and a character that does not fit is 
not written in part. A character that cannot be converted makes `printf()` fail 
with `EILSEQ`, as C99 specifies.
   
   ## Impact
   
   Only with `CONFIG_LIBC_LOCALE` (existing option, default n). `printf()` is 
in almost every image, so the conversion is built only with the locale support. 
Without it, `%lc` and `%ls` behave as before.
   
   ## Testing
   
   `qemu-armv8a:knsh`, QEMU 11, a test program:
   
   ```c
   printf("[%ls]\n", L"abc");
   printf("[%5ls][%-5ls]\n", L"ab", L"ab");
   printf("[%.2ls]\n", L"abcd");
   printf("[%lc][%lc]\n", (wint_t)L'x', (wint_t)0xe9);
   n = snprintf(b, sizeof(b), "%ls", L"été");
   printf("[%.1ls]\n", L"é");
   ```
   
   Before (and without `CONFIG_LIBC_LOCALE`):
   ```
   [a]
   [    a][a    ]
   [a]
   [x][?]
   snprintf returned 1: [?]
   [?] (a 2-byte character does not fit in 1 byte)
   ```
   
   After, with `CONFIG_LIBC_LOCALE`:
   ```
   [abc]
   [   ab][ab   ]
   [ab]
   [x][é]
   snprintf returned 5: [été]
   [] (a 2-byte character does not fit in 1 byte)
   ```
   
   Builds with `-Wno-cpp -Werror`: `lm3s6965-ek:qemu-flat` and `mps2-an500:nsh` 
as they are; `qemu-armv8a:nsh`, `qemu-armv8a:knsh`, `mps2-an500:nsh`, 
`raspberrypi-pico:nsh`, `rv-virt:nsh` and `qemu-armv8a:nsh` with 
`CONFIG_LIBC_NUMBERED_ARGS`, all with `CONFIG_LIBC_LOCALE`.
   
   `tools/checkpatch.sh` reports only the "Bad alignment" errors that 
`lib_libvsprintf.c` already has on master (its `case` labels are level with the 
braces).
   


-- 
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