Skip to content

host_env: Don't truncate two byte wchar_t - #8296

Merged
youknowone merged 1 commit into
RustPython:mainfrom
joshuamegnauth54:host_env-dont-truncate-wchar_t
Jul 17, 2026
Merged

host_env: Don't truncate two byte wchar_t#8296
youknowone merged 1 commit into
RustPython:mainfrom
joshuamegnauth54:host_env-dont-truncate-wchar_t

Conversation

@joshuamegnauth54

@joshuamegnauth54 joshuamegnauth54 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Casting a u32 to a u16 would truncate the value. It's safer to use Wtf8::encode_wide which already handles converting a CodePoint to a u16.

I removed vec_into_bytes because it was only used in two places. The function was unsound for T but sound in the way RustPython used it (POD to POD, less strict alignment).

AI disclosure: I linted this code with AI. I found this issue by accident while working on another patch in which I introduced a similar mistake. AI caught that mistake, so I linted the original code to cross-check it. I wrote the code myself in both instances.

Assisted-by: Codex:gpt-5.4

Summary

  • Fixes wchar_t being truncated from u32 to u16 instead of correctly being encoded.

Summary by CodeRabbit

  • Bug Fixes

    • Improved platform-specific handling of wide-character data to provide more consistent text encoding behavior across supported systems.
  • Refactor

    • Simplified internal conversion logic and enabled compile-time evaluation for a character conversion utility.

Casting a u32 to a u16 would truncate the value. It's safer to use
Wtf::encode_wide which already handles converting a CodePoint to a u16.

I removed vec_into_bytes because it was only used in two places. The
function was unsound for T but sound in the way RustPython used it (POD
to POD, less strict alignment).

AI disclosure: I linted this code with AI. I found this issue by
accident while working on another patch in which I introduced a similar
mistake. AI caught that mistake, so I linted the original code to
cross-check it. I wrote the code myself in both instances.

Assisted-by: Codex:gpt-5.4
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 955d304b-63b8-4e95-b931-7d93c39215cd

📥 Commits

Reviewing files that changed from the base of the PR and between 0b4e5da and 1e55497.

📒 Files selected for processing (1)
  • crates/host_env/src/ctypes.rs

📝 Walkthrough

Walkthrough

Wide-character helpers now support compile-time decoding and platform-dependent null-terminated byte generation. UTF-16 encoding explicitly flattens native-endian code units and appends a wide-character terminator.

Changes

Wide-character encoding

Layer / File(s) Summary
Wide-character conversion helpers
crates/host_env/src/ctypes.rs
wchar_from_bytes is now const-capable. Null-terminated wide-character buffers use platform-specific encoding, while UTF-16 bytes are explicitly built in native-endian order with a terminating zero word.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: youknowone

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: preventing truncation for 2-byte wchar_t handling in host_env.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ShaharNaveh ShaharNaveh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tysm!

lgtm:)

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good, i didn't think about this before, thanks!

@youknowone
youknowone merged commit e7be076 into RustPython:main Jul 17, 2026
26 checks passed
@joshuamegnauth54
joshuamegnauth54 deleted the host_env-dont-truncate-wchar_t branch July 17, 2026 03:10
@coderabbitai coderabbitai Bot mentioned this pull request Jul 18, 2026
2 tasks
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.

3 participants