Skip to content

PCWSTR has undocumented undefined behavior on non-windows targets #2996

@tyilo

Description

@tyilo

Summary

When using PWCSTR and PWSTR on non-windows targets, wcslen is incorrectly used to determine the length of a null-terminated string.

The as_wide implementations needs to be fixed in one of the following ways:

  • It could be disabled on non-windows targets
  • It could instantly panic on non-windows targets
  • It could be fixed to correctly count the non-zero u16 characters on non-windows targets

Crate manifest

[package]
name = "windows-test"
version = "0.1.0"
edition = "2021"

[dependencies]
windows-core = "0.56.0"

Crate code

use windows_core::PCWSTR;

fn main() {
    let arr = [b'a' as u16, b'b' as u16, 0, 0];
    let s = PCWSTR::from_raw(&arr as *const u16);
    let slice = unsafe { s.as_wide() };
    // Should output [97, 98]
    // Outputs [97] on Linux
    println!("{:?}", slice);

    let arr = [b'a' as u16, 0];
    let s = PCWSTR::from_raw(&arr as *const u16);
    let slice = unsafe { s.as_wide() };
    // Should output [97]
    // UB on Linux
    println!("{:?}", slice);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions