[libc-commits] [libc] [libc] Added c32rtomb implementation (PR #236061)

Arya C S via libc-commits libc-commits at lists.llvm.org
Fri Nov 13 22:28:16 PST 2026


================
@@ -132,6 +132,11 @@ LIBC_INLINE int CharacterConverter::push(char32_t utf32) {
   if (!isEmpty())
     return -1;
 
+  if (utf32 >= 0xd800 && utf32 <= 0xdfff) {
+    clear();
+    return EILSEQ;
----------------
AryaCS111 wrote:

`c32rtomb` delegates character conversion and validation to `CharacterConverter::push(char32_t)`. 

Previously, `CharacterConverter::push` only validated that codepoints did not exceed `0x10FFFF`. Because UTF-16 surrogate codepoints (`0xD800`–`0xDFFF`) are <= `0xFFFF`, the loop treated them as valid and erroneously encoded them into illegal 3-byte UTF-8 sequences (`0xED 0xA0 0x80`–`0xED 0xBF 0xBF`).

Under the C standard (C11/C23 §7.28.1.3) and Unicode / RFC 3629:
- Surrogates are not valid standalone Unicode characters.
- When an invalid character is passed to `c32rtomb`, the function is required to set `errno = EILSEQ` and return `(size_t)(-1)`.

Adding this check in `CharacterConverter::push` ensures that surrogate codepoints are rejected with `EILSEQ` at the conversion source, which is verified by the `InvalidSurrogate` test case in `c32rtomb_test.cpp`.

https://github.com/llvm/llvm-project/pull/236061


More information about the libc-commits mailing list