You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "MasterJH5574 (via GitHub)" <gi...@apache.org> on 2026/02/12 01:13:26 UTC
[PR] fix: handle UTF-8 bytes correctly in JSON parser control character check [tvm-ffi]
MasterJH5574 opened a new pull request, #442:
URL: https://github.com/apache/tvm-ffi/pull/442
Cast `*cur_` to `unsigned char` before comparing against space (0x20). On platforms where `char` is signed, bytes >= 0x80 (valid UTF-8 continuation/lead bytes) were misinterpreted as negative values, causing all non-ASCII strings to be rejected as "Invalid control character".
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: handle UTF-8 bytes correctly in JSON parser control character check [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on PR #442:
URL: https://github.com/apache/tvm-ffi/pull/442#issuecomment-3888208714
also this one might be interesting (not necessarily relevant): https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/tokenizer/detokenize.py#L14-L36
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: handle UTF-8 bytes correctly in JSON parser control character check [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #442:
URL: https://github.com/apache/tvm-ffi/pull/442#issuecomment-3888123050
## Summary of Changes
Hello @MasterJH5574, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses a critical bug in the JSON parser that prevented the correct processing of non-ASCII UTF-8 characters. By ensuring character comparisons are performed using unsigned values, the parser now accurately distinguishes between actual control characters and valid UTF-8 byte sequences, thereby enabling robust internationalization support for JSON data.
### Highlights
* **JSON Parser Fix**: Corrected the JSON parser's control character check by casting `char` to `unsigned char` before comparison. This resolves an issue where valid UTF-8 continuation/lead bytes (>= 0x80) were misinterpreted as negative values on platforms where `char` is signed, leading to the rejection of non-ASCII strings.
* **Regression Test Added**: Introduced a new test case (`RawUTF8Bytes`) to ensure that raw UTF-8 bytes are correctly handled and not rejected as invalid control characters, specifically targeting the scenario where signed `char` comparison previously failed.
🧠**New Feature in Public Preview:** You can now enable **Memory** to help **Gemini Code Assist** learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. **Click [here](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
<details>
<summary><b>Changelog</b></summary>
* **src/ffi/extra/json_parser.cc**
* Modified character comparison logic to use `static_cast<unsigned char>` for correct handling of UTF-8 bytes.
* **tests/cpp/extra/test_json_parser.cc**
* Added `RawUTF8Bytes` regression test to validate UTF-8 character parsing.
</details>
<details>
<summary><b>Using Gemini Code Assist</b></summary>
<br>
The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
<b>Invoking Gemini</b>
You can request assistance from Gemini at any point by creating a comment using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands on the current page.
Feature | Command | Description
--- | --- | ---
Code Review | `/gemini review` | Performs a code review for the current pull request in its current state.
Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state.
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help | `/gemini help` | Displays a list of available commands.
<b>Customization</b>
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
<b>Limitations & Feedback</b>
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up [here](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
<b>You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the [Gemini Code Assist IDE Extension](https://cloud.google.com/products/gemini/code-assist).</b>
</details>
[^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: handle UTF-8 bytes correctly in JSON parser control character check [tvm-ffi]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on PR #442:
URL: https://github.com/apache/tvm-ffi/pull/442#issuecomment-3888159553
good to followup with a comment
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: handle UTF-8 bytes correctly in JSON parser control character check [tvm-ffi]
Posted by "MasterJH5574 (via GitHub)" <gi...@apache.org>.
MasterJH5574 commented on PR #442:
URL: https://github.com/apache/tvm-ffi/pull/442#issuecomment-3888224230
@junrushao Thanks for the comments. To be standard JSON compliance, it looks like UTF-8 handling in tvm-ffi is already complete and no extra changes are needed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: handle UTF-8 bytes correctly in JSON parser control character check [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on PR #442:
URL: https://github.com/apache/tvm-ffi/pull/442#issuecomment-3888163277
Good catch! BTW, could you take a look at general UTF-8 handling in strings in mlc-python: https://github.com/mlc-ai/mlc-python/blob/main/include/mlc/core/str.h#L293-L443, and figure out what is missing in tvm-ffi?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: handle UTF-8 bytes correctly in JSON parser control character check [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao merged PR #442:
URL: https://github.com/apache/tvm-ffi/pull/442
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: handle UTF-8 bytes correctly in JSON parser control character check [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #442:
URL: https://github.com/apache/tvm-ffi/pull/442#discussion_r2796310044
##########
src/ffi/extra/json_parser.cc:
##########
@@ -341,7 +341,7 @@ class JSONParserContext {
// copy over the prefix that was already parsed
std::string out_str(start_pos + 1, cur_ - start_pos - 1);
while (cur_ != end_) {
- if (*cur_ < ' ') {
+ if (static_cast<unsigned char>(*cur_) < ' ') {
Review Comment:

For consistency with other parts of this file (e.g., `Peek()` and `Parse4Hex`), consider using `*reinterpret_cast<const uint8_t*>(cur_)` to treat the byte as an unsigned value. This would align with the approach used elsewhere in the parser for handling character bytes.
```c
if (*reinterpret_cast<const uint8_t*>(cur_) < ' ') {
```
##########
src/ffi/extra/json_parser.cc:
##########
@@ -131,7 +131,7 @@ class JSONParserContext {
++cur_;
return true;
}
- if (*cur_ < ' ' || *cur_ == '\\') {
+ if (static_cast<unsigned char>(*cur_) < ' ' || *cur_ == '\\') {
Review Comment:

For consistency with other parts of this file (e.g., `Peek()` and `Parse4Hex`), consider using `*reinterpret_cast<const uint8_t*>(cur_)` to treat the byte as an unsigned value. While `static_cast<unsigned char>(*cur_)` is correct, using a consistent approach for byte interpretation can improve code clarity and maintainability.
```c
if (*reinterpret_cast<const uint8_t*>(cur_) < ' ' || *cur_ == '\') {
```
##########
tests/cpp/extra/test_json_parser.cc:
##########
@@ -290,6 +290,12 @@ TEST(JSONParser, UnicodeEdgeCases) {
u8"\U0001F600\U0001F601");
}
+TEST(JSONParser, RawUTF8Bytes) {
+ // Regression test: raw UTF-8 bytes (>= 0x80) must not be rejected as control characters.
+ // This failed when the parser used signed char comparison: *cur_ < ' '
+ EXPECT_EQ(json::Parse("\"\xE4\xB8\xAD\xE6\x96\x87\"").cast<String>(), "\xE4\xB8\xAD\xE6\x96\x87");
Review Comment:

This regression test is great for verifying the fix. To make it more robust, consider adding a few more assertions to cover a wider range of scenarios. Using `u8` string literals can also improve readability while testing the same underlying byte sequences.
```suggestion
EXPECT_EQ(json::Parse(u8"\"䏿–‡\"").cast<String>(), u8"䏿–‡");
// Test with a mix of ASCII and non-ASCII characters.
EXPECT_EQ(json::Parse(u8"\"aä¸b\"").cast<String>(), u8"aä¸b");
// Test with a two-byte UTF-8 character (cent sign).
EXPECT_EQ(json::Parse(u8"\"¢\"").cast<String>(), u8"¢");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org