[newlib-cygwin/main] wcrtomb: fix CESU-8 value of leftover lone high surrogate

Christian Franke chrfranke@sourceware.org
Mon Jun 30 11:30:09 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=96f23496f249558949923e60270b9568956912bf

commit 96f23496f249558949923e60270b9568956912bf
Author: Christian Franke <christian.franke@t-online.de>
Date:   Sun Jun 29 19:03:36 2025 +0200

    wcrtomb: fix CESU-8 value of leftover lone high surrogate
    
    Addresses: https://cygwin.com/pipermail/cygwin/2025-June/258378.html
    Fixes: 6ff28fc3b121 ("Allow CESU-8 surrogate value encoding")
    Signed-off-by: Christian Franke <christian.franke@t-online.de>

Diff:
---
 newlib/libc/stdlib/wctomb_r.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/newlib/libc/stdlib/wctomb_r.c b/newlib/libc/stdlib/wctomb_r.c
index 5ea1e13e4..ec6adfa49 100644
--- a/newlib/libc/stdlib/wctomb_r.c
+++ b/newlib/libc/stdlib/wctomb_r.c
@@ -62,8 +62,8 @@ __utf8_wctomb (struct _reent *r,
 	 of the surrogate and proceed to convert the given character.  Note
 	 to return extra 3 bytes. */
       wchar_t tmp;
-      tmp = (state->__value.__wchb[0] << 16 | state->__value.__wchb[1] << 8)
-	    - (0x10000 >> 10 | 0xd80d);
+      tmp = (((state->__value.__wchb[0] << 16 | state->__value.__wchb[1] << 8)
+	    - 0x10000) >> 10) | 0xd800;
       *s++ = 0xe0 | ((tmp & 0xf000) >> 12);
       *s++ = 0x80 | ((tmp &  0xfc0) >> 6);
       *s++ = 0x80 |  (tmp &   0x3f);


More information about the Newlib-cvs mailing list