Skip to content

GSUnicode: fix UTF-16 encoding byte order and length - #122

Open
DTW-Thalion wants to merge 2 commits into
gnustep:masterfrom
DTW-Thalion:fix/gsunicode-utf16-encode
Open

GSUnicode: fix UTF-16 encoding byte order and length#122
DTW-Thalion wants to merge 2 commits into
gnustep:masterfrom
DTW-Thalion:fix/gsunicode-utf16-encode

Conversation

@DTW-Thalion

Copy link
Copy Markdown
Contributor

The UTF-16 branch of GSUnicodeToEncoding had two defects that corrupted CFStringGetBytes and CFStringCreateExternalRepresentation output for UTF-16:

The byte-order swap was guarded by the UTF-32BE/UTF-32LE constants (a copy of the UTF-32 branch), so UTF-16BE and UTF-16LE were never byte-swapped and came out in host order.

The copy length was computed as the minimum of a byte count and a character count and then used as a byte count, so any string longer than one character was truncated.

The branch now converts one character at a time, swapping to the requested byte order, and prepends a byte order mark only for the endianness-neutral UTF-16 encoding, matching Apple.

Tests/CFString/getbytes_utf16.m covers UTF-16BE/LE for single characters, multi-character strings and a surrogate pair. The existing encodings.m assertion that the plain and external UTF-16 conversions produce the same length is corrected: the external form adds a two-byte BOM. Both were verified against Apple CoreFoundation.

The UTF-16 branch of GSUnicodeToEncoding tested the UTF-32 encoding
constants when deciding whether to swap bytes, so UTF-16BE and UTF-16LE were
never byte-swapped, and it mixed byte and character counts when sizing the
copy, truncating multi-character output.  Convert one character at a time,
swapping for the non-native byte order, and emit a byte order mark only for
the endianness-neutral encoding.
@DTW-Thalion
DTW-Thalion force-pushed the fix/gsunicode-utf16-encode branch from b379000 to dd33e22 Compare July 24, 2026 15:43
@DTW-Thalion DTW-Thalion reopened this Jul 24, 2026
@DTW-Thalion DTW-Thalion reopened this Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant