Repository navigation
text-freetype2: Decode UTF-16 text files into wide strings - #121
Conversation
71a05a0 to
57e38f5
Compare
|
Rebased onto master (old head 71a05a0, replaced with --force-with-lease); the test/cmocka/CMakeLists.txt conflict is resolved with both blocks kept. RED→GREEN in Tier 2: at the test commit, |
Moves remove_cr and the bodies of load_text_from_file and read_from_end to text-file.c as ft2_read_text_file and ft2_read_text_file_end, which return the text (NULL when the file cannot be opened) instead of storing it in ft2_source. The two functions in text-functionality.c keep the logging, the file_load_failed flag and the swap of srcdata->text. No behavior change; this lets a test build the readers without FreeType. Refs obs-rust#95 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reads UTF-8 and UTF-16 (BOM) files with ft2_read_text_file and ft2_read_text_file_end and compares with the UTF-8 text through os_utf8_to_wcs_ptr, so the expectations hold for 2- and 4-byte wchar_t: - carriage returns removed, a surrogate pair is one character - a stray last byte of a UTF-16 file is dropped - the last lines of a UTF-16 file are those of the same UTF-8 file, without the BOM The UTF-16 cases fail until the next commit: the units are copied raw into wchar_t without a terminator, an odd size over-reads, the reader of whole UTF-16 files keeps \r, and reading from the end can include the BOM. Refs obs-rust#95 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both file readers copied the UTF-16 units of a file with a BOM straight into the wchar_t text: - where wchar_t is 4 bytes (Linux, macOS), pairs of units became one garbage character, and the text had no 4-byte terminator, so remove_cr and every later wcslen ran past the buffer and remove_cr wrote past it - ft2_read_text_file_end allocated no room for a terminator on any platform, and an odd file size over-read on Windows too - with an odd size, the backwards scan (cur_pos -= 2 on a uint32_t) could step below 0 and wrap, looping over ~2^31 seeks The text is now decoded unit by unit into a terminated wchar_t string; where wchar_t is UTF-32, surrogate pairs are joined and unpaired surrogates become U+FFFD. A stray last byte is dropped, and the scan from the end stops at the BOM, which is no longer part of the text. Whole UTF-16 files now have carriage returns removed like every other case. Fixes obs-rust#95 Co-Authored-By: Claude Opus 5.5 <[email protected]> Local-Gate: v1 tree=baf6344f2022afeba3e26ae8bd217e5de78f685a secs=365 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791671646,"gate":"rust/all/fmt","host":"linux-x86_64","key":"8cd6ee2baa7c11f5ea5f4c06b58caca818a5cd883ff975fdf62868855f8bce59","lane":"lint","parents":["a0fb5573845a038c6bfa1e3367c8eb22aa1abeaf"],"secs":172,"stamp":"d31d83fbc9862159d6e1d6cace73bf36","tree":"baf6344f2022afeba3e26ae8bd217e5de78f685a","v":1,"via":"run"} Ci-Attestation: {"at":1791671646,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"8cd6ee2baa7c11f5ea5f4c06b58caca818a5cd883ff975fdf62868855f8bce59","lane":"lint","parents":["a0fb5573845a038c6bfa1e3367c8eb22aa1abeaf"],"secs":172,"stamp":"2cba1a3c7f6a5cffc69eb655dd14b4d6","tree":"baf6344f2022afeba3e26ae8bd217e5de78f685a","v":1,"via":"run"} Ci-Attestation: {"at":1791671646,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"06091c0dd7710d795557ee417d411a9949649a045b418ecf18441c9b49e293fd","lane":"rust-test","parents":["a0fb5573845a038c6bfa1e3367c8eb22aa1abeaf"],"secs":141,"stamp":"141ab2fc2a703e334783eb057e77821c","tree":"baf6344f2022afeba3e26ae8bd217e5de78f685a","v":1,"via":"run"} Ci-Attestation: {"at":1791671646,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"77f72abd975a117377f43e6ec9115982f7a84a6453fe420ebcbebbfae1cc3c7b","lane":"tier2-linux","parents":["a0fb5573845a038c6bfa1e3367c8eb22aa1abeaf"],"secs":50,"stamp":"24250aba9b863e823ecd918260056932","tree":"baf6344f2022afeba3e26ae8bd217e5de78f685a","v":1,"via":"run"}
|
please switch this merge request from draft something more final |
|
Marked ready, thanks for the rebase and the Tier 2 run. It conflicts again since #168: only the test target moves, to |
57e38f5 to
8182022
Compare
|
Verified locally after rebasing onto current master. It's ready, but I can't push the rebase to your branch ("Allow edits by maintainers" is off). Your base predates #220, so CI configure fails on it with
The rebase applies cleanly. Please either run |
Fixes #95.
Both text file readers of the FreeType 2 text source (
load_text_from_file,read_from_end) copied the UTF-16 units of a file with a BOM straight into thewchar_t *text:wchar_t(Linux, macOS): pairs of units became one garbage character, and the text had no 4-byte terminator, soremove_crand every laterwcslenread past the buffer, andremove_crwrote past it (the issue's report).read_from_endallocated no room for a terminator;cur_pos -= 2on auint32_tcould wrap and loop over ~2³¹ seeks.load_text_from_filenever removed\rfrom UTF-16 files, unlike the UTF-8 path and both paths ofread_from_end. Whenread_from_endreached the start, it also kept the BOM as text.Commits
remove_crand the two readers' bodies move totext-file.casft2_read_text_file/ft2_read_text_file_end. They return the text (NULL if the file can't be opened) instead of writingft2_source.load_text_from_file/read_from_endkeep the logging,file_load_failedand the swap ofsrcdata->text. This lets a test build the readers without FreeType.test/cmocka/test_text_file.c. Expectations are UTF-8 strings converted withos_utf8_to_wcs_ptr, so they hold for bothwchar_tsizes:\rremoved, U+00E9 and a surrogate pair (U+1F600), empty and BOM-only files, missing filelog_lines0..6, a UTF-16 file gives the same text as the same UTF-8 filewchar_tstring. WhereWCHAR_MAX > 0xFFFF, surrogate pairs are joined and unpaired surrogates become U+FFFD. A stray last byte is dropped, the backwards scan stops at the BOM, and whole UTF-16 files getremove_crtoo.Testing
Run locally on Windows (MSVC, a minimal cmocka stand-in, and stubs for
bmalloc/os_fopenplus the UTF-8 converters around the realutil/utf8.c):\rkept (8 != 7), odd size over-read (3 != 1), and from-the-end text with the BOM plus garbage (71 != 1);The 4-byte
wchar_tbranch only runs on the Linux/macOS CI.🤖 Generated with Claude Code