Skip to content

Pass bytes above 127 safely to the cctype classifiers, and skip a UTF-8 BOM - #1456

Open
helixsolara wants to merge 2 commits into
hsutter:mainfrom
helixsolara:fix-cctype-bom
Open

helixsolara wants to merge 2 commits into
hsutter:mainfrom
helixsolara:fix-cctype-bom

Conversation

@helixsolara

Copy link
Copy Markdown

cppfront passes plain char values to isspace, isdigit, isxdigit, isalpha and isalnum. Where char is signed (MSVC), a byte above 127 becomes a negative int, which those functions don't accept: undefined behavior, and a debug build of cppfront stops with a CRT assertion dialog:

File: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp
Line: 36
Expression: c >= -1 && c <= 255

A .cpp2 file saved with a UTF-8 byte order mark, which Visual Studio can write, hits this on its first line.

Changes

  • common.h gains as_ctype(char), which converts through unsigned char, and every classifier call in source/ goes through it.
  • cpp2util.h's is_id_char casts the same way.
  • io.h drops a UTF-8 byte order mark from the start of the first line, so it doesn't reach the lexer as source.

Tests

  • New regression test pure2-utf8-byte-order-mark.cpp2, which starts with a byte order mark. A debug build without the fix fails the assertion on it; with the fix it passes run-tests.sh with MSVC (c++20 and c++latest), whose results are included. I could only run MSVC, so the other compilers' result files aren't included.
  • All 192 existing regression tests transpile to the same .cpp and .cpp2.output as before.

🤖 Generated with Claude Code

helixsolara and others added 2 commits November 24, 2026 08:16
…-8 BOM.

cppfront passed plain char values to isspace, isdigit, isxdigit, isalpha and isalnum. Where char is signed, as with MSVC, a byte above 127 becomes a negative int, which those functions don't accept: it is undefined behavior, and a debug build of cppfront stops with a CRT assertion dialog (isctype.cpp, c >= -1 && c <= 255). A source file saved with a UTF-8 byte order mark, as Visual Studio can write, hits this on its first line. common.h gains as_ctype, which converts through unsigned char, and every classifier call in the sources goes through it; cpp2util.h's is_id_char casts the same way. io.h also drops a byte order mark from the start of the first line, so it doesn't reach the lexer as source.

The new regression test pure2-utf8-byte-order-mark.cpp2 starts with a byte order mark. A debug build without the fix fails the assertion on it; with the fix, run-tests.sh passes it with MSVC in both c++20 and c++latest, and its results for those are included. The other compilers' results aren't, since I could only run MSVC. All 192 existing regression tests transpile to the same .cpp and .cpp2.output as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The first commit wrapped every direct call to a cctype classifier, but two places pass a classifier as a function to call later: move_next in io.h calls its predicate with a plain char, and source_line::indent in common.h gives &isspace to std::find_if_not over a std::string. Both now convert through unsigned char, move_next with as_ctype and indent with a lambda, since as_ctype is defined after it.

All 192 regression tests still transpile to the same .cpp and .cpp2.output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant