host_env: Fix wcslen, leverage NonNull<T> more - #8386
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (4)
📝 WalkthroughWalkthroughWide-string pointer handling now uses ChangesWide-string safety and decoding
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
da37bda to
a2827bc
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
crates/host_env/src/ctypes.rs (1)
2255-2255: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPass the slice directly.
wcharsis already a slice; useWtf8Buf::from_wide(wchars). Run Clippy to confirm and fix any introduced lint. As per coding guidelines, “Always run clippy to lint code withcargo clippybefore completing tasks and fix any warnings or lints introduced by changes.”🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/host_env/src/ctypes.rs` at line 2255, Update the Wtf8Buf::from_wide call to pass the existing wchars slice directly instead of taking a reference to it, then run cargo clippy and resolve any warnings introduced by this change.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/host_env/src/ctypes.rs`:
- Around line 2414-2418: Update the function returning Option<Wtf8Buf> to reject
a null ptr_value before computing any offset. Use checked arithmetic for the
start * wchar_size offset and address calculation, returning None on overflow;
only construct NonNull and call read_wide_string_strided after these validations
succeed.
In `@crates/host_env/src/wmi.rs`:
- Around line 474-483: Update the nullable length handling in the inner loop
around prop_name and prop_str so both failure cases clear prop_value with
VariantClear before breaking. Extract the optional length result first, route
either failure through the shared cleanup path, and preserve the existing loop
behavior without duplicating cleanup logic.
---
Nitpick comments:
In `@crates/host_env/src/ctypes.rs`:
- Line 2255: Update the Wtf8Buf::from_wide call to pass the existing wchars
slice directly instead of taking a reference to it, then run cargo clippy and
resolve any warnings introduced by this change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro Plus
Run ID: 94260c2a-7ee6-4bd9-be79-0dfe4b325fe9
📒 Files selected for processing (4)
crates/host_env/Cargo.tomlcrates/host_env/src/ctypes.rscrates/host_env/src/wmi.rscrates/vm/src/stdlib/_ctypes/pointer.rs
| let Some(cb_str1) = NonNull::new(prop_name) | ||
| .map(|prop_name| (unsafe { wcslen(prop_name) } * 2) as u32) | ||
| else { | ||
| break; | ||
| }; | ||
| let Some(cb_str2) = NonNull::new(prop_str.as_ptr().cast_mut()) | ||
| .map(|prop_str| (unsafe { wcslen(prop_str) } * 2) as u32) | ||
| else { | ||
| break; | ||
| }; |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Clear the VARIANT before the new early break.
A null prop_name exits the inner loop before VariantClear runs, leaking any resources owned by prop_value. Route both nullable-length failures through shared cleanup before breaking. As per coding guidelines, “When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once to avoid duplicate code.”
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@crates/host_env/src/wmi.rs` around lines 474 - 483, Update the nullable
length handling in the inner loop around prop_name and prop_str so both failure
cases clear prop_value with VariantClear before breaking. Extract the optional
length result first, route either failure through the shared cleanup path, and
preserve the existing loop behavior without duplicating cleanup logic.
Source: Coding guidelines
a2827bc to
783d82b
Compare
18c0d92 to
31608c2
Compare
`strlen` was both unused and unneeded. Rust's `CStr` handles the length for us without needing to call into `libc` manually or implementing our own version of `strlen`. Our `strlen` implemented a fallback that incremented and dereferenced a pointer to check for NUL. This is slower than what `libc` usually does. For example, `musl` operates on words instead of individual bytes. This implementation also caused UB if the caller passed in a null pointer. `wcslen` is similar. The currently implementation can cause UB for null pointers. It's also slow from the same reason mentioned above. Luckily, the widestring crate can handle this for us. The crate panics on null pointers, so using it was a good excuse to use NonNull in more places.
31608c2 to
021cca1
Compare
strlenwas both unused and unneeded. Rust'sCStrhandles the length for us without needing to call intolibcmanually or implementing our own version ofstrlen. Ourstrlenimplemented a fallback that incremented and dereferenced a pointer to check for NUL. This is slower than whatlibcusually does. For example,musloperates on words instead of individual bytes. This implementation also caused UB if the caller passed in a null pointer.wcslenis similar. The currently implementation can cause UB for null pointers. It's also slow from the same reason mentioned above. Luckily, the widestring crate can handle this for us. The crate panics on null pointers, so using it was a good excuse to use NonNull in more places.Summary
Summary by CodeRabbit